在 Java 面板上显示来自数据 URI 方案的图像

Show image from Data URI scheme on Java panel

我无法理解这个问题。

我收到一个 JSON 格式的 img,它是 Base64 格式的,对于 Web 开发人员来说,它唯一的作用是: "".

如何在 Java 中完成?

您可以使用 apache commons-codec 来转换字节数组。到 base64 和从 base64 到字节数组。 实际上,您可以使用番石榴。这个工件有 base64 库和 json。 只需在您的 maven 项目中添加 com.google.guava。

要创建图像,您可以使用:

InputStream in = new ByteArrayInputStream(yourbytearray);
BufferedImage bImageFromConvert = ImageIO.read(in);
ImageIO.write(bImageFromConvert, "jpg", new File("c:/yourimage.jpg"));
    //JSON funtion
    String cmd_getPhoto = so.cmd_getPhoto();

    //for remove ":"data:image\/png;base64,"
    String imageDataBytes = cmd_getPhoto.substring(cmd_getPhoto.indexOf(",") + 1);
    //for decode
    Base64 b = new Base64();
    byte[] decode = b.decode(imageDataBytes.getBytes());

    //create the stream
    InputStream stream = new ByteArrayInputStream(decode);

    try {
        //set the stream for a bufferedImage and do what your will with it
        BufferedImage bitmap = ImageIO.read(stream);
        jLabel6.setIcon(new ImageIcon(bitmap));
    } catch (IOException ex) {    }