关于java图片处理【转】
转一篇关于java图片处理的文章
1 import java.awt.BorderLayout;
2 import java.awt.Image;
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5 import java.awt.image.BufferedImage;
6 import java.awt.image.ColorModel;
7 import java.awt.image.MemoryImageSource;
8 import java.awt.image.PixelGrabber;
9 import java.io.File;
10 import java.io.IOException;
11 import java.util.LinkedList;
12
13 import javax.imageio.ImageIO;
14 import javax.swing.ImageIcon;
15 import javax.swing.JFileChooser;
16 import javax.swing.JFrame;
17 import javax.swing.JLabel;
18 import javax.swing.JMenu;
19 import javax.swing.JMenuBar;
20 import javax.swing.JMenuItem;
21 import javax.swing.JOptionPane;
22 import javax.swing.JScrollPane;
23 import javax.swing.filechooser.FileFilter;
24
25 public class MyShowImage extends JFrame {
26
27 // 保存当前操作的像素矩阵
28 private int currentPixArray[] = null;
29
30 // 图像的路径
31 private String fileString = null;
32
33 // 用于显示图像的标签
34 private JLabel imageLabel = null;
35
36 // 加载的图像
37 private BufferedImage newImage;
38
39 // 图像的高和宽
40 private int h, w;
41
42 // 保存历史操作图像矩阵
43 private LinkedList<int[]> imageStack = new LinkedList<int[]>();
44
45 private LinkedList<int[]> tempImageStack = new LinkedList<int[]>();
46
47 public MyShowImage(String title) {
48 super(title);
49 this.setSize(800, 600);
50 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
51
52 // 创建菜单
53 JMenuBar jb = new JMenuBar();
54 JMenu fileMenu = new JMenu("文件");
55 jb.add(fileMenu);
56
57 JMenuItem openImageMenuItem = new JMenuItem("打开图像");
58 fileMenu.add(openImageMenuItem);
59 openImageMenuItem.addActionListener(new OpenListener());
60
61 JMenuItem exitMenu = new JMenuItem("退出");
62 fileMenu.add(exitMenu);
63 exitMenu.addActionListener(new ActionListener() {
64 public void actionPerformed(ActionEvent e) {
65 System.exit(0);
66 }
67 });
68
69 JMenu operateMenu = new JMenu("图像处理");
70 jb.add(operateMenu);
71
72 JMenuItem RGBtoGrayMenuItem = new JMenuItem("灰度图像转换");
73 operateMenu.add(RGBtoGrayMenuItem);
74 RGBtoGrayMenuItem.addActionListener(new RGBtoGrayActionListener());
75
76 JMenuItem balanceMenuItem = new JMenuItem("均衡化");
77 operateMenu.add(balanceMenuItem);
78 balanceMenuItem.addActionListener(new BalanceActionListener());
79
80 JMenu frontAndBackMenu = new JMenu("历史操作");
81 jb.add(frontAndBackMenu);
82
83 JMenuItem backMenuItem = new JMenuItem("后退");
84 frontAndBackMenu.add(backMenuItem);
85 backMenuItem.addActionListener(new BackActionListener());
86
87 JMenuItem frontMenuItem = new JMenuItem("前进");
88 frontAndBackMenu.add(frontMenuItem);
89 frontMenuItem.addActionListener(new FrontActionListener());
90
91 this.setJMenuBar(jb);
92
93 imageLabel = new JLabel("");
94 JScrollPane pane = new JScrollPane(imageLabel);
95 this.add(pane, BorderLayout.CENTER);
96
97 this.setVisible(true);
98
99 }
100
101 private class OpenListener implements ActionListener {
102 public void actionPerformed(ActionEvent e) {
103 JFileChooser jc = new JFileChooser();
104 jc.setFileFilter(new FileFilter() {
105 public boolean accept(File f) { // 设定可用的文件的后缀名
106 if (f.getName().endsWith(".jpg") || f.isDirectory()|| f.getName().endsWith(".gif") || f.getName().endsWith(".bmp")) {
107 return true;
108 }
109 return false;
110 }
111
112 public String getDescription() {
113 return "图片(*.jpg,*.gif,*bmp)";
114 }
115 });
116 int returnValue = jc.showOpenDialog(null);
117 if (returnValue == JFileChooser.APPROVE_OPTION) {
118 File selectedFile = jc.getSelectedFile();
119 if (selectedFile != null) {
120 fileString = selectedFile.getAbsolutePath();
121 try {
122 newImage = ImageIO.read(new File(fileString));
123 w = newImage.getWidth();
124 h = newImage.getHeight();
125 currentPixArray = getPixArray(newImage, w, h);
126 imageStack.clear();
127 tempImageStack.clear();
128 imageStack.addLast(currentPixArray);
129 imageLabel.setIcon(new ImageIcon(newImage));
130
131 } catch (IOException ex) {
132 System.out.println(ex);
133 }
134
135 }
136 }
137 MyShowImage.this.repaint();
138 // MyShowImage.this.pack();
139 }
140 }
141
142 // ////////////////菜单监听器///////////
143 private class RGBtoGrayActionListener implements ActionListener {
144
145 public void actionPerformed(ActionEvent e) {
146 int[] resultArray = RGBtoGray(currentPixArray);
147 imageStack.addLast(resultArray);
148 currentPixArray = resultArray;
149 showImage(resultArray);
150 tempImageStack.clear();
151 }
152
153 }
154
155 private class BalanceActionListener implements ActionListener {
156
157 public void actionPerformed(ActionEvent e) {
158 int[] resultArray = balance(currentPixArray);
159 imageStack.addLast(resultArray);
160 currentPixArray = resultArray;
161 showImage(resultArray);
162 tempImageStack.clear();
163 }
164
165 }
166
167 private class BackActionListener implements ActionListener {
168
169 public void actionPerformed(ActionEvent e) {
170 if (imageStack.size() <= 1) {
171 JOptionPane.showMessageDialog(null, "此幅图片的处理已经没有后退历史操作了", "提示",
172 JOptionPane.INFORMATION_MESSAGE);
173 } else {
174 tempImageStack.addLast(imageStack.removeLast());
175 currentPixArray = imageStack.getLast();
176 showImage(currentPixArray);
177 }
178 }
179
180 }
181
182 private class FrontActionListener implements ActionListener {
183
184 public void actionPerformed(ActionEvent e) {
185 if (tempImageStack.size() < 1) {
186 JOptionPane.showMessageDialog(null, "此幅图片的处理已经没有前进历史操作了", "提示",
187 JOptionPane.INFORMATION_MESSAGE);
188 } else {
189 currentPixArray = tempImageStack.removeFirst();
190 imageStack.addLast(currentPixArray);
191 showImage(currentPixArray);
192 }
193 }
194
195 }
196
197 // ////////////////获取图像像素矩阵/////////
198 private int[] getPixArray(Image im, int w, int h) {
199 int[] pix = new int[w * h];
200 PixelGrabber pg = null;
201 try {
202 pg = new PixelGrabber(im, 0, 0, w, h, pix, 0, w);
203 if (pg.grabPixels() != true)
204 try {
205 throw new java.awt.AWTException("pg error" + pg.status());
206 } catch (Exception eq) {
207 eq.printStackTrace();
208 }
209 } catch (Exception ex) {
210 ex.printStackTrace();
211
212 }
213 return pix;
214 }
215
216 // ////////////////显示图片///////////
217 private void showImage(int[] srcPixArray) {
218 Image pic = createImage(new MemoryImageSource(w, h, srcPixArray, 0, w));
219 ImageIcon ic = new ImageIcon(pic);
220 imageLabel.setIcon(ic);
221 imageLabel.repaint();
222 }
223
224 // ////////////////灰度转换///////////
225 private int[] RGBtoGray(int[] ImageSource) {
226 int[] grayArray = new int[h * w];
227 ColorModel colorModel = ColorModel.getRGBdefault();
228 int i, j, k, r, g, b;
229 for (i = 0; i < h; i++) {
230 for (j = 0; j < w; j++) {
231 k = i * w + j;
232 r = colorModel.getRed(ImageSource[k]);
233 g = colorModel.getGreen(ImageSource[k]);
234 b = colorModel.getBlue(ImageSource[k]);
235 int gray = (int) (r * 0.3 + g * 0.59 + b * 0.11);
236 r = g = b = gray;
237 grayArray[i * w + j] = (255 << 24) | (r << 16) | (g << 8) | b;
238 }
239 }
240 return grayArray;
241 }
242
243 // ///////////////图像均衡化///////////
244 private int[] balance(int[] srcPixArray) {
245 int[] histogram = new int[256];
246 int[] dinPixArray = new int[w * h];
247
248 for (int i = 0; i < h; i++) {
249 for (int j = 0; j < w; j++) {
250 int grey = srcPixArray[i * w + j] & 0xff;
251 histogram[grey]++;
252 }
253 }
254 double a = (double) 255 / (w * h);
255 double[] c = new double[256];
256 c[0] = (a * histogram[0]);
257 for (int i = 1; i < 256; i++) {
258 c[i] = c[i - 1] + (int) (a * histogram[i]);
259 }
260 for (int i = 0; i < h; i++) {
261 for (int j = 0; j < w; j++) {
262 int grey = srcPixArray[i * w + j] & 0x0000ff;
263 int hist = (int) c[grey];
264
265 dinPixArray[i * w + j] = 255 << 24 | hist << 16 | hist << 8
266 | hist;
267 }
268 }
269 return dinPixArray;
270 }
271
272 public static void main(String[] args) {
273 new MyShowImage("ShowImage");
274 }
275
276 }