Scaling images using JAI
The following is the code which demonstrates using JAI and to scale a given image. It has been taken from the manual and I have made it even simpler to follow. The code follows:
import java.io.IOException;
import javax.media.jai.*;
import javax.media.jai.widget.ScrollingImagePanel;
import com.sun.media.jai.codec.FileSeekableStream;
import java.awt.image.renderable.ParameterBlock;
import java.awt.*;
public class JAISampleProgram
{
public static void main(String args[])
{
FileSeekableStream fi = null;
try
{
/*create a stream on the input file specified.*/
fi = new FileSeekableStream("c:\\buddi.jpg");
}
catch(IOException ex)
{
System.out.println("Error opening the image");
System.exit(0);
}
/**
* Create an operator to decode the image file
*/
RenderedOp image1 = JAI.create("stream",fi);
/*For now, think that RenderedOp class represents our image as an object. We are creating our image from a "stream" fi.*/
Interpolation interp = Interpolation.getInstance(Interpolation.INTERP_BILINEAR);
/* we are using Bilinear Interpolation */
/* we now prepare the parameters that are going to be applied on the image1 */
ParameterBlock params=new ParameterBlock();
params.addSource(image1); // first specify the image
params.add(0.1f); // X - scaling
params.add(0.1f); // Y - scaling factor
params.add(0.0f); // X - translate
params.add(0.0f); //Y- translate
// our second image is created by "scale"-ing based on the params we pass.
RenderedOp image2=JAI.create("scale",params);
//rest of the code is AWT window to display the image we created.
int width = image2.getWidth();
int height = image2.getHeight();
ScrollingImagePanel panel=new ScrollingImagePanel(image2,width,height);
Frame window=new Frame("JAI Sample Program");
window.add(panel);
window.pack();
window.show();
}
}
Now few simple questions arises for novice people like us.
1. What do you mean by interpolation?
2. What has it got to do with scaling of images?
3. What are the different types of interpolation techniques? When is the each type to be used?
All the answers I see on the web are more mathematical and might not sound interesting for us right now. I will try to get some information which makes sense for us developers and post it in the later posts.
0 Comments:
Post a Comment
<< Home