Class JpgImage

java.lang.Object
  extended byJpgImage

public class JpgImage
extends java.lang.Object

The JpgImage class can be used to resize JPEG images and either return them as either BufferedImages or save them as files. Here's a short example:

 JpgImage ji = new JpgImage("picture.jpg");
 ji.scalePercent(0.5);
 ji.cropProportions(5, 7, true);
 ji.sendToFile("new_picture.jpg");
 

Some of the code used in this class was a modification of the excellent example code provided by Will Bracken in the Java Developer's Forum, at:
http://forum.java.sun.com/thread.jsp?thread=260711&forum=20&message=985157

The "Java Examples in a Nutshell" book by David Flanagan was also a good reference. There are some nice examples of using transform matrices to blur and sharpen an image in there, if you want to add that functionality. While most of the methods here are fairly trivial wrappers around various transform objects, the rotate method is a bit more complicated, due to the fact that the image dimensions and origin are changed -- see the code itself for more details.

The Java 2D classes are used for the image manipulation, so this will only work with Java 1.2 or higher. Also, the com.sun.image.codec.jpeg.JPEGImageDecoder, com.sun.image.codec.jpeg.JPEGEncodeParam, and com.sun.image.codec.jpeg.JPEGCodec classes are used to read and save images, so this may not work with non-Sun implementations of Java. You may be able to use the more generic Image and/or ImageIcon classes to perform similar functions.

Program version 1.0. Author Julian Robichaux, http://www.nsftools.com

Version:
1.0
Author:
Julian Robichaux ( http://www.nsftools.com )

Constructor Summary
JpgImage(java.awt.image.BufferedImage image)
          Creates a JpgImage from the specified BufferedImage
JpgImage(java.lang.String fileName)
          Creates a JpgImage from a specified file name
 
Method Summary
 void cropProportions(double width, double height)
          Crops the current JpgImage object using the given proportions.
 void cropProportions(double width, double height, boolean optimize)
          Crops the current JpgImage object using the given proportions, optionally "optimizing" the cropping by swapping the height and width proportions if doing so would crop less of the image.
 int getHeight()
          Returns the height (in pixels) of the current JpgImage object
 int getWidth()
          Returns the width (in pixels) of the current JpgImage object
 void grayscale()
          Makes the current JpgImage object a greyscale image
 void invert()
          Inverts the current JpgImage object
 void negative()
          Makes the current JpgImage object a negative of the original image
 void rotate(double degrees)
          Rotates the current JpgImage object a specified number of degrees, with a default background color of white (also see the notes for the rotate(double, Color) method).
 void rotate(double degrees, java.awt.Color backgroundColor)
          Rotates the current JpgImage object a specified number of degrees.
 void scaleHeight(int height)
          Shrinks or enlarges the current JpgImage object so that the height of the image (in pixels) equals the given height
 void scaleHeightWidthMax(int height, int width)
          Shrinks or enlarges the current JpgImage object so that the size of the image (in pixels) is the greater of the height and width dictated by the parameters.
 void scaleHeightWidthMin(int height, int width)
          Shrinks or enlarges the current JpgImage object so that the size of the image (in pixels) is the lesser of the height and width dictated by the parameters.
 void scalePercent(double scale)
          Shrinks or enlarges the current JpgImage object by the given scale factor, with a scale of 1 being 100% (or no change).
 void scaleWidth(int width)
          Shrinks or enlarges the current JpgImage object so that the width of the image (in pixels) equals the given width
 java.awt.image.BufferedImage sendToBufferedImage()
          Returns the current JpgImage object as a BufferedImage
 void sendToFile(java.lang.String fileName)
          Writes the current JpgImage object to a file, with a quality of 0.75
 void sendToFile(java.lang.String fileName, float quality)
          Writes the current JpgImage object to a file, with the specified quality
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JpgImage

public JpgImage(java.lang.String fileName)
         throws java.io.IOException,
                com.sun.image.codec.jpeg.ImageFormatException
Creates a JpgImage from a specified file name

Parameters:
fileName - the name of a JPEG file
Throws:
java.io.IOException - if the file cannot be opened or read
com.sun.image.codec.jpeg.ImageFormatException - if the JPEG file is invalid

JpgImage

public JpgImage(java.awt.image.BufferedImage image)
         throws com.sun.image.codec.jpeg.ImageFormatException
Creates a JpgImage from the specified BufferedImage

Parameters:
image - a BufferedImage object
Throws:
com.sun.image.codec.jpeg.ImageFormatException - if the BufferedImage is null
Method Detail

getHeight

public int getHeight()
Returns the height (in pixels) of the current JpgImage object

Returns:
the height of the current image

getWidth

public int getWidth()
Returns the width (in pixels) of the current JpgImage object

Returns:
the width of the current image

scaleHeight

public void scaleHeight(int height)
Shrinks or enlarges the current JpgImage object so that the height of the image (in pixels) equals the given height

Parameters:
height - scale the image to this height

scaleWidth

public void scaleWidth(int width)
Shrinks or enlarges the current JpgImage object so that the width of the image (in pixels) equals the given width

Parameters:
width - scale the image to this width

scaleHeightWidthMax

public void scaleHeightWidthMax(int height,
                                int width)
Shrinks or enlarges the current JpgImage object so that the size of the image (in pixels) is the greater of the height and width dictated by the parameters.

For example, if the image has to be enlarged by a factor of 60% in order to be the given height, and it has to be enlarged by a factor of 80% in order to be the given width, then the image will be enlarged by 80% (the greater of the two). Use this method if you need to make sure that an image is at least the given height and width.

Parameters:
height - scale the image to at least this height
width - scale the image to at least this width

scaleHeightWidthMin

public void scaleHeightWidthMin(int height,
                                int width)
Shrinks or enlarges the current JpgImage object so that the size of the image (in pixels) is the lesser of the height and width dictated by the parameters.

For example, if the image has to be enlarged by a factor of 60% in order to be the given height, and it has to be enlarged by a factor of 80% in order to be the given width, then the image will be enlarged by 60% (the lesser of the two). Use this method if you need to make sure that an image is no larger than the given height or width.

Parameters:
height - scale the image to at most this height
width - scale the image to at most this width

scalePercent

public void scalePercent(double scale)
Shrinks or enlarges the current JpgImage object by the given scale factor, with a scale of 1 being 100% (or no change).

For example, if you need to reduce the image to 75% of the current size, you should use a scale of 0.75. If you want to double the size of the image, you should use a scale of 2. If you attempt to scale using a negative number, the image will not be modified.

Parameters:
scale - the amount that this image should be scaled (1 = no change)

cropProportions

public void cropProportions(double width,
                            double height)
Crops the current JpgImage object using the given proportions. The resulting image will be as large as possible, with either the width or the height of the image unchanged, and the other measurement of the image cropped equally on either side.

For example, to crop an image with the proportions of a 5x7 picture, you could pass a width of 5 and a height of 7 (or a width of 7 and a height of 5).

Parameters:
width - the proportional width to crop
height - the proportional height to crop

cropProportions

public void cropProportions(double width,
                            double height,
                            boolean optimize)
Crops the current JpgImage object using the given proportions, optionally "optimizing" the cropping by swapping the height and width proportions if doing so would crop less of the image. The resulting image will be as large as possible, with either the width or the height of the image unchanged, and the other measurement of the image cropped equally on either side.

For example, to crop an image with the proportions of a 5x7 picture, you could pass a width of 5 and a height of 7. In this case, if the "optimize" flag was set and the image was wider than it was tall, this method would automatically crop with proportions of 7x5 instead.

Parameters:
width - the proportional width to crop
height - the proportional height to crop
optimize - if true, indicates that the width and height can be swapped if that would cause less of the image to be cropped

rotate

public void rotate(double degrees)
Rotates the current JpgImage object a specified number of degrees, with a default background color of white (also see the notes for the rotate(double, Color) method). This is the equivalent of calling:

JpgImage.rotate(degrees, Color.white);

Parameters:
degrees - the number of degrees to rotate the image

rotate

public void rotate(double degrees,
                   java.awt.Color backgroundColor)
Rotates the current JpgImage object a specified number of degrees.

You should be aware of 2 things with regard to image rotation. First, the more times you rotate an image, the more the image degrades. So instead of rotating an image 90 degrees and then rotating it again 45 degrees, you should rotate it once at a 135 degree angle.

Second, a rotated image will always have a rectangular border with sides that are vertical and horizontal, and all of the area within this border will become part of the resulting image. Therefore, if you rotate an image at an angle that's not a multiple of 90 degrees, your image will appear to be placed at an angle against a rectangular background of the specified Color. For this reason, an image rotated 45 degrees and then another 45 degrees will not be the same as an image rotated 90 degrees.

Parameters:
degrees - the number of degrees to rotate the image
backgroundColor - the background color used for areas in the resulting image that are not covered by the image itself

invert

public void invert()
Inverts the current JpgImage object


grayscale

public void grayscale()
Makes the current JpgImage object a greyscale image


negative

public void negative()
Makes the current JpgImage object a negative of the original image


sendToBufferedImage

public java.awt.image.BufferedImage sendToBufferedImage()
Returns the current JpgImage object as a BufferedImage

Returns:
a BufferedImage representing the current JpgImage

sendToFile

public void sendToFile(java.lang.String fileName)
                throws java.io.IOException
Writes the current JpgImage object to a file, with a quality of 0.75

Parameters:
fileName - the name of the file to write the image to (if the file already exists, it will be overwritten)
Throws:
java.io.IOException - if there is an error writing to the file

sendToFile

public void sendToFile(java.lang.String fileName,
                       float quality)
                throws java.io.IOException
Writes the current JpgImage object to a file, with the specified quality

Parameters:
fileName - the name of the file to write the image to (if the file already exists, it will be overwritten)
quality - the JPEG quality of the resulting image file, from 0 to 1
Throws:
java.io.IOException - if there is an error writing to the file