1

I have some images stored in an oracle DB as blobs. I want to read them and display in a JLabel. After reading them, I have tried using ImageIO.read but it always returns null. See my code below:

Blob blob = rs.getBlob(2);
BufferedImage frontImg = ImageIO.read(blob.getBinaryStream());
lblFrontImage.setIcon(new ImageIcon(frontImg));

I am able to save the image to a file however using the following code so I know the image is valid:

Blob blob = rs.getBlob(2);
InputStream in = blob.getBinaryStream();
OutputStream out = new FileOutputStream("test.jpg");
byte[] buff = new byte[4096];  
int len = 0;
while ((len = in.read(buff)) != -1) {
   out.write(buff, 0, len);
}
out.close();

Other ways I have tried to display the image in a JLabel

byte[] frontBytes = rs.getBytes(2);
BufferedImage frontImg = ImageIO.read(new 
ByteArrayInputStream(fileContent));
lblFrontImage.setIcon(new ImageIcon(frontImg));

And

byte[] frontBytes = rs.getBytes(2);
BufferedImage image;
ByteArrayInputStream bis = new ByteArrayInputStream(frontBytes);
image = ImageIO.read(bis);
bis.close();
lblFrontImage.setIcon(new ImageIcon(image));

Also

InputStream in = blob.getBinaryStream();
image = ImageIO.read(in);
byte[] frontImgBytes = blob.getBytes(1, (int) blob.length());
System.out.println("front bytes length: ====\n" + frontImgBytes.length);
BufferedImage frontImage = ImageIO.read(new 
ByteArrayInputStream(frontImgBytes));
lblFrontImage.setIcon(new ImageIcon(frontImage));

Tried a lot of ways, just keep getting java.lang.NullPointerException. No other exception or error. Any help will be greatly appreciated.

Dare
  • 31
  • 3
  • I believe your first attempt should have worked fine, if the image is in a format ImageIO understands. Can you please attach or link the image file you stored to disk, without using ImageIO? That would help a lot. – Harald K Apr 01 '18 at 19:20

1 Answers1

1

I finally realised it was because the images were TIFF images. I couldn't use the default ImageIO libraries. I noticed another StackOverflow thread here Can't read and write a TIFF image file using Java ImageIO standard library and used your twelvemonkeys libraries @haraldK and it worked fine. Thanks a lot.

Dare
  • 31
  • 3