Basic JPEG image resizing in Java

C

cyranoVR

Hello all,

I wrote this small command-line java ap that demonstrates how to do
basic JPEG image resizing with the JDK and Sun JPEG Codecs.
Comments/criticisms would be greatly appreciated.

/* JpegResizerDemo.java */

import java.io.FileInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.awt.image.AffineTransformOp;
import java.awt.geom.AffineTransform;

import java.util.Map;
import java.util.HashMap;

import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
* @author CyranoVR
*/
public class ImageResizer {


public static void main(String args[]) {

if (args.length != 3) {
System.out.println("Usage: ImageResizer <input> <output>
<scale>");
System.exit(0);
}

String inFile = args[0];
String outFile = args[1];

FileInputStream fs = null;

try {

float scale = Float.parseFloat(args[2]);

fs = new FileInputStream(inFile);
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(fs);
BufferedImage srcImg = decoder.decodeAsBufferedImage();
fs.close();

AffineTransform af =
AffineTransform.getScaleInstance(scale, scale );


Map hints = new HashMap();
hints.put(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
RenderingHints rh = new RenderingHints(hints);

AffineTransformOp transform = new AffineTransformOp(af,rh);

BufferedImage destImg =
transform.createCompatibleDestImage(srcImg, srcImg.getColorModel());
transform.filter(srcImg, destImg);

FileOutputStream out = new FileOutputStream(outFile);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out,
JPEGCodec.getDefaultJPEGEncodeParam(destImg));

encoder.encode(destImg);
out.close();
System.out.println("Saved file " + outFile);

} catch (FileNotFoundException fnfe ) {
System.out.println("File " + inFile + " does not exist!");

} catch (NumberFormatException nfe){
System.out.println("You entered " + args[2] + ". Please
enter a decimal expression.");

} catch (IOException ioe) {
System.out.println("IO Exception: " + ioe.getMessage());

} catch (ImageFormatException ife) {
System.out.println("Image Format Excpetion: Could not
decode " + inFile);

} catch (Exception e) {
System.out.println(e.getMessage());

} finally {
try {
if(fs != null)
fs.close();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
}
 
L

Lucy

cyranoVR said:
Hello all,

I wrote this small command-line java ap that demonstrates how to do
basic JPEG image resizing with the JDK and Sun JPEG Codecs.
Comments/criticisms would be greatly appreciated.

1. You don't seem to have a "quality" setting capability.
JPEGEncodeParam par = encoder.getDefaultJPEGEncodeParam(I);
par.setQuality((float) 0.30, true); // set jpeg quality to 30
percent
encoder.setJPEGEncodeParam(par);
2. What happens to EXIF codes embedded in the jpg by my camera?
/* JpegResizerDemo.java */

import java.io.FileInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.awt.image.AffineTransformOp;
import java.awt.geom.AffineTransform;

import java.util.Map;
import java.util.HashMap;

import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
* @author CyranoVR
*/
public class ImageResizer {


public static void main(String args[]) {

if (args.length != 3) {
System.out.println("Usage: ImageResizer <input> <output>
<scale>");
System.exit(0);
}

String inFile = args[0];
String outFile = args[1];

FileInputStream fs = null;

try {

float scale = Float.parseFloat(args[2]);

fs = new FileInputStream(inFile);
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(fs);
BufferedImage srcImg = decoder.decodeAsBufferedImage();
fs.close();

AffineTransform af =
AffineTransform.getScaleInstance(scale, scale );


Map hints = new HashMap();
hints.put(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
RenderingHints rh = new RenderingHints(hints);

AffineTransformOp transform = new AffineTransformOp(af,rh);

BufferedImage destImg =
transform.createCompatibleDestImage(srcImg, srcImg.getColorModel());
transform.filter(srcImg, destImg);

FileOutputStream out = new FileOutputStream(outFile);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out,
JPEGCodec.getDefaultJPEGEncodeParam(destImg));

encoder.encode(destImg);
out.close();
System.out.println("Saved file " + outFile);

} catch (FileNotFoundException fnfe ) {
System.out.println("File " + inFile + " does not exist!");

} catch (NumberFormatException nfe){
System.out.println("You entered " + args[2] + ". Please
enter a decimal expression.");

} catch (IOException ioe) {
System.out.println("IO Exception: " + ioe.getMessage());

} catch (ImageFormatException ife) {
System.out.println("Image Format Excpetion: Could not
decode " + inFile);

} catch (Exception e) {
System.out.println(e.getMessage());

} finally {
try {
if(fs != null)
fs.close();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
}
 
A

Andrew Thompson

2. What happens to EXIF codes embedded in the jpg by my camera?

What happened to the 'del' key? There was no need to repost
most of those 121 lines.
 
C

cyranoVR

Lucy said:
2. What happens to EXIF codes embedded in the jpg by my camera?

I haven't investigated yet, but I imagine that you lose this
information, as the JPEG image is being converted from jpeg to
BufferedImage and back again.

The above code is meant as a toy app to get someone started. I think a
"production" quality solution (i.e. a java based photo-editing
application) would have to make use of the Java Image APIs, which
provide the ability to edit JPEG images directly.

Links:
http://java.sun.com/j2se/1.4.2/docs/guide/imageio/index.html
http://java.sun.com/j2se/1.4.2/docs/guide/imageio/spec/imageio_guideTOC.fm.html

What I'm hoping is that someone with practical experience with these
APIs can offer some pointers, advice or words of caution. And perhaps
there an alternative Java library available?

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top