package sy;
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;
import javax.swing.filechooser.FileFilter;
public class MyShowImage extends JFrame {
???? // 保存當前操作的像素矩陣
???? private int currentPixArray[] = null;
???? // 圖像的路徑
???? private String fileString = null;
???? // 用于顯示圖像的標簽
???? private JLabel imageLabel = null;
???? // 加載的圖像
???? private BufferedImage newImage;
???? // 圖像的高和寬
???? private int h, w;
???? // 保存歷史操作圖像矩陣
???? 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);
???????? // 創建菜單
???????? JMenuBar jb = new JMenuBar();
???????? JMenu fileMenu = new JMenu("文件");
???????? jb.add(fileMenu);
???????? JMenuItem openImageMenuItem = new JMenuItem("打開圖像");
???????? fileMenu.add(openImageMenuItem);
???????? openImageMenuItem.addActionListener(new OpenListener());
???????? JMenuItem exitMenu = new JMenuItem("退出");
???????? fileMenu.add(exitMenu);
???????? exitMenu.addActionListener(new ActionListener() {
???????????? public void actionPerformed(ActionEvent e) {
???????????????? System.exit(0);
???????????? }
???????? });
???????? JMenu operateMenu = new JMenu("圖像處理");
???????? jb.add(operateMenu);
???????? JMenuItem RGBtoGrayMenuItem = new JMenuItem("灰度圖像轉換");
???????? operateMenu.add(RGBtoGrayMenuItem);
???????? RGBtoGrayMenuItem.addActionListener(new RGBtoGrayActionListener());
???????? JMenuItem balanceMenuItem = new JMenuItem("均衡化");
???????? operateMenu.add(balanceMenuItem);
???????? balanceMenuItem.addActionListener(new BalanceActionListener());
???????? JMenu frontAndBackMenu = new JMenu("歷史操作");
???????? jb.add(frontAndBackMenu);
???????? JMenuItem backMenuItem = new JMenuItem("后退");
???????? frontAndBackMenu.add(backMenuItem);
???????? backMenuItem.addActionListener(new BackActionListener());
???????? JMenuItem frontMenuItem = new JMenuItem("前進");
???????? 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();
???????????? jc.setFileFilter(new FileFilter() {
???????????????? public boolean accept(File f) { // 設定可用的文件的后綴名
???????????????????? if (f.getName().endsWith(".jpg") || f.isDirectory()|| f.getName().endsWith(".gif") || f.getName().endsWith(".bmp")) {
???????????????????????? return true;
???????????????????? }
???????????????????? return false;
???????????????? }
???????????????? public String getDescription() {
???????????????????? return "圖片(*.jpg,*.gif,*bmp)";
???????????????? }
???????????? });
???????????? 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();
???????? }
???? }
???? // ////////////////菜單監聽器///////////
???? 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, "此幅圖片的處理已經沒有后退歷史操作了", "提示",
???????????????????????? 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, "此幅圖片的處理已經沒有前進歷史操作了", "提示",
???????????????????????? JOptionPane.INFORMATION_MESSAGE);
???????????? } else {
???????????????? currentPixArray = tempImageStack.removeFirst();
???????????????? imageStack.addLast(currentPixArray);
???????????????? showImage(currentPixArray);
???????????? }
???????? }
???? }
???? // ////////////////獲取圖像像素矩陣/////////
???? 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;
???? }
???? // ////////////////顯示圖片///////////
???? private void showImage(int[] srcPixArray) {
???????? Image pic = createImage(new MemoryImageSource(w, h, srcPixArray, 0, w));
???????? ImageIcon ic = new ImageIcon(pic);
???????? imageLabel.setIcon(ic);
???????? imageLabel.repaint();
???? }
???? // ////////////////灰度轉換///////////
???? private int[] RGBtoGray(int[] ImageSource) {
???????? int[] grayArray = new int[h * w];
???????? ColorModel colorModel = ColorModel.getRGBdefault();
???????? int i, 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]);
???????????????? int gray = (int) (r * 0.3 + g * 0.59 + b * 0.11);
???????????????? r = g = b = gray;
???????????????? grayArray[i * w + j] = (255 << 24) | (r << 16) | (g <<
| b;
???????????? }
???????? }
???????? return grayArray;
???? }
???? // ///////////////圖像均衡化///////////
???? private int[] balance(int[] srcPixArray) {
???????? int[] histogram = new int[256];
???????? int[] dinPixArray = new int[w * h];
???????? for (int i = 0; i < h; i++) {
???????????? for (int j = 0; j < w; j++) {
???????????????? int grey = srcPixArray[i * w + j] & 0xff;
???????????????? histogram[grey]++;
???????????? }
???????? }
???????? double a = (double) 255 / (w * h);
???????? double[] c = new double[256];
???????? c[0] = (a * histogram[0]);
???????? for (int i = 1; i < 256; i++) {
???????????? c[i] = c[i - 1] + (int) (a * histogram[i]);
???????? }
???????? for (int i = 0; i < h; i++) {
???????????? for (int j = 0; j < w; j++) {
???????????????? int grey = srcPixArray[i * w + j] & 0x0000ff;
???????????????? int hist = (int) c[grey];
???????????????? dinPixArray[i * w + j] = 255 << 24 | hist << 16 | hist << 8
???????????????????????? | hist;
???????????? }
???????? }
???????? return dinPixArray;
???? }
???? public static void main(String[] args) {
???????? new MyShowImage("ShowImage");
???? }
}
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;
import javax.swing.filechooser.FileFilter;
public class MyShowImage extends JFrame {
???? // 保存當前操作的像素矩陣
???? private int currentPixArray[] = null;
???? // 圖像的路徑
???? private String fileString = null;
???? // 用于顯示圖像的標簽
???? private JLabel imageLabel = null;
???? // 加載的圖像
???? private BufferedImage newImage;
???? // 圖像的高和寬
???? private int h, w;
???? // 保存歷史操作圖像矩陣
???? 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);
???????? // 創建菜單
???????? JMenuBar jb = new JMenuBar();
???????? JMenu fileMenu = new JMenu("文件");
???????? jb.add(fileMenu);
???????? JMenuItem openImageMenuItem = new JMenuItem("打開圖像");
???????? fileMenu.add(openImageMenuItem);
???????? openImageMenuItem.addActionListener(new OpenListener());
???????? JMenuItem exitMenu = new JMenuItem("退出");
???????? fileMenu.add(exitMenu);
???????? exitMenu.addActionListener(new ActionListener() {
???????????? public void actionPerformed(ActionEvent e) {
???????????????? System.exit(0);
???????????? }
???????? });
???????? JMenu operateMenu = new JMenu("圖像處理");
???????? jb.add(operateMenu);
???????? JMenuItem RGBtoGrayMenuItem = new JMenuItem("灰度圖像轉換");
???????? operateMenu.add(RGBtoGrayMenuItem);
???????? RGBtoGrayMenuItem.addActionListener(new RGBtoGrayActionListener());
???????? JMenuItem balanceMenuItem = new JMenuItem("均衡化");
???????? operateMenu.add(balanceMenuItem);
???????? balanceMenuItem.addActionListener(new BalanceActionListener());
???????? JMenu frontAndBackMenu = new JMenu("歷史操作");
???????? jb.add(frontAndBackMenu);
???????? JMenuItem backMenuItem = new JMenuItem("后退");
???????? frontAndBackMenu.add(backMenuItem);
???????? backMenuItem.addActionListener(new BackActionListener());
???????? JMenuItem frontMenuItem = new JMenuItem("前進");
???????? 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();
???????????? jc.setFileFilter(new FileFilter() {
???????????????? public boolean accept(File f) { // 設定可用的文件的后綴名
???????????????????? if (f.getName().endsWith(".jpg") || f.isDirectory()|| f.getName().endsWith(".gif") || f.getName().endsWith(".bmp")) {
???????????????????????? return true;
???????????????????? }
???????????????????? return false;
???????????????? }
???????????????? public String getDescription() {
???????????????????? return "圖片(*.jpg,*.gif,*bmp)";
???????????????? }
???????????? });
???????????? 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();
???????? }
???? }
???? // ////////////////菜單監聽器///////////
???? 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, "此幅圖片的處理已經沒有后退歷史操作了", "提示",
???????????????????????? 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, "此幅圖片的處理已經沒有前進歷史操作了", "提示",
???????????????????????? JOptionPane.INFORMATION_MESSAGE);
???????????? } else {
???????????????? currentPixArray = tempImageStack.removeFirst();
???????????????? imageStack.addLast(currentPixArray);
???????????????? showImage(currentPixArray);
???????????? }
???????? }
???? }
???? // ////////////////獲取圖像像素矩陣/////////
???? 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;
???? }
???? // ////////////////顯示圖片///////////
???? private void showImage(int[] srcPixArray) {
???????? Image pic = createImage(new MemoryImageSource(w, h, srcPixArray, 0, w));
???????? ImageIcon ic = new ImageIcon(pic);
???????? imageLabel.setIcon(ic);
???????? imageLabel.repaint();
???? }
???? // ////////////////灰度轉換///////////
???? private int[] RGBtoGray(int[] ImageSource) {
???????? int[] grayArray = new int[h * w];
???????? ColorModel colorModel = ColorModel.getRGBdefault();
???????? int i, 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]);
???????????????? int gray = (int) (r * 0.3 + g * 0.59 + b * 0.11);
???????????????? r = g = b = gray;
???????????????? grayArray[i * w + j] = (255 << 24) | (r << 16) | (g <<

???????????? }
???????? }
???????? return grayArray;
???? }
???? // ///////////////圖像均衡化///////////
???? private int[] balance(int[] srcPixArray) {
???????? int[] histogram = new int[256];
???????? int[] dinPixArray = new int[w * h];
???????? for (int i = 0; i < h; i++) {
???????????? for (int j = 0; j < w; j++) {
???????????????? int grey = srcPixArray[i * w + j] & 0xff;
???????????????? histogram[grey]++;
???????????? }
???????? }
???????? double a = (double) 255 / (w * h);
???????? double[] c = new double[256];
???????? c[0] = (a * histogram[0]);
???????? for (int i = 1; i < 256; i++) {
???????????? c[i] = c[i - 1] + (int) (a * histogram[i]);
???????? }
???????? for (int i = 0; i < h; i++) {
???????????? for (int j = 0; j < w; j++) {
???????????????? int grey = srcPixArray[i * w + j] & 0x0000ff;
???????????????? int hist = (int) c[grey];
???????????????? dinPixArray[i * w + j] = 255 << 24 | hist << 16 | hist << 8
???????????????????????? | hist;
???????????? }
???????? }
???????? return dinPixArray;
???? }
???? public static void main(String[] args) {
???????? new MyShowImage("ShowImage");
???? }
}
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
