如何将 ImageIcon 变回 blob?
我已将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会尝试这样的事情
请参阅 http ://download.oracle.com/javase/1.4.2/docs/api/java/awt/image/PixelGrabber.html
您可能需要适当调整它并指定正确的宽度和高度。
I'd try something like this
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.
在 Blob API 中:
setBinaryStream(long pos)
返回一个OutputStream
“检索可用于写入此 Blob 对象表示的 BLOB 值的流。”因此您可以将您的位直接写入该流。
我不太确定如何从修改后的 ImageIcon 中获取像素。如果您可以将其转换为
BufferedImage
,则该类具有getRGB
方法。In the Blob API:
setBinaryStream(long pos)
returns anOutputStream
"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 aBufferedImage
, that class has agetRGB
method.