如何将 ImageIcon 变回 blob?

发布于 2024-11-14 13:26:59 字数 656 浏览 1 评论 0原文

我已将 Blob (PNG 文件)存储到数据库中,如下所示:

File file = new File( "image.png" );
FileInputStream fis = new FileInputStream( file );
statement.setBinaryStream( 1, fis, (int) file.length() );

目前,我使用此过程从数据库获取 Blob 图像并将其转换回图像以供使用:

Blob blob = results.getBlob( 1 );
ImageIcon imageIcon = new ImageIcon( blob.getBytes( 1L, (int) blob.length() ) );

但是,我需要一种方法来将图像放回原处从 ImageIcon 进入数据库(在我更改它之后),而不创建文件,将其存储到磁盘,然后使用 FileInputSteam 将其读回。

为清楚起见进行编辑 好吧,假设我已经将该图像作为 ImageIcon 存储在标签内。我知道如何将其放入数据库的唯一方法是从 FileInputStream 中读取,但这会毫无意义地创建图像文件。那么我如何将 ImageIcon 中的图像作为 BinaryStream 或 Blob 读回数据库呢?

I've stored a Blob (PNG file) into the database like so:

File file = new File( "image.png" );
FileInputStream fis = new FileInputStream( file );
statement.setBinaryStream( 1, fis, (int) file.length() );

Currently I use this process to get the Blob image from the DB and convert it back into an image for use:

Blob blob = results.getBlob( 1 );
ImageIcon imageIcon = new ImageIcon( blob.getBytes( 1L, (int) blob.length() ) );

However, i need a method to put the image back into the database (after i've altered it) from the ImageIcon without creating a file, storing it to the disk then reading it back in with the FileInputSteam.

edit for clarity
Well, say i've got that image stored inside a Label as an ImageIcon. The only way i know how to put that into the database is to read from a FileInputStream, but that would involve pointlessly making a file of the image. So how would i read the Image from ImageIcon back out as a BinaryStream or Blob back to the database?

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

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

发布评论

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

评论(2

柳絮泡泡 2024-11-21 13:26:59

我会尝试这样的事情

PixelGrabber pg = new PixelGrabber(imageIcon.getImage(),0,0, w,h,true);
pg.grabPixels();
// You may need to wait here until all pixels are copied (see ImageObserver)
Object buffer = pg.getPixels();

请参阅 http ://download.oracle.com/javase/1.4.2/docs/api/java/awt/image/PixelGrabber.html

您可能需要适当调整它并指定正确的宽度和高度。

I'd try something like this

PixelGrabber pg = new PixelGrabber(imageIcon.getImage(),0,0, w,h,true);
pg.grabPixels();
// You may need to wait here until all pixels are copied (see ImageObserver)
Object buffer = pg.getPixels();

See http://download.oracle.com/javase/1.4.2/docs/api/java/awt/image/PixelGrabber.html

You may need to tweak it appropriately and specify correct w and h.

咿呀咿呀哟 2024-11-21 13:26:59

在 Blob API 中:

setBinaryStream(long pos) 返回一个 OutputStream

“检索可用于写入此 Blob 对象表示的 BLOB 值的流。”因此您可以将您的位直接写入该流。

我不太确定如何从修改后的 ImageIcon 中获取像素。如果您可以将其转换为 BufferedImage,则该类具有 getRGB 方法。

In the Blob API:

setBinaryStream(long pos) returns an OutputStream

"Retrieves a stream that can be used to write to the BLOB value that this Blob object represents." So you can write your bits directly to that stream.

I'm not quite sure how to get the pixels from your modified ImageIcon. If you can cast it to a BufferedImage, that class has a getRGB method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文