如何从 BufferedImage 获取输入流?

发布于 2024-07-15 12:39:46 字数 352 浏览 4 评论 0原文

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

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

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

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);

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迟到的我 2024-07-22 12:39:46

来自 http://usna86-techbits.blogspot.com/ 2010/01/inputstream-from-url-bufferedimage.html

它工作得很好!

以下是您如何制作
BufferedImage 的输入流:

URL url = 新 URL("http://www.google.com/intl/en_ALL/images/logo.gif"); 
  BufferedImage 图像 = ImageIO.read(url); 
  ByteArrayOutputStream os = new ByteArrayOutputStream(); 
  ImageIO.write(图像,“gif”,操作系统); 
  输入流 = new ByteArrayInputStream(os.toByteArray()); 
  

From http://usna86-techbits.blogspot.com/2010/01/inputstream-from-url-bufferedimage.html

It works very fine!

Here is how you can make an
InputStream for a BufferedImage:

URL url = new URL("http://www.google.com/intl/en_ALL/images/logo.gif");
BufferedImage image = ImageIO.read(url);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(image, "gif", os);
InputStream is = new ByteArrayInputStream(os.toByteArray());
初见 2024-07-22 12:39:46

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

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

如果您只想要字节,请尝试执行 write 调用,但将其传递给 ByteArrayOutputStream,然后您可以从中获取字节数组并对其执行您想要的操作。

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

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

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.

山川志 2024-07-22 12:39:46

通过重写方法 toByteArray(),返回 buf 本身(而不是复制),您可以避免与内存相关的问题。 这将共享相同的数组,而不是创建另一个正确大小的数组。 重要的是使用 size() 方法来控制数组中有效字节的数量。

final ByteArrayOutputStream output = new ByteArrayOutputStream() {
    @Override
    public synchronized byte[] toByteArray() {
        return this.buf;
    }
};
ImageIO.write(image, "png", output);
return new ByteArrayInputStream(output.toByteArray(), 0, output.size());

By overriding the method toByteArray(), returning the buf itself (not copying), you can avoid memory related problems. This will share the same array, not creating another of the correct size. The important thing is to use the size() method in order to control the number of valid bytes into the array.

final ByteArrayOutputStream output = new ByteArrayOutputStream() {
    @Override
    public synchronized byte[] toByteArray() {
        return this.buf;
    }
};
ImageIO.write(image, "png", output);
return new ByteArrayInputStream(output.toByteArray(), 0, output.size());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文