Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Java - How to resize a JPG image file

 
Thread Tools Search this Thread
Old 12-31-2005, 04:44 AM   #1
Default How to resize a JPG image file


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).getScaledInstan ce(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



Chris Berg
  Reply With Quote
Old 12-31-2005, 07:11 AM   #2
Knute Johnson
 
Posts: n/a
Default Re: How to resize a JPG image file
Chris Berg wrote:
> 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).getScaledInstan ce(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
>


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");
}
}

--

Knute Johnson
email s/nospam/knute/


Knute Johnson
  Reply With Quote
Old 07-20-2009, 04:15 PM   #3
james4u
Junior Member
 
Join Date: Jul 2009
Posts: 1
Default Good work
Chris, good work! This is really helpful, especially the library full path helps a lot. Thanks! Cheers.

Quote:
Originally Posted by Chris Berg
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).getScaledInstan ce(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


james4u
james4u is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
SONY DVD RW DW-G120A SOMETIMES FAILS...... atlantic965 DVD Video 0 06-18-2006 10:36 PM
how can we view an image file? Jaff DVD Video 11 11-28-2005 08:34 AM
problems backing up dvds Lawrence Traub DVD Video 11 09-27-2005 07:34 PM
Re: Ripping DVDs. Please answer the attached question. - Question.txt Stan Brown DVD Video 19 02-09-2005 11:19 PM
Burn process failed - help! Log file posted for help troubleshooting Michael Mason DVD Video 1 08-16-2004 09:24 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46