Create image from BLOB using ImageIO? - java

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?

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?

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));
}

Related

Trying to read image blob from oracle DB with ImageIO.read in Java not working [duplicate]

This question already has answers here:
Can't read and write a TIFF image file using Java ImageIO standard library
(5 answers)
Closed 4 years ago.
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.
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.

How to convert ImageInputStream to BufferedImage in Java?

I'm trying to retrieve a image blob from mysql database and converting it back to image file. Iterating mysql ResultSet as:
while(rs.next())
{
byte[] byteArray=rs.getBytes("image_blob");
InputStream in=new ByteArrayInputStream(byteArray);
ImageInputStream is = ImageIO.createImageInputStream(in);
BufferedImage image_Bf=ImageIO.read(is);
ImageIO.write(image_Bf, "png",new File("images/"+rs.getString("name")));
}
When I compile and run my java class it gives me error:
java.lang.IllegalArgumentException: image == null!
How can i solve this issue?
ImageIO.read(is); returns null for you. Javadoc says that means it can't locate an appropriate ImageReader, which seems to be a sign of an invalid input.
Actually, your error is in this string: rs.getBytes("name");. You are not getting image content to byteArray, but rather image name in byte form, so ImageIO.read fails.
EDIT:
If it still fails with same error, then you'll have to debug. Try outputting the byte buffer to the file directly. Do you get a valid image this way? I'm pretty sure ImageIO needs a correct image to operate on and convert to PNG.
while(rs.next())
{
byte[] byteArray=rs.getBytes("image_blob");
FileOutputStream fos = new FileOutputStream("images/"+rs.getString("name"));
fos.write(byteArray);
fos.close();
}
successfully resloved this issue.Here is correct code for retrieving images from database
while (rs.next())
{
Blob image_blob=rs.getBlob("image_100x100");
int blobLength = (int) image_blob.length();
byte[] blobAsBytes = image_blob.getBytes(1, blobLength);
InputStream in=new ByteArrayInputStream( blobAsBytes );
BufferedImage image_bf = ImageIO.read(in);
ImageIO.write(image_bf, "PNG", new File(folder_path+"/"+rs.getString("name")));
}

Reading an image as byte array with Android: skImageDecoder::Factory returned null

I have a Spring MVC Web Service that returns an image as a byte array. The output is in json format. The format is png
Here's the code snippet for the image.
BufferedImage img = ImageIO.read(new File(caminho.replace("/", "//")));
imagem = ((DataBufferByte) img.getRaster().getDataBuffer
()).getData();
When I run the server, this is the output:
[{"id":0,"caminhoMDPI":null,"caminhoHDPI":"C:/Users/Marcos/Pictures/postos/drawable-
mdpi/esso_logo.png","caminhoXHDPI":null,"caminhoXXHDPI":null,"imagem":"AAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAA....]
Eventually, other symbols appear on the "imagem" field. I suppose this is right, but I'm not
sure.
On my Android app, I have a routine to download the image and store it as a blob in the database. I receive the json format and transform it into a class with jackson. I've logged the "imagem" field and it looks the same.
My problem is that I can't transform it into an image. Here's the code snippet:
byte[] img_bandeira = cursor.getBlob(cursor.getColumnIndex("img_bandeira"));
Bitmap bmpBandeira = BitmapFactory.decodeByteArray(img_bandeira, 0, img_bandeira.length);
ImageView ivBandeira = (ImageView) view.findViewById(R.id.ivBandeira);
ivBandeira.setImageBitmap(bmpBandeira);
All I get is a message: skImageDecoder::Factory returned null.
I've looked at other similar posts, tried to change some lines, but nothing happened. I don't know what's going on here. Thanks in advance.
I solved it. In the server, the image should be sent like this:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedImage img = ImageIO.read(new File(caminho.replace("/", "//")));
ImageIO.write(img, "png", baos);
baos.flush();
String base64String = Base64.encode(baos.toByteArray());
baos.close();
In my app, it should be read like this:
public static Bitmap getImage(byte[] imageArray) {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imageArray);
Bitmap bitmap = BitmapFactory.decodeStream(byteArrayInputStream);
return bitmap;
}
Thank you, everybody ;)

Converting byte array to jpg image file throughing exception

Hi I have a byte array that I am converting to jpg image but that is giving exception as below please explain me wht is the problem with this.
ByteArrayInputStream bis = new ByteArrayInputStream(someByteArray);
Iterator<?> readers = ImageIO.getImageReadersByFormatName("jpg");
//ImageIO is a class containing static methods for locating ImageReaders
//and ImageWriters, and performing simple encoding and decoding.
ImageReader reader = (ImageReader) readers.next();
Object source = bis;
ImageInputStream iis = ImageIO.createImageInputStream(source);
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
BufferedImage image = reader.read(0, param);
//got an image file
BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
//bufferedImage is the RenderedImage to be written
Graphics2D g2 = bufferedImage.createGraphics();
g2.drawImage(image, null, null);
File imageFile = new File("D:\\newrose2.jpg");
ImageIO.write(bufferedImage, "jpg", imageFile);
Exception:
javax.imageio.IIOException: Invalid JPEG file structure: SOS before SOF
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readImageHeader(Native Method)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readNativeHeader(JPEGImageReader.java:550)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readNativeHeader(JPEGImageReader.java:550)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.checkTablesOnly(JPEGImageReader.java:295)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.gotoImage(JPEGImageReader.java:427)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readHeader(JPEGImageReader.java:543)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:986)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:966)
at Trnsport.writejpegfile(Trnsport.java:398)
at Trnsport.getData(Trnsport.java:107)
at Trnsport.run(Trnsport.java:63)
at java.lang.Thread.run(Thread.java:722)
Edit:
FileOutputStream fos = new FileOutputStream("image" + new Date().getTime() + ".jpg");
fos.write(someByteArray);
fos.close();
If your byte array already contains valid JPEG data, you do not need to invoke the JPEG reader or writer -- you can write the bytes to the file using ordinary file I/O.
If the byte array actually contains some format of raw pixel data, you will need to load it into a BufferedImage directly (such as via setRGB) and encode that as a JPEG.
The fact that you're getting an exception trying to decode it implies it is not JPEG data, but raw pixel data. Or, perhaps it is a different type of image altogether, or it has an image at some offset into the array instead of at the start the array, or it is not an image at all.

bytes to Image Conversion in java

I am trying to convert Image to Byte Array and save it in database. Image to Byte Conversion and bytes to Image Conversion using ImageIO Method works completely fine before saving it into database. But when i retrieve bytes from Database ImageIO returns null.
FileInputStream fis = new FileInputStream(picturePath);
BufferedImage image = ImageIO.read(new File(picturePath));
BufferedImage img = Scalr.resize(image, Scalr.Mode.FIT_EXACT, 124, 133, Scalr.OP_ANTIALIAS);
ByteArrayOutputStream ByteStream = new ByteArrayOutputStream();
ImageIO.write(img, "jpg", ByteStream);
ByteStream.flush();
byte[] imageBytes = ByteStream.toByteArray();
ByteStream.close();
PersonImage = imageBytes;
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(PersonImage);
BufferedImage ImageForBox = ImageIO.read((InputStream)byteArrayInputStream);
PersonImageBox.setIcon(new ImageIcon((Image)ImageForBox));
Above code is what I do before saving picture bytes in DB. I resize the Image then convert it into bytes and then convert other way around and show it in JLabel, it works fine. But when I retrieve bytes from database and use the same code i.e.
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(PersonImage); // PersonImage are bytes from dB.
BufferedImage ImageForBox = ImageIO.read((InputStream)byteArrayInputStream);
PersonImageBox.setIcon(new ImageIcon((Image)ImageForBox));
In this case ImageIO returns null. Please help.

Categories

Resources