[英]ImageIO.read() returning a null value
I am trying to convert a byte array to a bufferedImage
to display in a jLabel
but the ImageIO.read()
property is returning a null value and therefore a NullPonterException
.我正在尝试将字节数组转换为
bufferedImage
以显示在jLabel
中,但ImageIO.read()
属性返回 null 值,因此返回NullPonterException
。 What should I do?我应该怎么办?
InputStream input = new ByteArrayInputStream(array);
try {
BufferedImage bufer = ImageIO.read(input);
ImageIcon icon=new ImageIcon(new ImageIcon(bufer).getImage().getScaledInstance(jLabel3.getWidth(), jLabel3.getHeight(), Image.SCALE_SMOOTH));
jLabel3.setIcon(icon);
} catch (IOException ex) {
Logger.getLogger(Add.class.getName()).log(Level.SEVERE, null, ex);
}`
According to the javadoc , the read(InputStream)
method...根据javadoc ,
read(InputStream)
方法...
"Returns a
BufferedImage
as the result of decoding a suppliedInputStream
with anImageReader
chosen automatically from among those currently registered. TheInputStream
is wrapped in anImageInputStream
. If no registeredImageReader
claims to be able to read the resulting stream,null
is returned. "“返回
BufferedImage
作为解码提供的InputStream
的结果,其中ImageReader
从当前注册的那些中自动选择InputStream
包装在ImageInputStream
中。如果没有注册的ImageReader
声称能够读取生成的 stream,则返回null
。 “
It is most likely that the last sentence explains your problem.最后一句话很可能解释了您的问题。
What should I do?
我应该怎么办?
So your approach to solving this would be:所以你解决这个问题的方法是:
array
is what you expect it to be.array
的内容是否符合您的预期。ImageReader
class for.ImageReader
class 。Thanks for helping me to solve the problem I going to post the response here to help other.感谢您帮助我解决问题,我将在此处发布回复以帮助其他人。
1.The queries to the database (postgresql) must be preparedStatement because if you are saving an image converted to byte [] this declaration gives you a setBinaryStream functionality and when you retrieve it and add it in a byte[] nothing changes 1.对数据库(postgresql)的查询必须是preparedStatement,因为如果您保存转换为byte []的图像,则此声明为您提供setBinaryStream功能,当您检索它并将其添加到byte []中时,没有任何变化
////This way save the image and his path (the last is optional)
JFileChooser f = new JFileChooser();
f.showOpenDialog(null);
File file = f.getSelectedFile();
FileInputStream s = null;
String path = file.getAbsolutePath();
try {
s = new FileInputStream(file);
Conexion();
PreparedStatement pq = conexion.prepareStatement("INSERT INTO prueba(foto, cam) VALUES (?, ?);");
pq.setBinaryStream(1, s, (int) file.length());
pq.setString(2, path);
pq.executeUpdate();
s.close();
} catch (ClassNotFoundException ex) {
Logger.getLogger(Add.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(Add.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Add.class.getName()).log(Level.SEVERE, null, ex);
}
/////This way retrive the info
byte[] array = null;
String photopath = "";
try {
Conexion();
PreparedStatement p = conexion.prepareStatement("SELECT foto, cam FROM prueba;");
ResultSet sq = p.executeQuery();
while (sq.next()) {
array = sq.getBytes("foto");
photopath = sq.getString("cam");
//jLabel3.setIcon(new ImageIcon(array));
break;
}
sq.close();
p.close();
} catch (ClassNotFoundException ex) {
Logger.getLogger(Add.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(Add.class.getName()).log(Level.SEVERE, null, ex);
}
ImageIcon icon=new ImageIcon(array);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.