将内存中的图像转换为Blob [英] converting Image in memory to a Blob

查看:135
本文介绍了将内存中的图像转换为Blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在内存中有一个Image(类型:java.awt.Image),我想使用jdk 1.7将它转换为Blob(类型:java.sql.Blob)。

I have an Image (type: java.awt.Image) in memory, I want to convert it to a Blob (type: java.sql.Blob) using jdk 1.7.

我在这个主题上找到的所有东西都使用了流和文件。当然我不需要将此图像保存到文件中才能转换它?

Everything I've been able to find on this subject uses streams and files. Surely I don't need to save this Image to a file before being able to convert it??

这里没有太多要显示的,但下面是一个例子:

Not much to show here, but an example follows:

import java.sql.Blob;
import java.awt.Image;

import java.sql.Blob; import java.awt.Image;

public GenericResponseType savePhoto(Image image)
{
       Connection conn = ds.getConnection();

       << some DB code and assembly of PreparedStatement >>

       Blob blob = conn.createBlob();

           << here's where the magic needs to happen I need to get the image parameter to blob >>
           // I've tried the following but doesn't quite work as it wants a RenderedImage
       // OutputStream os = blob.setBinaryStream(1);
       // ImageIO.write(parameters.getPhoto().getImage(), "jpg", os);


       pstmt.setBlob(4, blob);
     }

更多细节(虽然我怀疑它很重要)是以上是使用来自WSDL的Web服务/ JAX-WS生成,并使用MTOM声明操作。因此它生成一个签名,其中Image作为变量传递。

Some more detail (though I doubt it matters much) is that the above is generated using web services/JAX-WS from WSDL with an operation declared using MTOM. So it generates a signature with an Image being passed as a variable.

推荐答案

java.awt.Image 非常简单。它没有提供任何可以写入/保存图像的方法,也没有提供任何方法来访问图像的基础像素数据。

java.awt.Image is pretty simply. It does not provide any means by which the image can be written/saved nor does it provide any means to get access to underlying pixel data of the image.

第一步,正在将 java.awt.Image 转换为 ImageIO 可以支持的内容。这将允许您将图像数据写出...

The first step, is converting the java.awt.Image to something that ImageIO can support. This will allow you to write the image data out...

ImageIO 需要 RenderedImage ,因为它是主要的图像源。 BufferedImage 是默认库中此接口的唯一实现...

ImageIO requires a RenderedImage as it's primary image source. BufferedImage is the only implementation of this interface within the default libraries...

不幸的是,没有简单的方法从一个转换到另一个。幸运的是,这并不难。

Unfortunately, there's no simply method for converting from one to the other. Fortunately, it's not to hard.

Image img = ...;

BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
g2d.drawImage(img, 0, 0, null);
g2d.dispose();

基本上,这只是描绘了原始的 java.awt.Image BufferedImage

Basically, this just paints the original java.awt.Image onto the BufferedImage

接下来,我们需要以某种方式保存图像以便它可以产生一个 InputStream ...

Next, we need to save the image in some way so that it can produce a InputStream...

这不是最优的,但可以完成工作。

This is a little less then optimal, but gets the job done.

ByteArrayOutputStream baos = null;
try {
    baos = new ByteArrayOutputStream();
    ImageIO.write(bi, "png", baos);
} finally {
    try {
        baos.close();
    } catch (Exception e) {
    }
}
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

基本上,我们将图像写入 ByteArrayOutputStream 并使用结果生成 ByteArrayInputStream

Basically, we write the image out to a ByteArrayOutputStream and the use the result to generate a ByteArrayInputStream

现在。如果内存有问题或图像相当大,您可以先将图像写入文件,然后只需阅读文件通过某种输入来回输入而不是......

Now. If memory is an issue or the image is rather large, you can first write the image to a File and then simply read the File back in via some kind of InputStream instead...

最后,我们设置 InputStream 到所需的列...

Finally, we set the InputStream to the required column...

PreparedStatement stmt = null;
//...    
stmt.setBlob(parameterIndex, bais);

而Blob是你的叔叔......

And Blob's your uncle...

这篇关于将内存中的图像转换为Blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆