This is my code to insert image into the database:
private void tbSaveActionPerformed(java.awt.event.ActionEvent evt) {
ImageIcon imgIcon = (ImageIcon) tbCapture.getIcon();
Image im = imgIcon.getImage();
byte[] imgByte = getByteArrayFromImage(im);
try{
db.putData("update patient set image ='"+Arrays.toString(imgByte)+"' where id = '"+wtid.getText()+"'");
JOptionPane.showMessageDialog(null, "Image Inserted");
}
catch(Exception ex){
}
}
private byte[] getByteArrayFromImage(Image img){
byte[] imgByte = null;
try {
int imgWidth = img.getWidth(null);
int imgHeight = img.getHeight(null);
int imgType = BufferedImage.TYPE_INT_ARGB; // you can experiment with this one
BufferedImage bi = new BufferedImage(imgWidth, imgHeight, imgType);
Graphics2D g2 = bi.createGraphics();
g2.drawImage(img, 0, 0, null);
g2.dispose();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (ImageIO.write(bi, "JPEG", baos)) {
imgByte = baos.toByteArray();
}
} catch (Exception ex) {
}
return imgByte;
}
And this is my code to retrieve the image and display in jLabel:
try{
ResultSet rset = db.getData("select image from patient where name = '"+tPatientid.getText()+"'");
while (rset.next()){
byte[] imagedata = rset.getBytes("image");
if (imagedata != null){
ImageIcon imageIcon = new ImageIcon(imagedata);
java.awt.Image img = imageIcon.getImage();
this.dImage.setIcon(imageIcon);
this.dImage.setText(null);
}
}
}
catch(Exception ex){
}
I am not getting any error but the image is not getting displayed. I have enclosed the jLable in Desktop pane
Aucun commentaire:
Enregistrer un commentaire