-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hello!
I always got the following error, when dislaying image-data: "Unsupported image format: jpeg; its width and height are assumed to zero"
I have a upload-dialog which does the follwing:
private void prepareUpload(){
Media[] images = event.getMedias();
for (Media med : images) {
if (med instanceof Image) {
....
picture.setData(med.getByteData()) // in picture-class, data is a byte[]
selectedFiles.add(picture); // local list for storing all selected pictures
handleUpload();
}
}
private void handleUpload(){
// for every picture-object in the list...
ByteArrayInputStream in = new ByteArrayInputStream(picture.getData());
try {
BufferedImage img = ImageIO.read(in);
// Scaling thumbail
int[] newDimensions = getNewDimensions(img.getWidth(), img.getHeight()); // calculating Dimensions of Thumbnail
java.awt.Image tempImg = (java.awt.Image) img. getScaledInstance(newDimensions[0], newDimensions[1],
java.awt.Image.SCALE_SMOOTH);
BufferedImage imageBuff = new BufferedImage(newDimensions[0], newDimensions[1], BufferedImage.TYPE_INT_RGB);
imageBuff.getGraphics().drawImage(tempImg, 0, 0, new Color(0, 0, 0), null);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ImageIO.write(imageBuff, "jpg", buffer);
AImage aimg = new AImage(picture.getFileName(), buffer.toByteArray());
picture.setThumbnail(aimg.getByteData());
}
Now using a "saveService" for storing the pictures to database.
When i put the picture in another list, which is binded to a listbox and do a "notifyChange", the thumbnails will be displayed, but when i reload the data from database, the error above is raising. Has anyone an idea, what i'm doing wrong?
Thanks in advance!!
If the error only happened when you restore image from database then there some two possibilities:
A. It is stored in wrong way, the data that stored to database is corrupted, or the image is not converted to the data correctly.
To verify this case you can perform a restore action with the data to see if you can get a correct image before you store the data into database.
B. The data is converted correctly, but has problem to store it into database.
You can verify this case by print the data before it is stored into database, then use sql command to get it from database after it is stored and see if you can get exactly the same value.
C. The data is converted correctly, and also stored to database nicely, but has problem to reuse it.
For this case, you can print the data once before you store it into database, then load it from database and print it again programmatically and see if you can get exactly the same value.
To convert between String and Image, you can also refer to an article at my blog: JAVA: Convert Image to Base64 String and Base64 String to Image
Asked: 2014-01-16 07:19:09 +0800
Seen: 21 times
Last updated: Jan 21 '14