我正在尝试在sql db中的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
}
答案 0 :(得分:1)
为什么需要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));