Target Audience: Java Developers
What should you know already? Java, Java Advanced Imaging
As a Java developer I could say that image representation or image processing using Java is cumbersome work when you work with specific image processing algorithm. Sometimes some of the existing software allows the developers to develop user-defined modules, often using the same API used in the software. The developer may be used his/her own modules, but often there is a restriction of cost or licensing.
A royalty-free, portable and flexible solution is the Java Advanced Imaging API (JAI). JAI is a platform-independent extensible image processing framework. JAI provides a set of object-oriented interfaces that supports a simple, high-level programming model which allows images to be manipulated easily in Java applications and applets.
I am assuming that you have access to jdk 1.4 or later with JAI API installed. Use the following methods to scale an image.
Using “scale” Operator
[java]
public PlanarImage loadScaledImage(File image){
float scale=2.0f;
ParameterBlock pb=new ParameterBlock();
pb.addSource(image);
pb.add(scale);
pb.add(scale);
pb.add(0.0f);
pb.add(0.0f);
pb.add(new InterpolationNearest());
PlanarImage scaledImage=JAI.create("scale", pb);
}
[/java]
Without “scale” Operator
[java]
/**
*
* @param file The image file to scale or resize
* @param destWidth Desired scale value of width for preview
* @param destHeight Desired scale value of height for preview
* @return BufferedImage
*/
public BufferedImage loadScaledImage(String file, int destWidth, int destHeight) {
BufferedImage bim = null;
if (file != null) {
PlanarImage pim = JAI.create("fileload", file);
float xScale = (float) destWidth / pim.getWidth();
float yScale = (float) destHeight / pim.getHeight();
RenderedOp op = ScaleDescriptor.create(pim, new Float(xScale), new Float(yScale), new Float(0.0f), new Float(0.0f), null, null);
bim = op.getAsBufferedImage();
}
return bim;
}
[/java]