我让转换器在这里工作
public class ImageProcessor { public static void Base64TImage(String base64) { try { byte[] btDataFile = new sun.misc.BASE64Decoder().decodeBuffer(base64); File of = new File("yourFile.png"); FileOutputStream osf = new FileOutputStream(of); osf.write(btDataFile); osf.flush(); } catch (IOException ex) { Logger.getLogger(ImageProcessor.class.getName()).log(Level.SEVERE, null, ex); } } public static void main(String args[]){ String base64 = "iVBORw0KGgoAAAANSUhEUgAAAMcAAABqCAYAAAAIh0X2AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAEihJREFUeNrsXctvG8cZH9H0Q35ElOM4aWBYzKvoIa6YOMihKOD1vYDZHHsRBTRAewp96y30oUVvof+BZH3uIdSlPZbqpUAax1TTAmnqxKTi+FXFImW9qUdn6Blp+HF2d/Y9tL4PoGUud2dn5/t+32vm2xnZ3d0lSEhIg5Rl/4yMjETW4O7yexP0T5F+LPrJ8b+Mmuyzs7XV7D5eanTbTxr0e+PU2zc7yAYkE2mEWY4owEFBcYn+qUhgcKWd7hbZ/KFNtpZWGGhq9FOlQGkhS5CeGXBQUEwywdYFBaTt9U2y8XCB7Gx02dc6/ZQQJEhDDw4KjA84MMJZne0dsv5ggWyvrLGvbfopU4DcQPYgDR04KCjGOChKUXaGAYS6WeKrTQEyjSxCGhpwcGAw96cQR4dWW/eEi4UAQRoecMQNDOFirdy5SyP2vRQzc7GuI6uQUgHHO++8o3Xy57MTn5KnadpYaXt1nazdfSgfsqz3ySyyC8lIy0GtxkdMiyfVsbXvH4kAnVGTWSucE0FKkjKa7tSVJIHB6OjZ0/LXfNL3R0LytBw8zmCaO5d050D2iqV482g9kEyyHHYawGB05Pm+2+bQeiAZYzn4kpB6mh0Eqd02tRzjyDYkEyxHNe0OHs4912c9nnxx8QqyDSlVcFCrMUVinM/QpUPHj8FDJWQbUtqWo2JEBw9nSeboYfmQhWxDSg0cPNbIm9LJQ6PHoGt1CVmHlJblMCorpHCt0HogJQ8OqZLPnE4ePYLgQDLCchS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s4v9U9UmDzFus00FbMdrO4xRbzF543GSx2SlVbY7FTOsCOExhePPm/AAMAAP61LDVrLLgAAAAASUVORK5CYII="; Base64TImage(base64); } }
此转换器创建一个文件并将其放在当前工作目录中。但我不想创建一个文件,我希望这个图像直接转到JLabel。这可能吗?
答案 0 :(得分:8)
以下是基于您的代码的示例:
byte[] btDataFile = new sun.misc.BASE64Decoder().decodeBuffer(base64);
BufferedImage image = ImageIO.read(new ByteArrayInputStream(btDataFile));
JOptionPane.showMessageDialog(null, "", "Image",
JOptionPane.INFORMATION_MESSAGE,
new ImageIcon(image));
我建议您避免使用sun.misc.BASE64Decoder
作为sun.* packages are not part of the supported, public interface
,这里是details。您可以从Apache Commons轻松切换到Base64,即:
byte[] btDataFile = Base64.decodeBase64(base64);
BufferedImage image = ImageIO.read(new ByteArrayInputStream(btDataFile));
JOptionPane.showMessageDialog(null, "", "Image",
JOptionPane.INFORMATION_MESSAGE,
new ImageIcon(image));
答案 1 :(得分:0)
http://commons.apache.org/proper/commons-codec/
的示例 JLabel jLabelImagen = new JLabel();
byte[] imageByte = org.apache.commons.codec.binary.Base64.decodeBase64(base64);
ByteArrayInputStream inputStream = new ByteArrayInputStream(imageByte);
BufferedImage bufImage = ImageIO.read(inputStream);
ImageIcon icon = new ImageIcon(bufImage);
jLabelImagen.setIcon(icon);