Link to home
Start Free TrialLog in
Avatar of Fuzzy_VB
Fuzzy_VB

asked on

converting and resizing a Gif image

hi, i want to convert a gif image to a jpg and then resize it to thumbnail size.
i tried using the ImageIO to read a gif and write a jpg but the jpg ends up being a broken image.
i dont mind even saving it as a gif.

but how do i resize it?

i am using the ACME GifEncoder to write the image

working code:

BufferedImage input = ImageIO.read(new File("c:/test.gif"));
FileOutputStream fos = new FileOutputStream(new File("c:/test2.gif"));
GifEncoder encoder = new GifEncoder(input,fos);
encoder.encode();

main prize goes to anyone who can show me how to resize and save it as a jpg. but even gif will do.
thanks.

-Fuzz
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Try

BufferedImage input = ImageIO.read(new File("c:/test.gif"));
FileOutputStream fos = new FileOutputStream(new File("c:/test2.gif"));
ImageIO.write(input, "jpg", new File("c:/test2.jpg"));
Sorry - line 2 above is redundant
Avatar of Fuzzy_VB
Fuzzy_VB

ASKER

thanks... but the main question is how to resize it before saving it in any format.
Thanks
Oh OK, sorry - i thought you didn't have the to-jpg working at all.

See the following source. In your case, you can dispense with the jpeg encoding classes, as you're using ImageIO

http://www.geocities.com/marcoschmidt.geo/java-save-jpeg-thumbnail.html
thanks for your help CEHJ

i have tried saving it as a jpg the same way you showed me.. and i have seen that link before.. if you can tell me why my jpg ends up as a broken picture i will award you the points.

no exceptions are thrown at all, and the file is created but the picture does not work.

-Fuzz :)
If you can post a compilabe example for me to test, i'll have a look
here is the convert method and what i imported.
----------------------------------------------
import javax.imageio.*;
import java.awt.image.BufferedImage;
import java.io.*;

public void convert(){
   try{
      File inputFile = new File("c:/test.gif");
      BufferedImage input = ImageIO.read(inputFile);
                ImageIO.write(input, "jpg", new File("c:/test2.jpg"));            
   }catch(Exception e){
      System.out.println(e);
   }
}
-------------------------------------------------
-Fuzz
...but that's without the thumbnail stuff of course..?
yes... i think first things first :)
i cant resize the jpg if a jpg doesnt work.. if it works, i will then use the thumbnail example you posted.

-Fuzz
It's a pity to say, but i've found the images libraries are simply not very good, so this doesn't surprise me.

Have you tried the original code at that link?
yes.. and that code works like a charm.
that if i test it with an existing jpg.

but converting a gif to a jpg is the problem.

i take it you tried my code and it never worked for you?

-Fuzz
>>i take it you tried my code and it never worked for you?

I haven't as if it doesn't work satisfactorily for you, it's not going to for me ;-)

>>but converting a gif to a jpg is the problem

meaning if you use the same code, but start with the gif it then doesn't work?
thats correct.
the thumnail code works.

but
ImageIO.write(input, "jpg", new File("c:/test2.jpg"));  
does not... if i convert it to a "png" it works... but i need a gif or a jpg.
and i need to resize it.

if it is possible to even keep it as a gif thats fine, but how do i resize it.
-Fuzz
>>but
ImageIO.write(input, "jpg", new File("c:/test2.jpg"));  
does not...

Well my last suggestion was as follows

a. Read gif using ImageIO
b. Write jpg using JPEGEncoder (i.e. *not* ImageIO)

Did you try that?
>>if it is possible to even keep it as a gif thats fine, but how do i resize it.

And that you could do using the 'thumbnail part' of the code at that link.
Where are we at on this one now Fuzzy_VB?
im trying out the JPEGEncoder here..

http://forum.java.sun.com/thread.jsp?thread=263816&forum=20&message=998474

i havnt forgotten ;)
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok... everything is working

here is the very untidy example for those who need it

try{
            File inputFile = new File("c:/test.gif");
            Image img = ImageIO.read(inputFile);
            BufferedImage bImg = new BufferedImage(img.getWidth(null),img.getHeight(null),BufferedImage.TYPE_INT_RGB);
            Graphics g;
            FileOutputStream jpegOut;
            jpegOut = new FileOutputStream("c:/test.jpg");
            g = bImg.createGraphics();
            g.drawImage(img, 0, 0, null);
            g.dispose();            
            JPEGImageEncoder encoder1 =
            JPEGCodec.createJPEGEncoder(jpegOut);
            JPEGEncodeParam param1 =
            encoder1.getDefaultJPEGEncodeParam(bImg);
            param1.setQuality(0.75f, true);
            encoder1.encode(bImg, param1);
            jpegOut.close();
            // this is where the resizing is done
                String args[] = {"c:/test.jpg","c:/test2.jpg","80","80","80"};
                Image image = Toolkit.getDefaultToolkit().getImage(args[0]);
                MediaTracker mediaTracker = new MediaTracker(new Container());
                mediaTracker.addImage(image, 0);
                mediaTracker.waitForID(0);
                // determine thumbnail size from WIDTH and HEIGHT
                int thumbWidth = Integer.parseInt(args[2]);
                int thumbHeight = Integer.parseInt(args[3]);
                double thumbRatio = (double)thumbWidth / (double)thumbHeight;
                int imageWidth = image.getWidth(null);
                int imageHeight = image.getHeight(null);
                double imageRatio = (double)imageWidth / (double)imageHeight;
                if (thumbRatio < imageRatio) {
                  thumbHeight = (int)(thumbWidth / imageRatio);
                } else {
                  thumbWidth = (int)(thumbHeight * imageRatio);
                }
                // draw original image to thumbnail image object and
                // scale it to the new size on-the-fly
                BufferedImage thumbImage = new BufferedImage(thumbWidth,
                  thumbHeight, BufferedImage.TYPE_INT_RGB);
                Graphics2D graphics2D = thumbImage.createGraphics();
                graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                  RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
                // save thumbnail image to OUTFILE
                BufferedOutputStream out = new BufferedOutputStream(new
                  FileOutputStream(args[1]));
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                JPEGEncodeParam param = encoder.
                  getDefaultJPEGEncodeParam(thumbImage);
                int quality = Integer.parseInt(args[4]);
                quality = Math.max(0, Math.min(quality, 100));
                param.setQuality((float)quality / 100.0f, false);
                encoder.setJPEGEncodeParam(param);
                encoder.encode(thumbImage);
                System.out.println("Done.");
                System.exit(0);

            }
            catch (Exception e)
            {
            e.printStackTrace();
            }

thanks CEHJ
No problem. Pity it doesn't produce satisfactory results with ImageIO :-(

btw, are your *sure* it doesn't if you just use ImageIO.write from here onwards?:

>>// save thumbnail image to OUTFILE