Link to home
Start Free TrialLog in
Avatar of askJava
askJava

asked on

convert jpg and gif to thumbnails?

Hi,
Can anyone help me with a java class to convert jpg and gif to thumbnails. The function including  resize the pic ratio. I am thinking if convert to thumbnails might be slow then I can use the resize ratio to resize the pic to certain ratio displaying on the web page.

I don't know how to calculate the resize ratio.

Thanks.
SOLUTION
Avatar of StillUnAware
StillUnAware
Flag of Lithuania 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
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
ASKER CERTIFIED SOLUTION
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
Avatar of askJava
askJava

ASKER

Hi objects,

Sorry for the delay. Had a family emergency. I did the following but got an error message:
Exception in thread "main" java.lang.IllegalArgumentException: Width (0) and hei
ght (0) cannot be <= 0
        at java.awt.image.DirectColorModel.createCompatibleWritableRaster(Unknow
n Source)
        at java.awt.image.BufferedImage.<init>(Unknown Source)
        at test.<init>(test.java:25)
        at test.main(test.java:11)

How u initialize the W?

            int W = 0;
            
            File file=new File(".");
            File thumb = new File(file.getParentFile(),  file.getName()+".thumb");
            Image i = new ImageIcon(file.getCanonicalPath()).getImage();
            int w = i.getWidth(null);
            int h = i.getHeight(null);
            double ratio = (double) W / (double) w;
            AffineTransform scale = AffineTransform.getScaleInstance(ratio, ratio);
            BufferedImage scaled = new BufferedImage(W, (int)(h*ratio), BufferedImage.TYPE_INT_RGB);
            Graphics2D g = scaled.createGraphics();
            g.setTransform(scale);
            g.drawImage(i, 0, 0, null);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(new FileOutputStream(thumb));
            JPEGEncodeParam jpegParams = encoder.getDefaultJPEGEncodeParam(scaled);
            jpegParams.setQuality(1.0f, false);
            encoder.setJPEGEncodeParam(jpegParams);
            encoder.encode(scaled);
Avatar of askJava

ASKER

I still didn't get the problem fix but thanks for your help.
Avatar of askJava

ASKER

Hi object, your answer is right. I own you more 100 points. Next time I will ask another simple question and will assign the 100 points to you. thanks.