Simple Java image processing program __java

Source: Internet
Author: User
Import Java.awt.BorderLayout;
Import Java.awt.Image;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.awt.image.BufferedImage;
Import Java.awt.image.ColorModel;
Import Java.awt.image.MemoryImageSource;
Import Java.awt.image.PixelGrabber;
Import Java.io.File;
Import java.io.IOException;
Import java.util.LinkedList;

Import Javax.imageio.ImageIO;
Import Javax.swing.ImageIcon;
Import Javax.swing.JFileChooser;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import Javax.swing.JMenu;
Import Javax.swing.JMenuBar;
Import Javax.swing.JMenuItem;
Import Javax.swing.JOptionPane;
Import Javax.swing.JScrollPane;

public class Myshowimage extends JFrame {

To save the pixel matrix for the current operation
private int currentpixarray[] = null;

The path of the image
Private String filestring = null;
Label used to display the image
Private JLabel Imagelabel = null;

Loaded images
Private BufferedImage newimage;

The height and width of the image
private int h,w;

Save History operation Image Matrix
private LinkedList < int [] > imagestack = new LinkedList < int [] > ();
private LinkedList < int [] > tempimagestack = new LinkedList < int [] > ();





Public Myshowimage (String title) {
Super (title);
this. SetSize (800, 600);
this. Setdefaultcloseoperation (Jframe.exit_on_close);

Create a Menu
JMenuBar JB = new JMenuBar ();
JMenu filemenu = new JMenu ("file");
Jb.add (Filemenu);

JMenuItem Openimagemenuitem = new JMenuItem ("open image");
Filemenu.add (Openimagemenuitem);
Openimagemenuitem.addactionlistener (New Openlistener ());

JMenuItem exitmenu = new JMenuItem ("Exit");
Filemenu.add (Exitmenu);
Exitmenu.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent e) {
System.exit (0);
}
});

JMenu operatemenu = new JMenu ("image processing");
Jb.add (Operatemenu);

JMenuItem Rgbtograymenuitem = new JMenuItem ("Grayscale image conversion");
Operatemenu.add (Rgbtograymenuitem);
Rgbtograymenuitem.addactionlistener (New Rgbtograyactionlistener ());

JMenuItem Balancemenuitem = new JMenuItem ("balanced");
Operatemenu.add (Balancemenuitem);
Balancemenuitem.addactionlistener (New Balanceactionlistener ());

JMenu frontandbackmenu = new JMenu ("Historical operation");
Jb.add (Frontandbackmenu);

JMenuItem Backmenuitem = new JMenuItem ("Back");
Frontandbackmenu.add (Backmenuitem);
Backmenuitem.addactionlistener (New Backactionlistener ());

JMenuItem Frontmenuitem = new JMenuItem ("forward");
Frontandbackmenu.add (Frontmenuitem);
Frontmenuitem.addactionlistener (New Frontactionlistener ());

this. Setjmenubar (JB);

Imagelabel = new JLabel ("");
JScrollPane pane = new JScrollPane (Imagelabel);
this. Add (Pane,borderlayout.center);

this. setvisible (true);

}

Private class Openlistener implements actionlistener{
public void actionperformed (ActionEvent e) {
JFileChooser JC = new JFileChooser ();
int returnvalue = Jc.showopendialog (null);
if (returnvalue = = jfilechooser.approve_option) {
File selectedfile = Jc.getselectedfile ();
if (selectedfile!= null) {
filestring = Selectedfile.getabsolutepath ();
try {
NewImage = Imageio.read (new File (filestring));
W = newimage.getwidth ();
h = newimage.getheight ();
Currentpixarray = Getpixarray (newimage,w,h);
Imagestack.clear ();
Tempimagestack.clear ();
Imagestack.addlast (Currentpixarray);
Imagelabel.seticon (New ImageIcon (newimage));

catch (IOException ex) {
System.out.println (ex);
}

}
}
Myshowimage. this. repaint ();
MyShowImage.this.pack ();
}
}


Menu Listener///////////
Private class Rgbtograyactionlistener implements actionlistener{

public void actionperformed (ActionEvent e) {
int [] Resultarray = Rgbtogray (Currentpixarray);
Imagestack.addlast (resultarray);
Currentpixarray = Resultarray;
ShowImage (resultarray);
Tempimagestack.clear ();
}

}

Private class Balanceactionlistener implements actionlistener{

public void actionperformed (ActionEvent e) {
int [] Resultarray = balance (Currentpixarray);
Imagestack.addlast (resultarray);
Currentpixarray = Resultarray;
ShowImage (resultarray);
Tempimagestack.clear ();
}

}

Private class Backactionlistener implements actionlistener{

public void actionperformed (ActionEvent e) {
if (Imagestack.size () <= 1) {
Joptionpane.showmessagedialog (NULL, "The processing of this picture has no back history operation", "hint",
Joptionpane.information_message);
} else {
Tempimagestack.addlast (Imagestack.removelast ());
Currentpixarray = Imagestack.getlast ();
ShowImage (Currentpixarray);
}
}

}

Private class Frontactionlistener implements actionlistener{

public void actionperformed (ActionEvent e) {
if (Tempimagestack.size () < 1) {
Joptionpane.showmessagedialog (NULL, "The processing of this picture has no advance history operation", "hint",
Joptionpane.information_message);
} else {
Currentpixarray = Tempimagestack.removefirst ();
Imagestack.addlast (Currentpixarray);
ShowImage (Currentpixarray);
}
}

}


Get Image pixel matrix/////////
private int []getpixarray (Image im, int w, int h) {
int [] pix = new int [w * h];
PixelGrabber pg = NULL;
try {
pg = new PixelGrabber (IM, 0, 0, W, h, Pix, 0, W);
if (Pg.grabpixels ()!= true)
try {
throw new Java.awt.AWTException ("PG Error" + pg.status ());
catch (Exception eq) {
Eq.printstacktrace ();
}
catch (Exception ex) {
Ex.printstacktrace ();

}
return pix;
}



Show Picture///////////
private void ShowImage (int [] srcpixarray) {
Image pic = createimage (new MemoryImageSource (W,h,srcpixarray, 0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.