简单的Java图像处理程序

本文介绍了一个简单的图像处理工具,可以进行图像灰度转换和直方图均衡化操作,并提供了图像文件的打开、保存等功能。该工具支持多种图像格式,如bmp、jpg、jpeg、gif等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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");
}

}



#程序暂时只提供图像的灰度转换和直方图的均衡化,有很多地方还需要

完善

#程序能够处理多种格式的图像,如bmp、jpg、jpeg、gif等

转自:http://hi.baidu.com/xianle/blog/item/6366a3ec30429d2463d09f6b.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值