将BufferedImage转换为Image数据类型

时间:2022-05-01 16:04:42

I am trying to retrieve blob data type from database table and display the image in a imageView. Here is my select SQL statement:

我试图从数据库表中检索blob数据类型并在imageView中显示图像。这是我的select SQL语句:

public boolean retrieve(){
boolean success = false;
ResultSet rs = null;
DBController db = new DBController();   
db.getConnection();     
String dbQuery = "SELECT * FROM sm_product WHERE productName ='" + name +"'";       
rs = db.readRequest(dbQuery);
try {
    if(rs.next()){
        desc = rs.getString("productDescription");
        price = rs.getInt("productPrice");
        quantity = rs.getInt("productQuantity");
        dateStr = rs.getString("dateOfCreation");
                    category = rs.getString("productCategory");     

                   Blob blob = rs.getBlob("productImage");
                   byte[] data = blob.getBytes(0, (int) blob.length());
                   image = ImageIO.read(new ByteArrayInputStream(data)); //Error here
                    success = true;
    }   
}
catch(Exception e){
    e.printStackTrace();
}   
db.terminate();
return success;
}

After retrieved, I want to display it in a imageview. Here is the code:

检索后,我想在imageview中显示它。这是代码:

panel.getMyImageView().setImage(product.getImage());

However, I got incompatible type error message. I know image = ImageIO.read(new ByteArrayInputStream(data)); this line supposed to store as BufferedImage and then from there I slowly convert to image datatype and display in image view. But after 2 hours of researching, I got no luck. Can somebody please help me with it?

但是,我收到了不兼容的类型错误消息。我知道image = ImageIO.read(new ByteArrayInputStream(data));该行应该存储为BufferedImage,然后从那里我慢慢转换为image数据类型并在图像视图中显示。但经过2个小时的研究,我没有运气。有人可以帮我吗?

Thanks in advance.

提前致谢。

1 个解决方案

#1


0  

Toolkit.getDefaultToolkit().createImage(data)

Call this for the byte array read from the blob

将此调用为从blob读取的字节数组

#1


0  

Toolkit.getDefaultToolkit().createImage(data)

Call this for the byte array read from the blob

将此调用为从blob读取的字节数组