First : Return the Input stream from your database :
String query = "SELECT `Picture` FROM `photo` WHERE `Employee ID` = 'EQ0103'";
stmt = (PreparedStatement) con.prepareStatement(query);
ResultSet result = stmt.executeQuery();
Returned image from Database
BufferedImage im = ImageIO.read(result.getBinaryStream(1));
Then make rezise to this image :
im =linearResizeBi(im, /*width*/, /*height*/);
linearResizeBi Method :
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;
}
then make the image is an Icon:
ImageIcon image1 = new ImageIcon(im);
then add the Icon to The Jlabel :
picture.setIcon(image);
getContentPane().add(picture);
setVisible(true);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…