java - 将 BufferedImage 转换为 Image 数据类型

标签 java mysql imageview blob bufferedimage

我正在尝试从数据库表中检索 blob 数据类型并在 imageView 中显示图像。这是我的选择 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;
}

检索后,我想将其显示在 ImageView 中。这是代码:

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

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

提前致谢。

最佳答案

Toolkit.getDefaultToolkit().createImage(data)

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

关于java - 将 BufferedImage 转换为 Image 数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17236894/

相关文章:

android - ImageView 适合宽度裁剪​​底部

android - 为什么找不到 ImageView 类?

java - 无法在 Person 和 Address 类之间建立 'many-to-many' 关系

java - Java 源文件中的重音字符在 JSF 页面中无法正确显示

java - 部署到 tomcat 时 GWT url 发生变化

java - 使用 Tomcat 时禁用测试 Web 服务。可能是什么原因呢?

Python、MySQL 使用变量时不插入。可以就地使用变量的精确值,并且它可以工作

php - 查询数据库以取回数组

mysql - Magento 2 独立的读/写数据库连接

android - 如何使用 JDBC Drive 将 MySql 表图像 (BLOB) 加载到 ImageView