【问题标题】:MySQL blob to Netbeans JLabelMySQL blob 到 Netbeans JLabel
【发布时间】:2013-03-15 07:12:54
【问题描述】:

我的 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 的名称

【问题讨论】:

  • 调试代码中的每一行。检查你得到了多少行,检查'blob'是否为空,检查检索的字节是否正常,检查是否创建了'image'(不为空且宽度/高度> 0)。
  • 我的if (pic.getRow() == 1) 等于 0
  • 但我的 SQL 语句是正确的@StanislavL
  • 是的@StanislavL 现在没问题,但我仍然不会显示
  • @Martijn Courteaux 需要帮助

标签: java swing netbeans blob jlabel


【解决方案1】:

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

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;
    }

然后将图片设为Icon:

ImageIcon image1 = new ImageIcon(im);

然后将图标添加到 Jlabel 中:

picture.setIcon(image);
getContentPane().add(picture);
setVisible(true);

【讨论】:

    【解决方案2】:

    使用resultset

     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

    【讨论】:

    • 不,我不能使用结果集,因为我试过了,我总是有用户连接错误,因为我这里有 100 多台计算机
    【解决方案3】:

    我得到了 我的文件名应该是这样的

      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);
             }
    

    【讨论】:

      【解决方案4】:

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

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

      JLabel 有一个 setIcon(Icon) 方法。

      label.setIcon(new ImageIcon(ByteStreams.toByteArray(blob.getBinaryStream())));
      

      【讨论】:

        【解决方案5】:

        试试:

        picture.setIcon(new ImageIcon(ByteStreams.toByteArray(blob.getBinaryStream())));
        

        【讨论】:

          猜你喜欢
          • 2015-06-26
          • 1970-01-01
          • 1970-01-01
          • 2015-07-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-29
          相关资源
          最近更新 更多