如何将 BufferedImage 对象转换为 InputStream 或 BLOB?

发布于 2024-12-08 08:00:24 字数 464 浏览 0 评论 0原文

我正在尝试使用 LONGBLOB 属性将用户上传的图像存储到数据库中...我遇到了 PreparedStatement 的问题,它有两种方法来设置Blob 分别是:

public void setBinaryStream(intparameterIndex, InputStream x)

public void setBlob(intparameterIndex, Blob x)

public void setBlob(intparameterIndex, InputStream) inputStream)

现在的问题是我有一个 BufferedImage 对象,必须将其转换为 BlobInputStream 才能上传...

如何我可以在不丢失原始图像格式或质量的情况下执行此操作吗?

I am trying to store an image uploaded by the user into the database with a LONGBLOB attribute... I ran into a problem with a PreparedStatement that has two methods to set a blob which are:

public void setBinaryStream(int parameterIndex, InputStream x)

public void setBlob(int parameterIndex, Blob x)

public void setBlob(int parameterIndex, InputStream inputStream)

Now the problem is I have a BufferedImage object which must be converted into Blob or InputStream to upload...

How can I do this without losing the original image format or quality?

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

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

发布评论

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

评论(4

↙温凉少女 2024-12-15 08:00:24

如何在不丢失原始图像格式或质量的情况下执行此操作?

您需要使用某种适当的(无损)格式保存它。 png 格式 是一种选择。

ImageIO 类具有用于写入的方法将图像输出到输出流。

以下是如何获取 InputStream 的完整示例,您可以从中读取 BufferedImage 内容的 PNG 表示形式:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", baos);
InputStream is = new ByteArrayInputStream(baos.toByteArray());

How can I do this without losing the original image format or quality?

You need to save it using some appropriate (lossless) format. The png format is one option.

The ImageIO class has methods for writing out an image to an output stream.

Here's a complete example of how you get hold of an InputStream from which you can read the PNG-representation of the content of a BufferedImage:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", baos);
InputStream is = new ByteArrayInputStream(baos.toByteArray());
冷心人i 2024-12-15 08:00:24

下面是示例代码:

ByteArrayOutputStream bas = new ByteArrayOutputStream();
ImageIO.write(image,"jpg", bas);
byte[] bytes = bas.toByteArray();
InputStream is = new ByteArrayInputStream(bytes);

不要忘记检查第二个 ImageIO.write 参数是否符合您所需的图像格式(例如“jpg”、“bmp”、“png”)。

Here is a sample code:

ByteArrayOutputStream bas = new ByteArrayOutputStream();
ImageIO.write(image,"jpg", bas);
byte[] bytes = bas.toByteArray();
InputStream is = new ByteArrayInputStream(bytes);

Don't forget to check the second ImageIO.write parameter to your required image format (e.g. "jpg", "bmp", "png").

纸伞微斜 2024-12-15 08:00:24

编写函数来序列化/反序列化 BufferedImage
到/从 byte [] 并使用类 ByteArrayInputStream
ByteArrayOutputStream

您可能需要序列化图像类型、宽度、高度
和图像像素值(通过 BufferedImage.getRGB(x, y) 获得)。

另一种方法是调用 ImageIO.write(image, "png", outStream)
将图像转换为 PNG 格式并将其写入
ByteArrayOutputStream。有一个等效的ImageIO.read(inputStream)
方法从 ByteArrayInputStream 读取图像。
与 PNG 之间的转换会产生一些处理开销。
但PNG是一种压缩图像格式,你会节省很多
与存储未压缩的图像相比,节省了空间。

Write functions to serialize/unserialize the BufferedImage
to/from a byte [] and use classes ByteArrayInputStream and
ByteArrayOutputStream.

You probably need to serialize the image type, width, height
and the image pixel values (obtained with BufferedImage.getRGB(x, y)).

The alternative is to call ImageIO.write(image, "png", outStream)
to convert the image to PNG format and write it to a
ByteArrayOutputStream. There is an equivalent ImageIO.read(inputStream)
method to read the image from a ByteArrayInputStream.
There is some processing overhead in converting to/from PNG.
But PNG is a compressed image format and you will save a lot
of space compared with storing the images uncompressed.

属性 2024-12-15 08:00:24

要创建 Blob:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", baos);
Blob blFile = new javax.sql.rowset.serial.SerialBlob(baos.toByteArray());

To create a Blob:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", baos);
Blob blFile = new javax.sql.rowset.serial.SerialBlob(baos.toByteArray());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文