0

Scaled AImage from byte[] ?

asked 2014-01-16 07:19:09 +0800

CR13 gravatar image CR13
36 1

updated 2014-01-16 07:21:26 +0800

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!!

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2014-01-21 03:39:07 +0800

benbai gravatar image benbai
2228 6
http://www.zkoss.org

updated 2014-01-21 03:39:34 +0800

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

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2014-01-16 07:19:09 +0800

Seen: 21 times

Last updated: Jan 21 '14

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More