Arun
Arun

Reputation: 97

Create image from BLOB using ImageIO?

I tried to create image from BLOB. I try following code but it is not working at step:

ImageIO.write(image, "JPG", iio);) 

image is null. Please give me any suggestion.

 byte[] imgData = null;
 if (rs.next ())
 {
   Blob img  = rs.getBlob(1);
   imgData = img.getBytes(1,(int)img.length());
   File f1 = new File(fillFilePath); //fillFilePath = path where image want to store
   BufferedImage image = ImageIO.read(new ByteArrayInputStream(imgData));
   ImageOutputStream iio = ImageIO.createImageOutputStream(f1);
   ImageIO.write(image, "JPG", iio);
 }

How to create image from BLOB using ImageIO?

Upvotes: 1

Views: 3118

Answers (2)

Kiran Wayal
Kiran Wayal

Reputation: 11

Below code works for me. I am able to retrieve BLOB(oracle)/binary(hive) from hive:

InputStream is=rs.getBinaryStream(1);

toImage(is,"C:\\hive_image.png");

public void toImage(InputStream is,String imagePath) throws IOException
{

    BufferedImage bufferedImage=ImageIO.read(is);
    ImageIO.write(bufferedImage, "png", new File(imagePath));

}

Upvotes: 0

Thomas
Thomas

Reputation: 88727

From the JavaDoc on ImageIO.read(InputStream):

If no registered ImageReader claims to be able to read the resulting stream, null is returned.

It seems your blob doesn't represent an image format that ImageIO is able to understand. What format does the image stored in the blob have?

Upvotes: 2

Related Questions