Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

How to resize a JPG image file

Thread view: 
Chris Berg - 31 Dec 2005 04:44 GMT
I need to re-size a .jpg image. This is what I do:

(tDim is the required new Dimension)
 Image newImg =
javax.imageio.ImageIO.read(myFile).getScaledInstance(tDim.width,
tDim.height,Image.SCALE_SMOOTH);
 java.awt.image.BufferedImage bim =
     new java.awt.image.BufferedImage(tDim.width,
tDim.height, java.awt.image.BufferedImage.TYPE_INT_RGB);
 bim.createGraphics().drawImage(newImg, 0, 0, null);
 FileOutputStream fos = new FileOutputStream(ofName);
 javax.imageio.ImageIO.write(bim, "jpg", fos);
 fos.close();

Is this the smartest (fastest, least ressource-using) way to do it?
And is TYPE_INT_RGB the right choice?

Chris
Knute Johnson - 31 Dec 2005 07:11 GMT
> I need to re-size a .jpg image. This is what I do:
>
[quoted text clipped - 14 lines]
>
> Chris

Chris:

Looks pretty good.  I did a similar one that I use with my web cam to
scale the image.

import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

public class ScaleImage2 {
    public static void scale(String srcFile, int destWidth, int destHeight,
     String destFile) throws IOException {
        BufferedImage src = ImageIO.read(new File(srcFile));
        BufferedImage dest =  new BufferedImage(destWidth,destHeight,
         BufferedImage.TYPE_INT_RGB);
        Graphics2D g = dest.createGraphics();
        AffineTransform at = AffineTransform.getScaleInstance(
         (double)destWidth/src.getWidth(),
         (double)destHeight/src.getHeight());
        g.drawRenderedImage(src,at);
        ImageIO.write(dest,"JPG",new File(destFile));
    }

    public static void main(String[] args) {
        if (args.length == 4) {
            try {

scale(args[0],Integer.parseInt(args[1]),Integer.parseInt(args[2]),
                 args[3]);
            } catch (Exception e) {
                System.out.println(e);
            }
        } else
            System.out.println("\nUsage: java -jar ScaleImage2.jar
srcfile " +
             "width height destfile\n");
    }
}

Signature

Knute Johnson
email s/nospam/knute/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.