如何将 SQL 中的 LONGBLOB 图像制作成缓冲图像以在标签中显示

how to make a LONGBLOB image from SQL into a buffered image for displaying in a label

我正在尝试在 sql 数据库中显示来自 longblob 图像的字节数组,然后将其变成 BufferedImage 并将其缩放到我的标签的大小,我知道可以肯定工作的部分是我的 SQL 语句和我在其他地方实现它时的重新缩放。我不知道它实际上是在写入变量 "image" 还是被制作成 icon 然后 bufferedImage。我确定有一种方法可以从一开始就使它成为缓冲图像,但我对 java 的这一部分并不太了解。任何见解都有帮助,下面是我的代码。

private void picPreviewerActionPerformed(java.awt.event.ActionEvent evt) {                                             
    // TODO add your handling code here:

        String vinNumber = vinInput.getText();


try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/newbieswithauctions","root","root");
PreparedStatement ps = con.prepareStatement("SELECT itemImage FROM images WHERE itemVin='"+ vinNumber + "'");

ResultSet rs = ps.executeQuery();    
    byte[] image = null;
    while(rs.next()){
         image = rs.getBytes("itemImage");
    }
Image img = Toolkit.getDefaultToolkit().createImage(image);
ImageIcon icon = new ImageIcon(img);
Image pic = icon.getImage();
BufferedImage bufferedPic =(BufferedImage) pic;
try{

     BufferedImage scaled = getScaledInstance(
     bufferedPic, picView.getWidth(), picView.getHeight(), RenderingHints.VALUE_INTERPOLATION_BILINEAR, true);
     picView.setIcon(new ImageIcon(scaled));
    }catch(Exception ex){

    }




}
catch(Exception ex)
{

}

}                                            

我修改了代码,我有这个按钮,它会打开一个带有图片的新表格,但没有显示这是按钮中的代码

  private void picPreviewerActionPerformed(java.awt.event.ActionEvent evt) {                                             
    // TODO add your handling code here:

    String vinNumber = vinInput.getText();


    try{
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/newbieswithauctions","root","root");
        PreparedStatement ps = con.prepareStatement("SELECT itemImage FROM images WHERE itemVin='"+ vinNumber + "'");

        ResultSet rs = ps.executeQuery();    
            byte[] image = null;
            while(rs.next()){
                 image = rs.getBytes("itemImage");
            }
        InputStream in = new ByteArrayInputStream(image);
        bufferedPic = ImageIO.read(in);
        ImageIO.write(bufferedPic, "png", new File("C:\Users\geluna\Desktop\Software Engineering\NWAGUI-sqlpicsworks\NWAGUI-sqlpicsworks\images\newImage.png"));


    }
    catch(Exception ex)
    {

    }
    new PicSearchWin().setVisible(true);
}  

在表格中,我在主要方法中有这个,所以它会在打开时立即填充图片,但什么也没有发生,但是当我在那个表格中制作一个按钮并将代码放在那里时,它会在按钮打开时工作按下。看起来很糟糕。我希望它在打开表单时执行。有什么想法吗?

private void closeBtnActionPerformed(java.awt.event.ActionEvent evt) {                                         

    // TODO add your handling code here:
    this.dispose();
}                                        

private void btnActionPerformed(java.awt.event.ActionEvent evt) {                                    
    // TODO add your handling code here:
       try{
     BufferedImage NewBufferedPic = ImageIO.read(new File("C:\Users\geluna\Desktop\Software Engineering\NWAGUI-sqlpicsworks\NWAGUI-sqlpicsworks\images\newImage.png"));
     BufferedImage scaled = getScaledInstance(
     NewBufferedPic, pic.getWidth(), pic.getHeight(), RenderingHints.VALUE_INTERPOLATION_BILINEAR, true);
     pic.setIcon(new ImageIcon(scaled));
    }catch(Exception ex){
         JOptionPane.showMessageDialog(null,"GAY", "Error ", JOptionPane.ERROR_MESSAGE);
    }
}                                   





/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(PicSearchWin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(PicSearchWin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(PicSearchWin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(PicSearchWin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new PicSearchWin().setVisible(true);

        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton btn;
private javax.swing.JButton closeBtn;
public static javax.swing.JLabel pic;
// End of variables declaration                   

}

为什么需要 BufferedImage?
如果你存储了一个 BLOB 那么试试这个:

Blob blob = rs.getBlob("itemImage"); 
byte[] bytes = blob.getBytes(1, (int) blob.length());
Image myImage = Toolkit.getDefaultToolkit().createImage(bytes);
Image scaled = createYourScaledInstance();
picView.setIcon(new ImageIcon(scaled));