• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 534
  • Last Modified:

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.
0
askJava
Asked:
askJava
3 Solutions
 
StillUnAwareCommented:
the answer is here: java.awt.Image

Image.getScaledInstance(int width, int height, int hints)
0
 
CEHJCommented:
http://javaalmanac.com/egs/java.awt.image/CreateTxImage.html

shows setting the ratio.

tx.scale(0.5, 0.5); // half size
0
 
objectsCommented:
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);
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
askJavaAuthor Commented:
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);
0
 
askJavaAuthor Commented:
I still didn't get the problem fix but thanks for your help.
0
 
askJavaAuthor Commented:
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now