importjava.awt.BorderLayout;
importjava.awt.Image;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.image.BufferedImage;
importjava.awt.image.ColorModel;
importjava.awt.image.MemoryImageSource;
importjava.awt.image.PixelGrabber;
importjava.io.File;
importjava.io.IOException;
importjava.util.LinkedList;
importjavax.imageio.ImageIO;
importjavax.swing.ImageIcon;
importjavax.swing.JFileChooser;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JMenu;
importjavax.swing.JMenuBar;
importjavax.swing.JMenuItem;
importjavax.swing.JOptionPane;
importjavax.swing.JScrollPane;
publicclassMyShowImageextendsJFrame{
//保存当前操作的像素矩阵
privateintcurrentPixArray[]=null;
//图像的路径
privateStringfileString=null;
//用于显示图像的标签
privateJLabelimageLabel=null;
//加载的图像
privateBufferedImagenewImage;
//图像的高和宽
privateinth,w;
//保存历史操作图像矩阵
privateLinkedList<int[]>imageStack=newLinkedList<int[]>();
privateLinkedList<int[]>tempImageStack=newLinkedList<int[]>();
publicMyShowImage(Stringtitle){
super(title);
this.setSize(800,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//创建菜单
JMenuBarjb=newJMenuBar();
JMenufileMenu=newJMenu("文件");
jb.add(fileMenu);
JMenuItemopenImageMenuItem=newJMenuItem("打开图像");
fileMenu.add(openImageMenuItem);
openImageMenuItem.addActionListener(newOpenListener());
JMenuItemexitMenu=newJMenuItem("退出");
fileMenu.add(exitMenu);
exitMenu.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
System.exit(0);
}
});
JMenuoperateMenu=newJMenu("图像处理");
jb.add(operateMenu);
JMenuItemRGBtoGrayMenuItem=newJMenuItem("灰度图像转换");
operateMenu.add(RGBtoGrayMenuItem);
RGBtoGrayMenuItem.addActionListener(newRGBtoGrayActionListener());
JMenuItembalanceMenuItem=newJMenuItem("均衡化");
operateMenu.add(balanceMenuItem);
balanceMenuItem.addActionListener(newBalanceActionListener());
JMenufrontAndBackMenu=newJMenu("历史操作");
jb.add(frontAndBackMenu);
JMenuItembackMenuItem=newJMenuItem("后退");
frontAndBackMenu.add(backMenuItem);
backMenuItem.addActionListener(newBackActionListener());
JMenuItemfrontMenuItem=newJMenuItem("前进");
frontAndBackMenu.add(frontMenuItem);
frontMenuItem.addActionListener(newFrontActionListener());
this.setJMenuBar(jb);
imageLabel=newJLabel("");
JScrollPanepane=newJScrollPane(imageLabel);
this.add(pane,BorderLayout.CENTER);
this.setVisible(true);
}
privateclassOpenListenerimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
JFileChooserjc=newJFileChooser();
intreturnValue=jc.showOpenDialog(null);
if(returnValue==JFileChooser.APPROVE_OPTION){
FileselectedFile=jc.getSelectedFile();
if(selectedFile!=null){
fileString=selectedFile.getAbsolutePath();
try{
newImage=ImageIO.read(newFile(fileString));
w=newImage.getWidth();
h=newImage.getHeight();
currentPixArray=getPixArray(newImage,w,h);
imageStack.clear();
tempImageStack.clear();
imageStack.addLast(currentPixArray);
imageLabel.setIcon(newImageIcon(newImage));
}catch(IOExceptionex){
System.out.println(ex);
}
}
}
MyShowImage.this.repaint();
//MyShowImage.this.pack();
}
}
//////////////////菜单监听器///////////
privateclassRGBtoGrayActionListenerimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
int[]resultArray=RGBtoGray(currentPixArray);
imageStack.addLast(resultArray);
currentPixArray=resultArray;
showImage(resultArray);
tempImageStack.clear();
}
}
privateclassBalanceActionListenerimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
int[]resultArray=balance(currentPixArray);
imageStack.addLast(resultArray);
currentPixArray=resultArray;
showImage(resultArray);
tempImageStack.clear();
}
}
privateclassBackActionListenerimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
if(imageStack.size()<=1){
JOptionPane.showMessageDialog(null,"此幅图片的处理已经没有后退历史操作了","提示",
JOptionPane.INFORMATION_MESSAGE);
}else{
tempImageStack.addLast(imageStack.removeLast());
currentPixArray=imageStack.getLast();
showImage(currentPixArray);
}
}
}
privateclassFrontActionListenerimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
if(tempImageStack.size()<1){
JOptionPane.showMessageDialog(null,"此幅图片的处理已经没有前进历史操作了","提示",
JOptionPane.INFORMATION_MESSAGE);
}else{
currentPixArray=tempImageStack.removeFirst();
imageStack.addLast(currentPixArray);
showImage(currentPixArray);
}
}
}
//////////////////获取图像像素矩阵/////////
privateint[]getPixArray(Imageim,intw,inth){
int[]pix=newint[w*h];
PixelGrabberpg=null;
try{
pg=newPixelGrabber(im,0,0,w,h,pix,0,w);
if(pg.grabPixels()!=true)
try{
thrownewjava.awt.AWTException("pgerror"+pg.status());
}catch(Exceptioneq){
eq.printStackTrace();
}
}catch(Exceptionex){
ex.printStackTrace();
}
returnpix;
}
//////////////////显示图片///////////
privatevoidshowImage(int[]srcPixArray){
Imagepic=createImage(newMemoryImageSource(w,h,srcPixArray,0,w));
ImageIconic=newImageIcon(pic);
imageLabel.setIcon(ic);
imageLabel.repaint();
}
//////////////////灰度转换///////////
privateint[]RGBtoGray(int[]ImageSource){
int[]grayArray=newint[h*w];
ColorModelcolorModel=ColorModel.getRGBdefault();
inti,j,k,r,g,b;
for(i=0;i<h;i++){
for(j=0;j<w;j++){
k=i*w+j;
r=colorModel.getRed(ImageSource[k]);
g=colorModel.getGreen(ImageSource[k]);
b=colorModel.getBlue(ImageSource[k]);
intgray=(int)(r*0.3+g*0.59+b*0.11);
r=g=b=gray;
grayArray[i*w+j]=(255<<24)|(r<<16)|(g<<8)|b;
}
}
returngrayArray;
}
/////////////////图像均衡化///////////
privateint[]balance(int[]srcPixArray){
int[]histogram=newint[256];
int[]dinPixArray=newint[w*h];
for(inti=0;i<h;i++){
for(intj=0;j<w;j++){
intgrey=srcPixArray[i*w+j]&0xff;
histogram[grey]++;
}
}
doublea=(double)255/(w*h);
double[]c=newdouble[256];
c[0]=(a*histogram[0]);
for(inti=1;i<256;i++){
c[i]=c[i-1]+(int)(a*histogram[i]);
}
for(inti=0;i<h;i++){
for(intj=0;j<w;j++){
intgrey=srcPixArray[i*w+j]&0x0000ff;
inthist=(int)c[grey];
dinPixArray[i*w+j]=255<<24|hist<<16|hist<<8|hist;
}
}
returndinPixArray;
}
publicstaticvoidmain(String[]args){
newMyShowImage("ShowImage");
}
}
importjava.awt.Image;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.image.BufferedImage;
importjava.awt.image.ColorModel;
importjava.awt.image.MemoryImageSource;
importjava.awt.image.PixelGrabber;
importjava.io.File;
importjava.io.IOException;
importjava.util.LinkedList;
importjavax.imageio.ImageIO;
importjavax.swing.ImageIcon;
importjavax.swing.JFileChooser;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JMenu;
importjavax.swing.JMenuBar;
importjavax.swing.JMenuItem;
importjavax.swing.JOptionPane;
importjavax.swing.JScrollPane;
publicclassMyShowImageextendsJFrame{
//保存当前操作的像素矩阵
privateintcurrentPixArray[]=null;
//图像的路径
privateStringfileString=null;
//用于显示图像的标签
privateJLabelimageLabel=null;
//加载的图像
privateBufferedImagenewImage;
//图像的高和宽
privateinth,w;
//保存历史操作图像矩阵
privateLinkedList<int[]>imageStack=newLinkedList<int[]>();
privateLinkedList<int[]>tempImageStack=newLinkedList<int[]>();
publicMyShowImage(Stringtitle){
super(title);
this.setSize(800,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//创建菜单
JMenuBarjb=newJMenuBar();
JMenufileMenu=newJMenu("文件");
jb.add(fileMenu);
JMenuItemopenImageMenuItem=newJMenuItem("打开图像");
fileMenu.add(openImageMenuItem);
openImageMenuItem.addActionListener(newOpenListener());
JMenuItemexitMenu=newJMenuItem("退出");
fileMenu.add(exitMenu);
exitMenu.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
System.exit(0);
}
});
JMenuoperateMenu=newJMenu("图像处理");
jb.add(operateMenu);
JMenuItemRGBtoGrayMenuItem=newJMenuItem("灰度图像转换");
operateMenu.add(RGBtoGrayMenuItem);
RGBtoGrayMenuItem.addActionListener(newRGBtoGrayActionListener());
JMenuItembalanceMenuItem=newJMenuItem("均衡化");
operateMenu.add(balanceMenuItem);
balanceMenuItem.addActionListener(newBalanceActionListener());
JMenufrontAndBackMenu=newJMenu("历史操作");
jb.add(frontAndBackMenu);
JMenuItembackMenuItem=newJMenuItem("后退");
frontAndBackMenu.add(backMenuItem);
backMenuItem.addActionListener(newBackActionListener());
JMenuItemfrontMenuItem=newJMenuItem("前进");
frontAndBackMenu.add(frontMenuItem);
frontMenuItem.addActionListener(newFrontActionListener());
this.setJMenuBar(jb);
imageLabel=newJLabel("");
JScrollPanepane=newJScrollPane(imageLabel);
this.add(pane,BorderLayout.CENTER);
this.setVisible(true);
}
privateclassOpenListenerimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
JFileChooserjc=newJFileChooser();
intreturnValue=jc.showOpenDialog(null);
if(returnValue==JFileChooser.APPROVE_OPTION){
FileselectedFile=jc.getSelectedFile();
if(selectedFile!=null){
fileString=selectedFile.getAbsolutePath();
try{
newImage=ImageIO.read(newFile(fileString));
w=newImage.getWidth();
h=newImage.getHeight();
currentPixArray=getPixArray(newImage,w,h);
imageStack.clear();
tempImageStack.clear();
imageStack.addLast(currentPixArray);
imageLabel.setIcon(newImageIcon(newImage));
}catch(IOExceptionex){
System.out.println(ex);
}
}
}
MyShowImage.this.repaint();
//MyShowImage.this.pack();
}
}
//////////////////菜单监听器///////////
privateclassRGBtoGrayActionListenerimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
int[]resultArray=RGBtoGray(currentPixArray);
imageStack.addLast(resultArray);
currentPixArray=resultArray;
showImage(resultArray);
tempImageStack.clear();
}
}
privateclassBalanceActionListenerimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
int[]resultArray=balance(currentPixArray);
imageStack.addLast(resultArray);
currentPixArray=resultArray;
showImage(resultArray);
tempImageStack.clear();
}
}
privateclassBackActionListenerimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
if(imageStack.size()<=1){
JOptionPane.showMessageDialog(null,"此幅图片的处理已经没有后退历史操作了","提示",
JOptionPane.INFORMATION_MESSAGE);
}else{
tempImageStack.addLast(imageStack.removeLast());
currentPixArray=imageStack.getLast();
showImage(currentPixArray);
}
}
}
privateclassFrontActionListenerimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
if(tempImageStack.size()<1){
JOptionPane.showMessageDialog(null,"此幅图片的处理已经没有前进历史操作了","提示",
JOptionPane.INFORMATION_MESSAGE);
}else{
currentPixArray=tempImageStack.removeFirst();
imageStack.addLast(currentPixArray);
showImage(currentPixArray);
}
}
}
//////////////////获取图像像素矩阵/////////
privateint[]getPixArray(Imageim,intw,inth){
int[]pix=newint[w*h];
PixelGrabberpg=null;
try{
pg=newPixelGrabber(im,0,0,w,h,pix,0,w);
if(pg.grabPixels()!=true)
try{
thrownewjava.awt.AWTException("pgerror"+pg.status());
}catch(Exceptioneq){
eq.printStackTrace();
}
}catch(Exceptionex){
ex.printStackTrace();
}
returnpix;
}
//////////////////显示图片///////////
privatevoidshowImage(int[]srcPixArray){
Imagepic=createImage(newMemoryImageSource(w,h,srcPixArray,0,w));
ImageIconic=newImageIcon(pic);
imageLabel.setIcon(ic);
imageLabel.repaint();
}
//////////////////灰度转换///////////
privateint[]RGBtoGray(int[]ImageSource){
int[]grayArray=newint[h*w];
ColorModelcolorModel=ColorModel.getRGBdefault();
inti,j,k,r,g,b;
for(i=0;i<h;i++){
for(j=0;j<w;j++){
k=i*w+j;
r=colorModel.getRed(ImageSource[k]);
g=colorModel.getGreen(ImageSource[k]);
b=colorModel.getBlue(ImageSource[k]);
intgray=(int)(r*0.3+g*0.59+b*0.11);
r=g=b=gray;
grayArray[i*w+j]=(255<<24)|(r<<16)|(g<<8)|b;
}
}
returngrayArray;
}
/////////////////图像均衡化///////////
privateint[]balance(int[]srcPixArray){
int[]histogram=newint[256];
int[]dinPixArray=newint[w*h];
for(inti=0;i<h;i++){
for(intj=0;j<w;j++){
intgrey=srcPixArray[i*w+j]&0xff;
histogram[grey]++;
}
}
doublea=(double)255/(w*h);
double[]c=newdouble[256];
c[0]=(a*histogram[0]);
for(inti=1;i<256;i++){
c[i]=c[i-1]+(int)(a*histogram[i]);
}
for(inti=0;i<h;i++){
for(intj=0;j<w;j++){
intgrey=srcPixArray[i*w+j]&0x0000ff;
inthist=(int)c[grey];
dinPixArray[i*w+j]=255<<24|hist<<16|hist<<8|hist;
}
}
returndinPixArray;
}
publicstaticvoidmain(String[]args){
newMyShowImage("ShowImage");
}
}
#程序暂时只提供图像的灰度转换和直方图的均衡化,有很多地方还需要
完善
#程序能够处理多种格式的图像,如bmp、jpg、jpeg、gif等
转自:http://hi.baidu.com/xianle/blog/item/6366a3ec30429d2463d09f6b.html