|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectJpgImage
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
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 |
public JpgImage(java.lang.String fileName) throws java.io.IOException, com.sun.image.codec.jpeg.ImageFormatException
fileName
- the name of a JPEG file
java.io.IOException
- if the file cannot be opened or read
com.sun.image.codec.jpeg.ImageFormatException
- if the JPEG file is invalidpublic JpgImage(java.awt.image.BufferedImage image) throws com.sun.image.codec.jpeg.ImageFormatException
image
- a BufferedImage object
com.sun.image.codec.jpeg.ImageFormatException
- if the BufferedImage is nullMethod Detail |
public int getHeight()
public int getWidth()
public void scaleHeight(int height)
height
- scale the image to this heightpublic void scaleWidth(int width)
width
- scale the image to this widthpublic void scaleHeightWidthMax(int height, int width)
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.
height
- scale the image to at least this heightwidth
- scale the image to at least this widthpublic void scaleHeightWidthMin(int height, int width)
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.
height
- scale the image to at most this heightwidth
- scale the image to at most this widthpublic void scalePercent(double scale)
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.
scale
- the amount that this image should be scaled (1 = no change)public void cropProportions(double width, double height)
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).
width
- the proportional width to cropheight
- the proportional height to croppublic void cropProportions(double width, double height, boolean optimize)
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.
width
- the proportional width to cropheight
- the proportional height to cropoptimize
- if true, indicates that the width and height can be
swapped if that would cause less of the image to be
croppedpublic void rotate(double degrees)
JpgImage.rotate(degrees, Color.white);
degrees
- the number of degrees to rotate the imagepublic void rotate(double degrees, java.awt.Color backgroundColor)
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.
degrees
- the number of degrees to rotate the imagebackgroundColor
- the background color used for areas
in the resulting image that are not
covered by the image itselfpublic void invert()
public void grayscale()
public void negative()
public java.awt.image.BufferedImage sendToBufferedImage()
public void sendToFile(java.lang.String fileName) throws java.io.IOException
fileName
- the name of the file to write the image to
(if the file already exists, it will be
overwritten)
java.io.IOException
- if there is an error writing to the filepublic void sendToFile(java.lang.String fileName, float quality) throws java.io.IOException
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
java.io.IOException
- if there is an error writing to the file
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |