Clyde,
I've written a quick image resizing class, ImageResizer, using the
javax.JavaIO class (requires JDK1.4) as you mentioned. The encoding/decoding
is not that complicated, but as you already know it does not support writing
GIFs (since the GIF format is owned by AOL?). It does however support
reading GIFs, but from the little I have tested the code the results of
encoding a read GIF is not that satisfactory. The best would be to use PNG
or JPG with this approach.
You could however have a look at this reference page, "List of Java
libraries to read and write image files":
http://www.geocities.com/marcoschmidt.geo/java-image-coding.html ...for
other alternatives.
The ImageResizer class can be started with the following command:
java ImageResizer <source image file> <destination image file> <x scaling
factor> <y scaling factor>
The javax.JavaIO supports the following formats:
jpg (read/write), png (read/write), gif (read only)
The code:
// Millian Brave [
[email protected]], 2003/10/16
import java.io.File;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import javax.imageio.ImageIO;
public class ImageResizer
{
/**
* Constructor.
* @param srcImgName the source image name
* @param dstImgName the destination image name
*/
public ImageResizer(String srcImgName, String dstImgName,
float scaleX, float scaleY, String imgOutputFormat)
{
BufferedImage srcImg = null, dstImg = null;
try {
srcImg = ImageIO.read(new File(srcImgName));
dstImg = resizeImage(srcImg, scaleX, scaleY);
ImageIO.write(dstImg, imgOutputFormat, new File(dstImgName));
}
catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* Resizes the image according to the given scaling factors.
* @param srcImg the image to be resized
* @return resized image
*/
public BufferedImage resizeImage(BufferedImage srcImg,
float scaleX, float scaleY)
{
// get image dimensions
int srcW = srcImg.getWidth();
int srcH = srcImg.getHeight();
int dstW = (int) (srcW * scaleX);
int dstH = (int) (srcH * scaleY);
// Get data structures
BufferedImage dstImg = new BufferedImage(dstW, dstH, srcImg.getType());
Raster srcRaster = srcImg.getRaster();
WritableRaster dstRaster = dstImg.getRaster();
double[] tmpPix = {0, 0, 0};
// resize image
for (int y=0; y<dstH; y++) {
for (int x=0; x<dstW; x++) {
int xPos = (int) (x * (1/scaleX)); // (find corresponding src x pos)
int yPos = (int) (y * (1/scaleY)); // (find corresponding src y pos)
tmpPix = srcRaster.getPixel(xPos, yPos, tmpPix);
dstRaster.setPixel(x, y, tmpPix);
}
}
return dstImg;
}
/**
* Application starting point.
* @param argv <p>argv[0] --> the source image name</p>
* <p>argv[1] --> the destination image name</p>
* <p>argv[2] --> x scaling factor</p>
* <p>argv[3] --> y scaling factor</p>
*/
public static void main(String[] argv)
{
if (argv.length == 5) {
float scaleX = Float.parseFloat(argv[2]);
float scaleY = Float.parseFloat(argv[3]);
ImageResizer imageResizer = new ImageResizer(argv[0], argv[1],
scaleX, scaleY,
argv[4]);
}
else {
System.err.println("usage: java ImageResizer " +
"<srcImg> <dstImg> <scaleX> <scaleY> <output
format>");
System.exit(1);
}
}
}