本文介绍了如何从 BufferedImage 获取 InputStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

如何从 BufferedImage 对象获取 InputStream?我试过了,但 ImageIO.createImageInputStream() 总是返回 NULL

How can I get an InputStream from a BufferedImage object? I tried this but ImageIO.createImageInputStream() always returns NULL

BufferedImage bigImage = GraphicsUtilities.createThumbnail(ImageIO.read(file), 300);
ImageInputStream bigInputStream = ImageIO.createImageInputStream(bigImage);

图像缩略图正在正确生成,因为我可以成功地将 bigImage 绘制到 JPanel.

The image thumbnail is being correctly generated since I can paint bigImage to a JPanel with success.

推荐答案

如果您尝试将图像保存到文件,请尝试:

If you are trying to save the image to a file try:

ImageIO.write(thumb, "jpeg", new File(....));

如果您只想要字节,请尝试执行 write 调用,但将其传递给 ByteArrayOutputStream,然后您就可以从中取出字节数组并按照您的意愿使用它.

If you just want at the bytes try doing the write call but pass it a ByteArrayOutputStream which you can then get the byte array out of and do with it what you want.

这篇关于如何从 BufferedImage 获取 InputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 15:25