Help Resizing PNG's and GIF's

H

HugeBob

Good Morning All,

I'm a CF developer and I've been tasked to come up with a way to
upload images (JPG, PNG and GIF) and resize them while maintaining the
original aspect ratio for the resized image. I was able to find and
modify some sample code that handled the resizing for JPEG's. But, I
have to do this for the other formats as well. Here's the sample code
snippet for JPEG's:


import com.sun.image.codec.jpeg.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;

public class Thumbnail {
..
..
..

public myResizeMethod(String inputFile, String outputFile, int
thumbWidth, int thumbHeight, int qualitySpec) throws Exception {
Image image = Toolkit.getDefaultToolkit().getImage(inputFile);
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);

double thumbRatio = (double)thumbWidth / (double)thumbHeight;
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
double imageRatio = (double)imageWidth / (double)imageHeight;
if (thumbRatio < imageRatio) {
thumbHeight = (int)(thumbWidth / imageRatio);
} else {
thumbWidth = (int)(thumbHeight * imageRatio);
}

// draw original image to thumbnail image object and
// scale it to the new size on-the-fly
BufferedImage thumbImage = new BufferedImage(thumbWidth,
thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,

RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);

// save thumbnail image to OUTFILE
BufferedOutputStream out = new BufferedOutputStream(new
FileOutputStream(outputFile));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param =
encoder.getDefaultJPEGEncodeParam(thumbImage);

int quality = Math.max(0, Math.min(qualitySpec, 100));
param.setQuality((float)quality / 100.0f, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(thumbImage);
out.close();
}
}

Now, not knowing much about the internal workings of images, can I use
a similar algorithm for PNG's and GIF's? I was hoping there was a
similar set of encoders/decoder classes for them. But, I haven't been
able to find any.
 
K

Knute Johnson

HugeBob said:
Good Morning All,

I'm a CF developer and I've been tasked to come up with a way to
upload images (JPG, PNG and GIF) and resize them while maintaining the
original aspect ratio for the resized image. I was able to find and
modify some sample code that handled the resizing for JPEG's. But, I
have to do this for the other formats as well. Here's the sample code
snippet for JPEG's:


import com.sun.image.codec.jpeg.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;

public class Thumbnail {
.
.
.

public myResizeMethod(String inputFile, String outputFile, int
thumbWidth, int thumbHeight, int qualitySpec) throws Exception {
Image image = Toolkit.getDefaultToolkit().getImage(inputFile);
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);

double thumbRatio = (double)thumbWidth / (double)thumbHeight;
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
double imageRatio = (double)imageWidth / (double)imageHeight;
if (thumbRatio < imageRatio) {
thumbHeight = (int)(thumbWidth / imageRatio);
} else {
thumbWidth = (int)(thumbHeight * imageRatio);
}

// draw original image to thumbnail image object and
// scale it to the new size on-the-fly
BufferedImage thumbImage = new BufferedImage(thumbWidth,
thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,

RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);

// save thumbnail image to OUTFILE
BufferedOutputStream out = new BufferedOutputStream(new
FileOutputStream(outputFile));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param =
encoder.getDefaultJPEGEncodeParam(thumbImage);

int quality = Math.max(0, Math.min(qualitySpec, 100));
param.setQuality((float)quality / 100.0f, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(thumbImage);
out.close();
}
}

Now, not knowing much about the internal workings of images, can I use
a similar algorithm for PNG's and GIF's? I was hoping there was a
similar set of encoders/decoder classes for them. But, I haven't been
able to find any.

Huge:

You need to look at the ImageIO classes. They can read/write JPEG, GIF,
PNG and BMP format files. If you don't need to change the compression
ratio on your JPEGs then all the better. Using Image and reading them
with Toolkit.getImage() and blocking on MediaTracker has long since been
replaced by the ImageIO classes.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top