gpt4 book ai didi

java - 从数据库中提取图像(blob)并将其插入框架 Java

转载 作者:行者123 更新时间:2023-11-29 01:41:06 27 4
gpt4 key购买 nike

我有一个带有表“Images”的数据库,其中包含一个包含图像(类型 BLOB)的字段“Img”。

我用这样的查询 sql 提取图像:

select img from images where id = 1

在java swing项目中,如何获取该查询的图像结果?

最佳答案

这应该可行

Blob blob = rs.getBlob("img");
int blobLength = (int) blob.length();

byte[] bytes = blob.getBytes(1, blobLength);
blob.free();
BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytes));

您可以选择如何将其添加到框架中。您可以将它添加到 JLabel 中,也可以在 JPanel 上绘制它。仅取决于您的要求和/或偏好。

  • 使用JLabel

    ImageIcon icon = new ImageIcon(bytes); // you can read straight from byte array
    JLabel label = new JLabel(icon);
    frame.add(label);
  • JPanel上绘画

    @Override
    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
    // img is the BufferedImage in the first code.
    }

关于java - 从数据库中提取图像(blob)并将其插入框架 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21887882/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com