Java图像处理

要求实现简单的图像处理功能:
     (1)打开一副图像;
     (2)对打开的图像进行简单处理,至少包括两种操作,如均值滤波、直方图均衡化等;
     (3)对处理之后的图像进行保存;

import java.awt.EventQueue;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

/**
 * A program for viewing images.
 * @version 1.22 2017-3-17
 * @author **
 */
public class ImageViewer
{
   public static void main(String[] args)
   {
      EventQueue.invokeLater(new Runnable()
         {
            public void run()
            { 
               JFrame frame = new ImageViewerFrame();
               frame.setTitle("Image Processing");
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               frame.setVisible(true);
            }
         });
   }
}

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.PixelGrabber;
import java.io.*;

import javax.imageio.ImageIO;
import javax.swing.*;

class ImageViewerFrame extends JFrame
{
   private JLabel label;
   private JFileChooser chooser;
   private static final int DEFAULT_WIDTH = 600;
   private static final int DEFAULT_HEIGHT = 480;
   String name = " ";
   Image im, tmp; 
   int i, iw, ih; 
   int[] pixels; 
   boolean flag_load = false;
   boolean flag_grey = false;

public ImageViewerFrame()
   {
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

// use a label to display the images
      label = new JLabel();
      add(label);

// set up the file chooser
      chooser = new JFileChooser();
      chooser.setCurrentDirectory(new File("."));

// set up the menu bar
      JMenuBar menuBar = new JMenuBar();
      setJMenuBar(menuBar);

JMenu menu = new JMenu("File");
      menuBar.add(menu);

JMenuItem openItem = new JMenuItem("Open");
      menu.add(openItem);
      openItem.addActionListener(new ActionListener()
      {
          public void actionPerformed(ActionEvent event)
          {
               // show file chooser dialog
           int result = chooser.showOpenDialog(null);

// if file selected, set it as icon of the label
              if (result == JFileChooser.APPROVE_OPTION)
              {
                  name = chooser.getSelectedFile().getPath();
                  //label.setIcon(new ImageIcon(name));
                 
                  File inputFile = new File(name);
                 
                  if(name != " ")
                   flag_load = true;
                 
               BufferedImage input = null;
    try {
     input = ImageIO.read(inputFile);
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } 
                  iw = input.getWidth(); 
                  ih = input.getHeight(); 
                  pixels = new int[iw*ih]; 
                  im = input; 
                  tmp = input; 
                  flag_load = true; 
                  repaint();

}
          }
  
      });

JMenuItem SaveItem = new JMenuItem("Save");
      menu.add(SaveItem);
      SaveItem.addActionListener(new ActionListener()
      {
         public void actionPerformed(ActionEvent e)
         {
          try { 
                 jSave_ActionPerformed(e); 
             } catch (IOException e1) { 
                 e1.printStackTrace(); 
             }
         }
      });
     
      JMenuItem SaveAsItem = new JMenuItem("Save As");
      menu.add(SaveAsItem);
      SaveAsItem.addActionListener(new ActionListener()
      {
         public void actionPerformed(ActionEvent e)
         {
          try { 
                 jSave_ActionPerformed1(e); 
             } catch (IOException e1) { 
                 e1.printStackTrace(); 
             }
         }
      });
     
      JMenuItem exitItem = new JMenuItem("Exit");
      menu.add(exitItem);
      exitItem.addActionListener(new ActionListener()
      {
          public void actionPerformed(ActionEvent event)
          {
              System.exit(0);
          }
      });
     
      JMenu menu1 = new JMenu("Edit");
      menuBar.add(menu1);
      JMenuItem deal1 = new JMenuItem("均值滤波");
      menu1.add(deal1);
      deal1.addActionListener(new ActionListener()
      {
         public void actionPerformed(ActionEvent e)
         {
   jMean_ActionPerformed(e); 
         }
      });
     
      JMenuItem deal2 = new JMenuItem("直方图均衡化");
      menu1.add(deal2);
      deal2.addActionListener(new ActionListener()
      {
         public void actionPerformed(ActionEvent e)
         {
          try {
    jGrey_ActionPerformed(e);
   } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }
          jRun_ActionPerformed(e);
         }
      });
     
      JMenuItem deal3 = new JMenuItem("黑白");
      menu1.add(deal3);
      deal3.addActionListener(new ActionListener()
      {
         public void actionPerformed(ActionEvent e)
         {
          try {  
                    jGrey_ActionPerformed(e);  
                } catch (IOException e1) {  
                    e1.printStackTrace();  
                }
         }
      });
   }
   public void jMean_ActionPerformed(ActionEvent e) {     
    if(flag_load){ 
        try{ 
            PixelGrabber pg = new PixelGrabber(im,0,0,iw,ih,pixels,0,iw); 
                  pg.grabPixels(); 
              }catch(InterruptedException e3){ 
                e3.printStackTrace(); 
              } 
            BufferedImage grayImage = new BufferedImage(iw, ih,  
                      BufferedImage.TYPE_INT_RGB); 
             
            ColorModel cm = ColorModel.getRGBdefault(); 
             
            for(int i=1;i<ih-1;i++){ 
                for(int j=1;j<iw-1;j++){ 
                     
                    int red1 = cm.getRed(pixels[(i-1)*iw+j-1]); 
                    int red2 = cm.getRed(pixels[(i-1)*iw+j]); 
                    int red3 = cm.getRed(pixels[(i-1)*iw+j+1]); 
                    int red4 = cm.getRed(pixels[i*iw+j-1]); 
                    int red6 = cm.getRed(pixels[i*iw+j+1]); 
                    int red7 = cm.getRed(pixels[(i+1)*iw+j-1]); 
                    int red8 = cm.getRed(pixels[(i+1)*iw+j]); 
                    int red9 = cm.getRed(pixels[(i+1)*iw+j+1]); 
                    int meanRed = (red1+red2+red3+red4+red6+red7+red8+red9)/8; 
                     
                    int green1 = cm.getGreen(pixels[(i-1)*iw+j-1]); 
                    int green2 = cm.getGreen(pixels[(i-1)*iw+j]); 
                    int green3 = cm.getGreen(pixels[(i-1)*iw+j+1]); 
                    int green4 = cm.getGreen(pixels[i*iw+j-1]); 
                    int green6 = cm.getGreen(pixels[i*iw+j+1]);
                    int green7 = cm.getGreen(pixels[(i+1)*iw+j-1]); 
                    int green8 = cm.getGreen(pixels[(i+1)*iw+j]); 
                    int green9 = cm.getGreen(pixels[(i+1)*iw+j+1]); 
                    int meanGreen = (green1+green2+green3+green4+green6+green7+green8+green9)/8; 
                     
                    int blue1 = cm.getBlue(pixels[(i-1)*iw+j-1]); 
                    int blue2 = cm.getBlue(pixels[(i-1)*iw+j]); 
                    int blue3 = cm.getBlue(pixels[(i-1)*iw+j+1]); 
                    int blue4 = cm.getBlue(pixels[i*iw+j-1]); 
                    int blue6 = cm.getBlue(pixels[i*iw+j+1]); 
                    int blue7 = cm.getBlue(pixels[(i+1)*iw+j-1]); 
                 int blue8 = cm.getBlue(pixels[(i+1)*iw+j]); 
                    int blue9 = cm.getBlue(pixels[(i+1)*iw+j+1]); 
                    int meanBlue = (blue1+blue2+blue3+blue4+blue6+blue7+blue8+blue9)/8; 
                     
                    int rgb = 255<<24|meanRed<<16|meanGreen<<8|meanBlue;  
                    grayImage.setRGB(j, i, rgb); 
                }    
            } 
            tmp = grayImage; 
            repaint(); 
         
        }else{ 
            JOptionPane.showMessageDialog(null, "请打开一张图片!","提示:", 
                    JOptionPane.WARNING_MESSAGE); 
            } 
    }
    public void jSave_ActionPerformed(ActionEvent e) throws IOException{
    if(flag_load)
    {

BufferedImage bi = new BufferedImage(tmp.getWidth(null),tmp.getHeight(null), BufferedImage.TYPE_INT_RGB); 
         Graphics g = bi.getGraphics(); 
         g.drawImage(tmp,0, 0,null); 
         g.dispose(); 
             
            File save_path=new File(name+"(1)"+".jpg"); 
            ImageIO.write(bi, "JPG", save_path); 
 
    }
    else
    { 
     JOptionPane.showMessageDialog(null, "请打开一张图片!","提示:",JOptionPane.WARNING_MESSAGE); 
    }
    }
    public void jSave_ActionPerformed1(ActionEvent e) throws IOException{
     if(flag_load)
     {
         int result = chooser.showOpenDialog(null);
         BufferedImage bi = new BufferedImage(tmp.getWidth(null),tmp.getHeight(null), BufferedImage.TYPE_INT_RGB); 
         Graphics g = bi.getGraphics(); 
         g.drawImage(tmp,0, 0,null); 
         g.dispose(); 
           
         String name1 = chooser.getSelectedFile().getPath();
         if (result == JFileChooser.APPROVE_OPTION)
            {
          
          File save_path=new File(name1); 
          ImageIO.write(bi, "JPG", save_path); 
            }
 
     }
     else
     { 
      JOptionPane.showMessageDialog(null, "请打开一张图片!","提示:",JOptionPane.WARNING_MESSAGE); 
     }
    }
   
    public void jRun_ActionPerformed(ActionEvent e){  
        if(flag_load&&flag_grey){  
            try{  
                PixelGrabber pg = new PixelGrabber(tmp,0,0,iw,ih,pixels,0,iw);  
                pg.grabPixels();  
            }catch(InterruptedException e3){  
                e3.printStackTrace();  
        }  
        BufferedImage greyImage = new BufferedImage(iw, ih,   
                    BufferedImage.TYPE_BYTE_GRAY);  
        //获取图像的直方图  
        int[] histogram = new int[256];  
        for(int i=0; i<ih-1; i++){  
            for(int j=0; j<iw-1; j++){  
                int grey = pixels[i*iw+j]&0xff;  
                histogram[grey]++;  
            }  
        }  
        //直方图均衡化  
        double a = (double)255/(iw*ih);  
        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<ih; i++){  
            for(int j=0; j<iw; j++){  
                int grey = pixels[i*iw+j]&0x0000ff;  
                int hist = (int)(c[grey]);  
                pixels[i*iw+j] = 255<<24|hist<<16|hist<<8|hist;  
                greyImage.setRGB(j, i, pixels[i*iw+j]);  
            }  
        }  
        tmp = greyImage;  
        flag_load = true;  
        repaint();  
        }else{     
        }  
    }
   
    public void jGrey_ActionPerformed(ActionEvent e) throws IOException{  
        if(flag_load){  
            File inputFile = new File(name);  
            BufferedImage input = ImageIO.read(inputFile);  
            iw = input.getWidth(this);  
            ih = input.getHeight(this);  
            pixels = new int[iw*ih];  
              
            BufferedImage grayImage = new BufferedImage(iw, ih,   
                    BufferedImage.TYPE_BYTE_GRAY);  
            for(int i=0; i<iw; i++){  
                for(int j=0; j<ih; j++){  
                    int rgb = input.getRGB(i, j);    
                    int grey = (int) (0.3*((rgb&0xff0000 )>>16)+0.59*((rgb&0xff00 )>>8)+0.11*((rgb&0xff)));   
                    rgb = 255<<24|grey<<16|grey<<8|grey;  
                    grayImage.setRGB(i, j, rgb);  
                }  
            }  
            tmp = grayImage;  
            try{  
                PixelGrabber pg = new PixelGrabber(tmp,0,0,iw,ih,pixels,0,iw);  
                pg.grabPixels();  
            }catch(InterruptedException e3){  
                e3.printStackTrace();  
        }  
            flag_grey = true;  
            repaint();  
            } else{  
                JOptionPane.showMessageDialog(null, "请打开一张图片!","提示:",  
                        JOptionPane.WARNING_MESSAGE);  
            }  
    }

public void paint(Graphics g){ 
        if(flag_load){
            g.drawImage(tmp,DEFAULT_WIDTH/8,DEFAULT_HEIGHT/6,this);  
        }else{} 
    }
}

时间: 2024-11-15 15:18:28

Java图像处理的相关文章

Java图像处理最快技术:ImageJ 学习第一篇

ImageJ是世界上最快的纯Java的图像处理程序.它可以过滤一个2048x2048的图像在0.1秒内(*).这是每秒40万像素!ImageJ的扩展通过使用内置的文本编辑器和Java编译器的ImageJ的开发插件.500多插件可用. 数据类型:8位灰度或索引色,16位无符号整数,32位浮点和RGB色彩. 文件格式:读写所有支持的数据类型为TIFF(非压缩)或原始数据.打开和保存GIF,JPEG,BMP,PNG,PGM,FITS和ASCII.打开DICOM.使用URL打开的TIFF.GIF文件.J

java 图像处理小软件(界面+图像处理)

一.界面学习 用java实现一个简易计算器(代码)如下: 1 /*CJSCalculator.java 2014.8.4 by cjs 2 *当点击含有加号的按钮时,则第一排第二个按钮的文本变为加号: 3 *当点击“OK”按钮时,将算出12+2的结果并在第一排最后一个按钮显示: 4 *减号,乘号,除号的功能类似.其中,数字可以自己输入,也可以固定不变. 5 *以上是简单的版本,如果有能力可以设计出更好更完善的计算器. 6 **/ 7 8 import java.awt.*; 9 import j

Java 图像处理框架-Marvin

网上看到,摘录过来的,暂时还没涉足这方面的东西 Marvin 1.4.5 的插件接口支持处理多个图像作为输入,新的插件可通过多个图片来确认背景,新的插件可使用多个图片来合并相同场景. Marvin 是一个Java开发的可扩展的图像处理框架,该框架主要提供以下几方面的功能: 基本图像操作; 从视频中捕获帧; 多线程的图像处理; 通过GUI界面集成插件; 插件性能分析; 通过插件进行功能扩展.

java 图像处理

1 package com.linxi.page; 2 3 import java.awt.AlphaComposite; 4 import java.awt.Color; 5 import java.awt.Font; 6 import java.awt.Graphics; 7 import java.awt.Graphics2D; 8 import java.awt.Image; 9 import java.awt.Toolkit; 10 import java.awt.color.Colo

Atitit. 图像处理jpg图片的压缩 清理垃圾图片 java版本

Atitit. 图像处理jpg图片的压缩  清理垃圾图片 java版本 1. 清理图片压缩图片尺寸 1 2. 所以要使用ImageWriter 1 3. Thumbnails质量压缩builder.outputQuality(0.9); 2 4. attilax框架的处理 code 2 5. 到一篇文章提到如何控制jpg图片后压缩的质量 3 6. 参考 4 1. 清理图片压缩图片尺寸 目标::300kb>>>10kb.. 处理流程:::scale,outputQuality(0.5) 裁

atitit.验证码识别step3----去除边框---- 图像处理类库 attilax总结java版本

atitit.验证码识别step3----去除边框---- 图像处理类库 attilax总结java版本 1. 去除边框思路原理 1 2. Thumbnailator 是一个用来生成图像缩略图.裁切.旋转.添加水印等操作 2 3. OpenCL的Java库 JavaCL 2 4. Java Image Filters是一款基于Java的图像处理类库,特别是在图像滤镜特效方面, 2 4.1.1. 色彩调整 2 4.1.2. 变形和扭曲 5 5. JJIL 是一个Java 的图像处理类库,有超过60

百分之九十九的JAVA工作者都不知道的知识

1.Core Java部分 这是最基础的,对于一个java高级开发/设计人员,你需要对这一部分达到精通的水平,重点内容如下: a.面向对象编程思想(封装继承多态接口) b.字符串处理 c.java.lang包,java.util包等常用包 4.java异常处理 2.Java高级部分 a.Java I/O流 b.Java多线程技术 c.Java网络编程 d.Java Swing 后两项可以了解即可,如果项目需要可以深入研究 3.前端基本技能 * HTML + CSS网页开发 * JavaScrip

JAVA面试必备的知识宝典(一)

相关概念 面向对象的三个特征 封装,继承,多态.这个应该是人人皆知.有时候也会加上抽象. 多态的好处 允许不同类对象对同一消息做出响应,即同一消息可以根据发送对象的不同而采用多种不同的行为方式(发送消息就是函数调用).主要有以下优点: 可替换性:多态对已存在代码具有可替换性. 可扩充性:增加新的子类不影响已经存在的类结构. 接口性:多态是超累通过方法签名,想子类提供一个公共接口,由子类来完善或者重写它来实现的. 灵活性: 简化性: 代码中如何实现多态 实现多态主要有以下三种方式: 1. 接口实现

JAVA面试必备的知识宝典(二)

WeakReference与SoftReference的区别? 这点在四种引用类型中已经做了解释,这里简单说明一下即可: 虽然 WeakReference 与 SoftReference 都有利于提高 GC 和 内存的效率,但是 WeakReference ,一旦失去最后一个强引用,就会被 GC 回收,而软引用虽然不能阻止被回收,但是可以延迟到 JVM 内存不足的时候. 为什么要有不同的引用类型 不像C语言,我们可以控制内存的申请和释放,在Java中有时候我们需要适当的控制对象被回收的时机,因此