Beginners guide to Java Advanced Imaging(JAI)

An attempt to fill the absense of a simple guide to writing Image processing applications using Java with the Java Advanced Imaging(JAI) API and others.

Tuesday, November 07, 2006

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.

Monday, November 06, 2006

Configuring development environment for Java Advanced Imaging API with Eclipse

About me:
Hi! This is Krishna Bhargava Vangapandu(you can call me Krish or Bhargav). I am currently working as a Software Associate at Kanbay Software India(now part of CapGemini). Though I am currently on Mainframes platform working with COBOL,VSAM, CICS and boring-what-not; I always have this free time to pursue my interests which keep changing from time to time. One interest that has been with me since long is Image Processing and I dream of writing everyday applications that makes use of Image Processing techniques to do excellent things. As a part of it, I created this blog to maintain a dairy of what I did when learning the JAI. I intend to post all the code, all the knowledge I get from the internet regarding the JAI here. The main intention behind this blog is to let those programmers interested in Image processing but got no time or patience to search through the internet to collect information to learn the API.

In this post:
In this post, I will provide the preliminary steps to get started writing Java applications that makes use of Java Advanced Imaging API. There is no theory or explanation as of now in this post. Its all about system setup to write JAI applications.

Pre-requisites:
I expect the readers to have little working knowledge of Java, by working knowledge I mean that the reader should know how to install jdk, eclipse IDE. I use Eclipse and so all my further posts will be with respect to Eclipse.

Getting Started (assuming you already have the JDK installed)
Step 1: Download the Eclipse from http://www.eclipse.org (the direct link for eclipse sdk is http://www.eclipse.org/downloads/download.php?file=/eclipse/downloads/drops/R-3.2.1-200609210945/eclipse-SDK-3.2.1-win32.zip )
Step 2: Download the Java Media Framework from the link :
http://javashoplm.sun.com/ECom/docs/Welcome.jsp?StoreId=22&PartDetailId=7372-jmf-2.1.1e-oth-JPR&SiteId=JSC&TransactionId=noreg
Choose the JDK version, as we intend to develop applications with the library. Run the set up and you are done with installation of JMF libraries.
Step 3: Download the JAI Libraries from the following link :
http://java.sun.com/products/java-media/jai/downloads/download-1_1_2_01.html
Run the setup and show your JDK; you are now ready to write simple JAI applications. Before going further, lets install another set of libraries called the JAI Image I/O aPI. Download from :
http://java.sun.com/products/java-media/jai/downloads/download-iio-1_0_01.html
Step 4: Now if you already have eclipse and started an eclipse workspace, you will have the package explorer when you use the Java Perspective (Window->Show Perspective->Java), you can see the Package Explorer with JRE listed as one of the libraries. Expand the JRE System Library in the Package explorer. Make sure you see the jai and jmf related jars listed as shown.
I tried marking a few, but I am not sure those are the only libraries. But those are the libraries that are good enough for us to get started with simple image processing applications.
If you have installed JMF, JAI and do not find the JARs listed in the package explorer, then you need to add them to your project as external JARS (from Project Properties->Add External Jars). But the better way for this is to show the correct JRE on which you have installed the JMF and JAI/Image-IO libraries (remember! it asks during installation to show the JDK, the libraries will then, I think, be installed into %jdk&/jre/lib/ext/). So to use this latest JRE, go to Project -> Properties -> Java Build Path->Libraries then select the JRE system library and then click on Edit. Select alternate JRE and click on "Installed JREs". Now it searches for the JREs and then select the one which has your libraries. After you apply the settings (ofcourse by clicking correct buttons ;) during the process i just gave), Eclipse rebuilds the workspace.
If you did not use Eclipse earlier or creating new Project for your JAI applications, then during creation of project you can repeat the same process and show Eclipse what JRE to use.
Now your development environment is ready to write simple JAI programs. In the next post, I will post a JAI Hello World program and explain some concepts I learn.

Disclaimer: Please forgive me for typos, I dont have time to proof-read my blogs. As far as I know the information I share here is correct, but still I am not responsible for anything that happens to your exisiting settings. You know, I am a developer and we always say "But it works on my system!". Your feedback is welcome at krishnabhargav@yahoo.com, or if you have a similar information to share on this blog, please mail me so that I will post it here.
PLEASE DO NOT COPY THE INFORMATION AND POST AS YOURS! THATS BAD! :D