Java 如何将 BufferedImage 转换为 InputStream?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4251383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert BufferedImage to InputStream?
提问by
I am uploading images using servlet. To perform resize operations i am converting InputStream to BufferedImage. Now i want to save it in mongoDB. Since, i am new to mongoDB as far as i know, GridFS takes InputStream.
我正在使用 servlet 上传图像。为了执行调整大小操作,我将 InputStream 转换为 BufferedImage。现在我想将它保存在 mongoDB 中。因为,据我所知,我是 mongoDB 的新手,GridFS 采用 InputStream。
So, is there any way to convert BufferedImage to InputStream?
那么,有没有办法将BufferedImage 转换为InputStream?
采纳答案by SLaks
You need to save the BufferedImage to a ByteArrayOutputStream
using the ImageIO
class, then create a ByteArrayInputStream
from toByteArray()
.
您需要ByteArrayOutputStream
使用ImageIO
类将 BufferedImage 保存到 a ,然后创建一个ByteArrayInputStream
from toByteArray()
。
回答by Edward83
First of all you must get your "bytes":
首先,你必须得到你的“字节”:
byte[] buffer = ((DataBufferByte)(bufferedImage).getRaster().getDataBuffer()).getData();
And then use ByteArrayInputStream(byte[] buf)constructor to create your InputStream;
然后使用ByteArrayInputStream(byte[] buf)构造函数来创建您的 InputStream;
回答by Igor
By overriding the method toByteArray()
, returning the buf
itself (not copying), you can avoid memory related problems. This will share the same array, and 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.
通过覆盖方法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 Sorter
BufferedImage
? ByteArrayOutputStream
? byte[]
? ByteArrayInputStream
BufferedImage
? ByteArrayOutputStream
? byte[]
?ByteArrayInputStream
Use the ImageIO.write
method to make a BufferedImage
(which is a RenderedImage
) into a ByteArrayOutputStream
. From there get a byte array (byte[]
), feeding that into an InputStream
of type ByteArrayInputStream
.
使用该ImageIO.write
方法将 a BufferedImage
(即 a RenderedImage
)变成 a ByteArrayOutputStream
。从那里得到一个字节数组 ( byte[]
),将它输入到InputStream
类型中ByteArrayInputStream
。
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(buffImage, "jpeg", os); // Passing: ?(RenderedImage im, String formatName, OutputStream output)
InputStream is = new ByteArrayInputStream(os.toByteArray());
Both the ByteArrayOutputStream
and InputStream
implement AutoCloseable
. So you can conveniently have those closed automatically by using try-with-resourcessyntax.
无论是ByteArrayOutputStream
和InputStream
落实AutoCloseable
。因此,您可以使用try-with-resources语法方便地自动关闭它们。