I have read clob from oracle procedure and convert them into java.awt.Image object by the following code .
InputStream stream = clob.getAsciiStream();
ByteArrayOutputStream output = new ByteArrayOutputStream();
int a1 = stream.read();
while (a1 >= 0) {
output.write((char) a1);
a1 = stream.read();
}
Image myImage = Toolkit.getDefaultToolkit().createImage(output.toByteArray());
output.close();
Now, I want to save myImage to hard disk. So, What must I do to save these java.awt.Image to file ?
My try :
I have cast the myImage to BufferedImage and then write this to ImageIo by the following code :
BufferedImage bi = (BufferedImage)myImage;
ImageIO.write(bi, "jpg",new File("E:\\out.jpg"));
But I am getting the following exception :
Got Exception as : sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage
java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage
at com.connect.home.Home.getHomeParameter(Home.java:103)
at com.connect.home.Home.main(Home.java:141)
How can I remove this error ? Any advice is of great help .
First
Oracle JDBC drivers support the manipulation of data streams in either
direction between server and client. The drivers support all stream
conversions: binary, ASCII, and Unicode. Following is a brief
description of each type of stream:
Binary. Used for RAW bytes of data, and corresponds to the getBinaryStream method
ASCII. Used for ASCII bytes in ISO-Latin-1 encoding, and corresponds to the getAsciiStream method
Unicode. Used for Unicode bytes with the UTF-16 encoding, and corresponds to the getUnicodeStream method
So probably you will need to use clob.getBinaryStream to retrieve your stream.
Second. You can not save ToolKitImage whith ImageIO::write because it expect a RenderedImage instance.And ToolkitImage not implements this interface.
So you have two options.
Read the stream with ImageIO
RenderedImage image = ImageIO.read(clob.getBinaryStream());
ImageIO.write(image, "JPG",new File("E:\\out.jpg"));
Or draw the ToolkitImage on a RenderedImage
BufferedImage bi = new BufferedImage(myImage.getWidth(null), myImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
bi.getGraphics().drawImage(myImage,0,0, null);
ImageIO.write(bi, "JPG", new File("E:\\out.jpg"));
Related
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")));
}
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.
I see that a number of people have had a similar problem, however I'm yet to try find exactly what I'm looking for.
So, I have a method which reads an input image and converts it to a byte array:
File imgPath = new File(ImageName);
BufferedImage bufferedImage = ImageIO.read(imgPath);
WritableRaster raster = bufferedImage .getRaster();
DataBufferByte data = (DataBufferByte) raster.getDataBuffer();
What I now want to do is convert it back into a BufferedImage (I have an application for which I need this functionality). Note that "test" is the byte array.
BufferedImage img = ImageIO.read(new ByteArrayInputStream(test));
File outputfile = new File("src/image.jpg");
ImageIO.write(img,"jpg",outputfile);
However, this returns the following exception:
Exception in thread "main" java.lang.IllegalArgumentException: im == null!
This is because the BufferedImage img is null. I think this has something to do with the fact that in my original conversion from BufferedImage to byte array, information is changed/lost so that the data can no longer be recognised as a jpg.
Does anyone have any suggestions on how to solve this? Would be greatly appreciated.
This is recommended to convert to a byte array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, "jpg", baos);
byte[] bytes = baos.toByteArray();
Note that calling close or flush will do nothing, you can see this for yourself by looking at their source/doc:
Closing a ByteArrayOutputStream has no effect.
The flush method of OutputStream does nothing.
Thus use something like this:
ByteArrayOutputStream baos = new ByteArrayOutputStream(THINK_ABOUT_SIZE_HINT);
boolean foundWriter = ImageIO.write(bufferedImage, "jpg", baos);
assert foundWriter; // Not sure about this... with jpg it may work but other formats ?
byte[] bytes = baos.toByteArray();
Here are a few links concerning the size hint:
Java: Memory efficient ByteArrayOutputStream
jpg bits per pixel
Of course always read the source code and docs of the version you are using, do not rely blindly on SO answers.
I am trying to convert byte[] array to buffered image so than i can resize the image..but problem is conversion always turned into null.here is my code..
ByteArrayInputStream bais = new ByteArrayInputStream(user.getUser_image());
//Here user.getUser_image() returns byte[] returned from server..
try {
BufferedImage image = ImageIO.read(bais);
System.out.println("============><================"+image);//Here it prints null
BufferedImage scaledImage = Scalr.resize(image,48);
}
.....and so on
It means that the ImageIO class is not able to select an appropriate ImageReader. The purpose of this could be corrupted byte array or unsupported image type. Try to debug it.
I need to add a CMYK Image (java.awt.BufferedImage) to a Pdf-Document with iText.
I'm trying to do it with:
com.lowagie.text.Image img = Image.getInstance(BufferedImage, bgColor);
This produces an RGB image in the resulting PDF. (and I suppose it's a bug, because it just ignores ColorModel). However I could use:
com.lowagie.text.Image img = Image.getInstance(byte[] rawData);
And it produces a correct CMYK-Image in PDF. But for the second case I need to convert java.awt.BufferedImage in ByteArray. I cannot do it with ImageIO.write(ByteArrayOutputStream). I also cannot do it with com.sun.image.codec.jpeg.JPEGImageEncoder because I must use OpenJDK.
Any ideas how can I achieve the correct behavior to write a CMYK image in PDF using iText?
So basically what you're asking is how to convert a BufferedImage to a byte[] to print to PDF?
BufferedImage img; // your image to be printed
String formatName; // name of the image format (see ImageIO docs)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( img, formatName, baos);
byte[] rawData = baos.toByteArray();
You should be able to use that for the CMYK-image as you had in your original post:
com.lowagie.text.Image img = Image.getInstance(byte[] rawData);