Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have blob data in oracle database i have converted it to byte array.

Now i need to pass this byte array to a function which accepts only Image object.

I want to know how to convert the byte array to Image.

This is the code that I'm using for conversion right now.

Java
Blob temp;
Image[] tempImg=new Image[1];
temp = rs.getBlob("FACE");
nt blobLength = (int) temp.length();
byte[] blobbytes = temp.getBytes(1, blobLength);
tempImg[0] = new ImageIcon(blobbytes).getImage();


getBlob is the function to get the blob data form the database.

any help will be appreciated.
Thanks in advance.
Posted
Updated 6-Nov-12 19:24pm
v2

1 solution

You should do it like this:

How To Convert Byte[] To BufferedImage In Java[^]. BufferedImage is a subtype of Image.
 
Share this answer
 
Comments
Manish_Mohan 7-Nov-12 5:48am    
I am the using the same code.

but i'm getting the buffered image as null.
TorstenH. 7-Nov-12 5:50am    
you might not be able to write on the HDD. Check the user rights of the OS.
You should use a temporary folder to write the file if that is possible.
Manish_Mohan 7-Nov-12 5:54am    
I tried all that.

i kept an "if" condition that the file will only be written in the HDD if the "bImageFromConvert" in not null.
but it is coming out to be null.

here is the code :

temp = rs.getBytes("FACE");

if (temp != null) {
InputStream in = new ByteArrayInputStream(temp);
BufferedImage bImageFromConvert = ImageIO.read(in);
if (bImageFromConvert != null) {
ImageIO.write(bImageFromConvert, "jpg", new File(
"E:\\face.jpg"));

JOptionPane.showMessageDialog(null, "image");
}
else
JOptionPane.showMessageDialog(null, "bImageFromConvert is null");
}
Sergey Alexandrovich Kryukov 12-Feb-13 2:06am    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900