显示blob字段中的图像 - Java

时间:2022-10-07 01:23:28

I have to show a picture stored in a mysql database (in a blob field) in a JLabel or anything else to show it. after writing this line, what should I do?

我必须在JLabel或其他任何东西中显示存储在mysql数据库(在blob字段中)的图片以显示它。写完这一行后,我该怎么办?

Blob b = rs.getBlob ("image"); 

please explain in detail, I have searched all over the site but have not managed to finish anything. thanks in advance to all.

请详细解释,我搜索了整个网站,但还没有完成任何事情。提前感谢所有人。

PS: I ask the administrators to not put off topic immediately because otherwise I can not replicate, thank you :) .

PS:我要求管理员不要立即推迟话题,否则我无法复制,谢谢:)。

stack trace:

java.lang.NullPointerException
Finger was removed from fingerprint reader
at javax.swing.ImageIcon.<init>(Unknown Source)
at AppMain.ExtractDB(AppMain.java:294)
at AppMain.initDB(AppMain.java:271)
at AppMain.initialize(AppMain.java:245)
at AppMain.<init>(AppMain.java:68)
at MainForm.identify(MainForm.java:356)
at MainForm.extract(MainForm.java:257)
at MainForm.onImageAcquired(MainForm.java:213)
at com.griaule.grfingerjava.GrFingerJavaNative.callbackImage(GrFingerJavaNative.java:157)

1 个解决方案

#1


0  

You need to read BLOB's input stream and convert it to byte[] like this:

您需要读取BLOB的输入流并将其转换为byte [],如下所示:

InputStream byte_stream = b.getBinaryStream();
byte [] byte_array = new byte [byte_stream.available()];
int bytes_read = byte_stream.read(byte_array);

And than use this byte_array to create ImageIcon and set into JLabel like this:

然后使用这个byte_array来创建ImageIcon并像这样设置成JLabel:

Another Way using ImageIO

使用ImageIO的另一种方式

InputStream in = b.getBinaryStream();
JLabel label = new JLabel(new ImageIcon(ImageIO.read(in)));

Hope this will give you right direction to resolve your issue

希望这会给你正确的方向来解决你的问题

#1


0  

You need to read BLOB's input stream and convert it to byte[] like this:

您需要读取BLOB的输入流并将其转换为byte [],如下所示:

InputStream byte_stream = b.getBinaryStream();
byte [] byte_array = new byte [byte_stream.available()];
int bytes_read = byte_stream.read(byte_array);

And than use this byte_array to create ImageIcon and set into JLabel like this:

然后使用这个byte_array来创建ImageIcon并像这样设置成JLabel:

Another Way using ImageIO

使用ImageIO的另一种方式

InputStream in = b.getBinaryStream();
JLabel label = new JLabel(new ImageIcon(ImageIO.read(in)));

Hope this will give you right direction to resolve your issue

希望这会给你正确的方向来解决你的问题