我正在从数据库中检索一个图像,该图像需要安装在尺寸为300乘300的JLabel内。在此代码中,图像不会调整大小,而是在JLabel内部显示它的一部分:
ResultSet r;
r = s.executeQuery("select * from employee where emp_name='"+user+"'");
boolean v=r.next();
if (v==true) {
add(r.getString("designation"));//to call add function
InputStream is = r.getBinaryStream(3);
BufferedImage bimg = ImageIO.read(is);
bimg.getScaledInstance(300,300,Image.SCALE_SMOOTH);
ImageIcon n=new ImageIcon();
n.setImage(bimg);
l[1].setIcon(n);
}
答案 0 :(得分:2)
getScaledInstance()
不会修改原始图片。做
Image bimg = ImageIO.read(is);
bimg = bimg.getScaledInstance(300,300,Image.SCALE_SMOOTH);
代替。
答案 1 :(得分:1)
让我们说你的BLOB专栏名称是' picture'。
public void ReadImage(String user, JLabel jl){
try{
String query = "SELECT picture FROM employee WHERE emp_name='"+user+"'";
pst = con.prepareStatement(query);
rs = pst.executeQuery();
byte[] imageData = rs.getBytes(1);
ImageIcon ii = new ImageIcon(imageData);
Image image = ii.getImage();
image = image.getScaledInstance(300, 300, Image.SCALE_SMOOTH);
ii = new ImageIcon(image);
jl.setIcon(ii);
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}