---
category: swing
folder: Base64Encoder
title: Base64エンコーダを使用して画像を文字列に変換する
tags: [Base64, ImageIcon, ImageIO]
author: aterai
pubdate: 2018-10-01T01:53:21+09:00
description: Base64エンコーダで画像ファイルを文字列、デコーダで文字列をImageIconに変換します。
image: https://drive.google.com/uc?id=1Q3H7o8qNXeAHstp2cSIgG5--D-bC41DgOA
---
* Summary [#summary]
`Base64`エンコーダで画像ファイルを文字列、デコーダで文字列を`ImageIcon`に変換します。
#download(https://drive.google.com/uc?id=1Q3H7o8qNXeAHstp2cSIgG5--D-bC41DgOA)
* Source Code Examples [#sourcecode]
#code(link){{
JButton encode = new JButton("encode");
encode.addActionListener(e -> {
JFileChooser chooser = new JFileChooser();
chooser.addChoosableFileFilter(
new FileNameExtensionFilter("PNG (*.png)", "png"));
int retvalue = chooser.showOpenDialog(encode);
if (retvalue == JFileChooser.APPROVE_OPTION) {
Path path = chooser.getSelectedFile().toPath();
try {
textArea.setText(
Base64.getEncoder().encodeToString(Files.readAllBytes(path)));
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
JButton decode = new JButton("decode");
decode.addActionListener(e -> {
String b64 = textArea.getText();
if (b64.isEmpty()) {
return;
}
// byte[] bytes = b64.getBytes(StandardCharsets.ISO_8859_1);
// try (InputStream input = new ByteArrayInputStream(
// Base64.getDecoder().decode(bytes))) {
try (InputStream input = new ByteArrayInputStream(Base64.getDecoder().decode(b64))) {
label.setIcon(new ImageIcon(ImageIO.read(input)));
} catch (IOException ex) {
ex.printStackTrace();
}
});
}}
* Description [#explanation]
* Description [#description]
上記のサンプルでは、`JDK 1.8.0`から導入された[https://docs.oracle.com/javase/jp/8/docs/api/java/util/Base64.html Base64]クラスを使用して、画像ファイルの`Base64`形式文字列へのエンコードや`Base64`形式文字列から`byte`配列へのデコードを行っています。
- `encode`
-- `JFileChooser`で選択した画像ファイルを`Base64.Encoder`の`encodeToString(...)`メソッドで文字列に変換して`JTextArea`に設定
-- ファイルの`byte[]`への変換には`Files.readAllBytes(...)`メソッドを使用
- `decode`
-- `JTextArea`から取得した文字列を`Base64.Decoder`の[https://docs.oracle.com/javase/jp/8/docs/api/java/util/Base64.Decoder.html#decode-java.lang.String- decode(String)]メソッドで`byte`配列に変換、さらに`new ImageIcon(ImageIO.read(new ByteArrayInputStream(byte[])))`で`ImageIcon`に変換して`JLabel`に設定
--- `decode(src)`は`decode(src.getBytes(StandardCharsets.ISO_8859_1))`と同等
* Reference [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/java/util/Base64.html Base64 (Java Platform SE 8)]
- [https://stackoverflow.com/questions/51103717/jeditorpane-content-type-for-html-embedded-base64-images java - JEditorPane Content Type for HTML Embedded Base64 Images - Stack Overflow]
-- `<img src='data:image/png;base64,iVBOR...' />`のように`HTML`テキスト中に埋め込まれた`Base64`文字列を`JEditorPane`(`HTMLEditorKit`)で画像として表示するサンプルが回答されている
* Comment [#comment]
#comment
#comment