如何将BufferedImage转换为InputStream?

发布于 2024-10-04 01:19:52 字数 181 浏览 6 评论 0原文

我正在使用 servlet 上传图像。为了执行调整大小操作,我将 InputStream 转换为 BufferedImage。现在我想将它保存在 mongoDB 中。因为据我所知,我是 mongoDB 的新手,GridFS 采用 InputStream。

那么,有没有办法将BufferedImage转换为InputStream呢?

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.

So, is there any way to convert BufferedImage to InputStream?

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

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

发布评论

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

评论(4

迷迭香的记忆 2024-10-11 01:19:52

BufferedImageByteArrayOutputStreambyte[]ByteArrayInputStream

使用 ImageIO.write 方法来创建 BufferedImage (这是一个 RenderedImage) 到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());

ByteArrayOutputStreamInputStream 都实现 AutoCloseable。因此,您可以方便地使用 try-with-resources 语法。

BufferedImageByteArrayOutputStreambyte[]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.

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-resources syntax.

绝影如岚 2024-10-11 01:19:52

首先你必须得到你的“字节”:

byte[] buffer = ((DataBufferByte)(bufferedImage).getRaster().getDataBuffer()).getData();

然后使用ByteArrayInputStream(byte[] buf)构造函数来创建你的InputStream;

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;

小ぇ时光︴ 2024-10-11 01:19:52

您需要将 BufferedImage 保存到 ByteArrayOutputStream 使用 ImageIO,然后创建一个 ByteArrayInputStream 来自 toByteArray()

You need to save the BufferedImage to a ByteArrayOutputStream using the ImageIO class, then create a ByteArrayInputStream from toByteArray().

霓裳挽歌倾城醉 2024-10-11 01:19:52

通过重写方法 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, 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.

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 和您的相关数据。
原文