如何从 BufferedImage 获取输入流?
如何从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自 http://usna86-techbits.blogspot.com/ 2010/01/inputstream-from-url-bufferedimage.html
它工作得很好!
From http://usna86-techbits.blogspot.com/2010/01/inputstream-from-url-bufferedimage.html
It works very fine!
如果您尝试将图像保存到文件中,请尝试:
如果您只想要字节,请尝试执行 write 调用,但将其传递给 ByteArrayOutputStream,然后您可以从中获取字节数组并对其执行您想要的操作。
If you are trying to save the image to a file try:
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.
通过重写方法
toByteArray()
,返回buf
本身(而不是复制),您可以避免与内存相关的问题。 这将共享相同的数组,而不是创建另一个正确大小的数组。 重要的是使用size()
方法来控制数组中有效字节的数量。By overriding the method
toByteArray()
, returning thebuf
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 thesize()
method in order to control the number of valid bytes into the array.