0

我的 MySQL 中有一个 blob 类型字段,我想将此字段中的数据JLabel作为图标放入。例如,这JLabel将是我表单中用户的个人资料图片。

我使用了这些代码,但没有任何反应,而且我想fix to width或修复我的 jlabel 中的任何图像大小

DefaultTableModel pic = MyDB.DataTable("SELECT `Picture` FROM `photo` WHERE `Employee ID` = 'EQ0103'");
     if (pic.getRowCount() > 0){
         Blob blob = pic.getBlob(1);
         byte[] image1 = blob.getBytes(1, ALLBITS);
         ImageIcon image = new ImageIcon(image1);
         picture.setIcon(image);
         getContentPane().add(picture);
         setVisible(true);
     }

picture是我的 jlabel 的名字

4

5 回答 5

6

首先:从您的数据库返回输入流:

String query = "SELECT `Picture` FROM `photo` WHERE `Employee ID` = 'EQ0103'";
stmt = (PreparedStatement) con.prepareStatement(query);
ResultSet result = stmt.executeQuery();

从数据库返回的图像

BufferedImage im = ImageIO.read(result.getBinaryStream(1));

然后对此图像进行调整:

im =linearResizeBi(im, /*width*/, /*height*/);

linearResizeBi方法:

static public BufferedImage linearResizeBi(BufferedImage origin, int width, int height) {
        BufferedImage resizedImage = new BufferedImage(width, height ,BufferedImage.TYPE_INT_RGB);
        Graphics2D g = resizedImage.createGraphics();
        float xScale = (float)width / origin.getWidth();
        float yScale = (float)height / origin.getHeight();
        AffineTransform at = AffineTransform.getScaleInstance(xScale,yScale);
        g.drawRenderedImage(origin,at);
        g.dispose();
        return resizedImage;
    }

然后使图像是一个图标:

ImageIcon image1 = new ImageIcon(im);

然后将图标添加到 Jlabel :

picture.setIcon(image);
getContentPane().add(picture);
setVisible(true);
于 2013-03-24T14:29:18.780 回答
1

使用结果集

 Statement stmt = con.createStatement();
 ResultSet rs = stmt.executeQuery("SELECT `Picture` FROM `photo` WHERE `Employee ID` = 'EQ0103'");

你可以从

Blob blob = rs.getBlob(1);

换成另一种选择

InputStream binaryStream = rs.getBinaryStream(1);

您可以在此处参考从博客获取图像的官方指南 http://docs.oracle.com/javase/1.5.0/docs/guide/jdbc/blob.html

于 2013-03-20T04:20:48.800 回答
1

我的文件名应该是这样的

  txtPicPath.setText(file.getAbsoluteFile().toString());

我使用了这些代码,它也适合 jlabel 大小

 ResultSet rst = MyDB.rsFetch("SELECT `Picture` FROM `photo` WHERE `Employee ID` = '"+ Data.User.getText()+"'");
         while (rst.next()) {
         Blob filenameBlob = rst.getBlob("Picture");
         byte[] content = filenameBlob.getBytes(1L,(int)filenameBlob.length());
         ImageIcon ik = new ImageIcon(content);
         Image img = ik.getImage();
         Image newimg = img.getScaledInstance(Data.picture.getWidth(), Data.picture.getHeight(), java.awt.Image.SCALE_SMOOTH);
         ik = new ImageIcon(newimg);
         Data.picture.setIcon(ik);
         }
于 2013-03-26T07:28:26.037 回答
0

Blob 有一个 getBinaryStream(),它返回一个字节流,其中包含存储在 Blob 中的数据。

实现 Icon 的 ImageIcon 有一个以字节数组作为参数的构造函数。

JLabel 有一个 setIcon(Icon) 方法。

label.setIcon(new ImageIcon(ByteStreams.toByteArray(blob.getBinaryStream())));
于 2013-03-20T15:23:38.907 回答
0

尝试:

picture.setIcon(new ImageIcon(ByteStreams.toByteArray(blob.getBinaryStream())));
于 2013-03-21T11:19:47.323 回答