如何转换List Java中的字符串到BufferedImage

问题描述 投票:2回答:4

我想将Base64 String数组转换为BufferedImage。我试图完成的方法:

static BufferedImage decodeToImage(List<String> imageStrings) {
    BufferedImage image = null;
    ByteBuffer imageByteBuffer = ByteBuffer.allocate(500000000);
    try {
        BASE64Decoder decoder = new BASE64Decoder();
        for (String imageString : imageStrings) {
            imageByteBuffer.put(imageString.getBytes());
        }

        ByteArrayInputStream bis = new ByteArrayInputStream(imageByteBuffer.array());
        image = ImageIO.read(bis);
        bis.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return image;
}

我想返回一个BufferedImage,但它返回null。

java string decode bufferedimage
4个回答
2
投票

你可以使用这个:

public static BufferedImage decodeToImage(List<String> imageStrings) {
    try {
        byte[] bytes = Base64.getDecoder().decode(String.join("", imageStrings));
        return ImageIO.read(new ByteArrayInputStream(bytes));
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

1
投票

你看过你进去的是什么?

导致此代码

public static void main(String[] args) {
    String base64String = "data:image/png;base64,iVBORw0...=";
    String[] strings = base64String.split(",");
    List<String> param = new ArrayList<String>();
    param.add(strings[1]);
    BufferedImage bi = decodeToImage(param);
    System.out.println("BufferedImage: " + bi);
}

static BufferedImage decodeToImage(List<String> imageStrings) {
    BufferedImage image = null;
    ByteBuffer imageByteBuffer = ByteBuffer.allocate(500000000);
    try {
        for (String imageString : imageStrings) {
          imageByteBuffer.put(DatatypeConverter.parseBase64Binary(imageString));
        }
        ByteArrayInputStream bis = new ByteArrayInputStream(imageByteBuffer.array());
        image = ImageIO.read(bis);
        bis.close();
    } catch (Exception e) {
        System.out.println("error?");
        e.printStackTrace();
    }
    return image;
}

会打印

BufferedImage:BufferedImage @ 7eda2dbb:type = 6 ColorModel:#pixelBits = 32 numComponents = 4 color space = java.awt.color.ICC_ColorSpace@6576fe71 transparency = 3 has alpha = true isAlphaPre = false ByteInterleavedRaster:width = 302 height = 232 #numDataElements 4 dataOff [0] = 3

但是,如果我只给它整个base64文本,前面有图像信息

 public static void main(String[] args) {
    String base64String = "data:image/png;base64,iVBORw0...=";
    String[] strings = base64String.split(",");
    List<String> param = new ArrayList<String>();
    param.add(base64String); //THIS IS THE DIFFERENT LINE
    BufferedImage bi = decodeToImage(param);
    System.out.println("BufferedImage: " + bi);
    }

它将打印null

将base64String替换为有效的base 64编码png

http://freeonlinetools24.com/base64-image

我在这个例子中将其剪下来,否则它会变成几千个字符。


1
投票

Java中字符串的最大长度是否真的成为您的代码的问题?如果没有,我会像这样简化你的代码(并修复解码错误):

static BufferedImage decodeToImage(String imageString) {
    try {
        BASE64Decoder decoder = new BASE64Decoder();    
        return ImageIO.read(new ByteArrayInputStream(decoder.decode(imageString)));
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

或者,如果你确实有字符串长度的问题(Base64确实产生比输入更长的输出,所以它在极端情况下可能有意义,但验证这一点,因为否则会使你的代码变得复杂),保留列表字符串。我只是跳过ByteBuffer并使用ByteArrayOutputStream代替,因为它更容易使用。

static BufferedImage decodeToImage(List<String> imageStrings) {
    try {
        ByteArrayOuputStream buffer = new ByteArrayOuputStream();

        BASE64Decoder decoder = new BASE64Decoder();
        for (String imageString : imageStrings) {
            buffer.write(decoder.decode(imageString));
        }

        return ImageIO.read(new ByteArrayInputStream(buffer.toArray()));
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

PS:虽然你通常应该关闭流,但close()ByteArrayInputStream上的ByteArrayOutputStream方法是无操作的。


0
投票

使用缓冲区有点棘手。在这里看看可能是一个好主意:https://docs.oracle.com/javase/7/docs/api/java/nio/Buffer.html#flip()

据我所知,你必须准备缓冲区才能阅读它;内部“光标”的位置必须设置为起始位置,然后您可以从中读取“容量”字节。好吧,正如我所说:据我所知。十二年前,我不得不直接使用缓冲区。

© www.soinside.com 2019 - 2024. All rights reserved.