java图像处理编程_简单的Java图像处理程序

这个Java程序实现了图像处理功能,包括将RGB图像转换为灰度图像和进行图像均衡化。用户可以通过菜单选择打开图像,程序会显示图像并提供灰度转换和均衡化操作。此外,还支持历史操作的后退和前进功能。
摘要由CSDN通过智能技术生成

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;//图像的路径privateString fileString=null;//用于显示图像的标签privateJLabel imageLabel=null;//加载的图像privateBufferedImage newImage;//图像的高和宽privateinth,w;//保存历史操作图像矩阵privateLinkedListimageStack=newLinkedList();privateLinkedListtempImageStack=newLinkedList();publicMyShowImage(String title){super(title);this.setSize(800,600);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//创建菜单JMenuBar jb=newJMenuBar();

JMenu fileMenu=newJMenu("文件");

jb.add(fileMenu);

JMenuItem openImageMenuItem=newJMenuItem("打开图像");

fileMenu.add(openImageMenuItem);

openImageMenuItem.addActionListener(newOpenListener());

JMenuItem exitMenu=newJMenuItem("退出");

fileMenu.add(exitMenu);

exitMenu.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent e){

System.exit(0);

}

});

JMenu operateMenu=newJMenu("图像处理");

jb.add(operateMenu);

JMenuItem RGBtoGrayMenuItem=newJMenuItem("灰度图像转换");

operateMenu.add(RGBtoGrayMenuItem);

RGBtoGrayMenuItem.addActionListener(newRGBtoGrayActionListener());

JMenuItem balanceMenuItem=newJMenuItem("均衡化");

operateMenu.add(balanceMenuItem);

balanceMenuItem.addActionListener(newBalanceActionListener());

JMenu frontAndBackMenu=newJMenu("历史操作");

jb.add(frontAndBackMenu);

JMenuItem backMenuItem=newJMenuItem("后退");

frontAndBackMenu.add(backMenuItem);

backMenuItem.addActionListener(newBackActionListener());

JMenuItem frontMenuItem=newJMenuItem("前进");

frontAndBackMenu.add(frontMenuItem);

frontMenuItem.addActionListener(newFrontActionListener());this.setJMenuBar(jb);

imageLabel=newJLabel("");

JScrollPane pane=newJScrollPane(imageLabel);this.add(pane,BorderLayout.CENTER);this.setVisible(true);

}privateclassOpenListenerimplementsActionListener{publicvoidactionPerformed(ActionEvent e){

JFileChooser jc=newJFileChooser();intreturnValue=jc.showOpenDialog(null);if(returnValue==JFileChooser.APPROVE_OPTION) {

File selectedFile=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(IOException ex){

System.out.println(ex);

}

}

}

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

}//菜单监听器///privateclassRGBtoGrayActionListenerimplementsActionListener{publicvoidactionPerformed(ActionEvent e){int[] resultArray=RGBtoGray(currentPixArray);

imageStack.addLast(resultArray);

currentPixArray=resultArray;

showImage(resultArray);

tempImageStack.clear();

}

}privateclassBalanceActionListenerimplementsActionListener{publicvoidactionPerformed(ActionEvent e){int[] resultArray=balance(currentPixArray);

imageStack.addLast(resultArray);

currentPixArray=resultArray;

showImage(resultArray);

tempImageStack.clear();

}

}privateclassBackActionListenerimplementsActionListener{publicvoidactionPerformed(ActionEvent e){if(imageStack.size()<=1){

JOptionPane.showMessageDialog(null,"此幅图片的处理已经没有后退历史操作了","提示",

JOptionPane.INFORMATION_MESSAGE);

}else{

tempImageStack.addLast(imageStack.removeLast());

currentPixArray=imageStack.getLast();

showImage(currentPixArray);

}

}

}privateclassFrontActionListenerimplementsActionListener{publicvoidactionPerformed(ActionEvent e){if(tempImageStack.size()<1){

JOptionPane.showMessageDialog(null,"此幅图片的处理已经没有前进历史操作了","提示",

JOptionPane.INFORMATION_MESSAGE);

}else{

currentPixArray=tempImageStack.removeFirst();

imageStack.addLast(currentPixArray);

showImage(currentPixArray);

}

}

}//获取图像像素矩阵/privateint[]getPixArray(Image im,intw,inth){int[] pix=newint[w*h];

PixelGrabber pg=null;try{

pg=newPixelGrabber(im,0,0, w, h, pix,0, w);if(pg.grabPixels()!=true)try{thrownewjava.awt.AWTException("pg error"+pg.status());

}catch(Exception eq){

eq.printStackTrace();

}

}catch(Exception ex){

ex.printStackTrace();

}returnpix;

}//显示图片///privatevoidshowImage(int[] srcPixArray){

Image pic=createImage(newMemoryImageSource(w,h,srcPixArray,0,w));

ImageIcon ic=newImageIcon(pic);

imageLabel.setIcon(ic);

imageLabel.repaint();

}//灰度转换///privateint[] RGBtoGray(int[] ImageSource){int[]grayArray=newint[h*w];

ColorModel colorModel=ColorModel.getRGBdefault();inti ,j,k,r,g,b;for(i=0; i

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

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

dinPixArray[i*w+j]=255<<24|hist<<16|hist<<8|hist;

}

}returndinPixArray;

}publicstaticvoidmain(String[] args) {newMyShowImage("ShowImage");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值