服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

a java advanced imaging program


  // this program has two parts , it can invert a image, and compare with the original one in a frame.you need download and install java advanced imaging before you use it!!!

// compile:
javac "###1&2.java"
//run:
java "###2" "image file name.jpg" <return>


// now, here is the code:

//part one:

import java.awt.gridlayout;
import java.awt.event.adjustmentevent;
import java.awt.event.adjustmentlistener;
import java.awt.image.renderedimage;

import javax.media.jai.planarimage;
import javax.swing.jpanel;
import javax.swing.jscrollpane;

/**
* this class represents a panel which contains two instances of
* displayjaiwithpixelinfo.
* the scrolling bars of both images are synchronized so scrolling one image
* will automatically scroll the other.
*/
public class displaytwosynchronizedimages extends jpanel
implements adjustmentlistener
{
/** the displayjaiwithpixelinfo for the first image. */
protected displayjaiwithpixelinfo dj1;
/** the displayjaiwithpixelinfo for the second image. */
protected displayjaiwithpixelinfo dj2;
/** the jscrollpane which will contain the first of the images */
protected jscrollpane jsp1;
/** the jscrollpane which will contain the second of the images */
protected jscrollpane jsp2;

/**
* constructs an instance of this class, setting the components? layout,
* creating two instances of displayjai for the two images and creating/
* registering event handlers for the scroll bars.
* @param im1 the first image (left side)
* @param im2 the second image (right side)
*/
public displaytwosynchronizedimages(planarimage im1,planarimage im2)
{
super();
setlayout(new gridlayout(1,2));
dj1 = new displayjaiwithpixelinfo(im1); // instances of displayjai for the
dj2 = new displayjaiwithpixelinfo(im2); // two images
jsp1 = new jscrollpane(dj1); // jscrollpanes for the both
jsp2 = new jscrollpane(dj2); // instances of displayjai
add(jsp1);
add(jsp2);
// retrieve the scroll bars of the images and registers adjustment
// listeners to them.
// horizontal scroll bar of the first image.
jsp1.gethorizontalscrollbar().addadjustmentlistener(this);
// vertical scroll bar of the first image.
jsp1.getverticalscrollbar().addadjustmentlistener(this);
// horizontal scroll bar of the second image.
jsp2.gethorizontalscrollbar().addadjustmentlistener(this);
// vertical scroll bar of the second image.
jsp2.getverticalscrollbar().addadjustmentlistener(this);
}

/**
* this method changes the first image to be displayed.
* @param newimage the new first image.
*/
public void setimage1(planarimage newimage)
{
dj1.set(newimage);
repaint();
}

/**
* this method changes the second image to be displayed.
* @param newimage the new second image.
*/
public void setimage2(planarimage newimage)
{
dj2.set(newimage);
repaint();
}

/**
* this method returns the first image.
* @return the first image.
*/
public renderedimage getimage1()
{
return dj1.getsource();
}

/**
* this method returns the second image.
* @return the second image.
*/
public renderedimage getimage2()
{
return dj2.getsource();
}

/**
* this method returns the first displayjai component.
* @return the first displayjai component.
*/
public displayjaiwithpixelinfo getdisplayjaicomponent1()
{
return dj1;
}

/**
* this method returns the second displayjai component.
* @return the second displayjai component.
*/
public displayjaiwithpixelinfo getdisplayjaicomponent2()
{
return dj2;
}

/**
* this method will be called when any of the scroll bars of the instances of
* displayjai are changed. the method will adjust the scroll bar of the other
* displayjai as needed.
* @param e the adjustmentevent that ocurred (meaning that one of the scroll
* bars position has changed.
*/
public void adjustmentvaluechanged(adjustmentevent e)
{
// if the horizontal bar of the first image was changed...
if (e.getsource() == jsp1.gethorizontalscrollbar())
{
// we change the position of the horizontal bar of the second image.
jsp2.gethorizontalscrollbar().setvalue(e.getvalue());
}
// if the vertical bar of the first image was changed...
if (e.getsource() == jsp1.getverticalscrollbar())
{
// we change the position of the vertical bar of the second image.
jsp2.getverticalscrollbar().setvalue(e.getvalue());
}
// if the horizontal bar of the second image was changed...
if (e.getsource() == jsp2.gethorizontalscrollbar())
{
// we change the position of the horizontal bar of the first image.
jsp1.gethorizontalscrollbar().setvalue(e.getvalue());
}
// if the vertical bar of the second image was changed...
if (e.getsource() == jsp2.getverticalscrollbar())
{
// we change the position of the vertical bar of the first image.
jsp1.getverticalscrollbar().setvalue(e.getvalue());
}
}

}




//ok, now is the main function , part two:

import javax.media.jai.jai;
import javax.media.jai.planarimage;
import javax.swing.jframe;


/**
* this class demonstrates the use of the invert operator.
*/
public class invert
{
/**
* the application entry point.
* @param args the command line arguments.
*/
public static void main(string[] args)
{
// we need one argument: the image filename.
if (args.length != 1)
{
system.err.println("usage: java operators.invert image");
system.exit(0);
}
// read the image.
planarimage input = jai.create("fileload", args[0]);
// invert the image.
planarimage output = jai.create("invert", input);
// create a jframe for displaying the results.
jframe frame = new jframe();
frame.settitle("invert image "+args[0]);
// add to the jframe?s contentpane an instance of displaytwosynchronized-
// images, which will contain the original and processed image.
frame.getcontentpane().add(new displaytwosynchronizedimages(input,output));
// set the closing operation so the application is finished.
frame.setdefaultcloseoperation(jframe.exit_on_close);
frame.pack(); // adjust the frame size using preferred dimensions.
frame.setvisible(true); // show the frame.
}

}

扫描关注微信公众号