实现水平方向重采样的java图像处理程序
该程序主要实现水平方向上的灰度图像重采样。重采样通过3个for循环控制完成。原始lena图如下所示:
水平方向每8个点采集一个点的lena图像如下所示:
package p01;import java.awt.*;import java.awt.event.*;import java.awt.image.*;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;import javax.swing.*;public class chongcaiyang extends Frame{Image im, tmp;int iw, ih;int[] pixels;boolean flag_load = false;//构造方法public chongcaiyang(){super("chongcaiyang");Panel pdown; Button load, run, save, quit; addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); pdown = new Panel(); pdown.setBackground(Color.lightGray); //定义按钮 load = new Button("装载图像"); run = new Button("重采样"); save = new Button("保存"); quit = new Button("退出"); this.add(pdown, BorderLayout.SOUTH); //添加按钮 此处的顺序为案板上的左->右的顺序 pdown.add(load); pdown.add(run); pdown.add(save); pdown.add(quit); //按钮的动作程序 装载图像 load.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ try {jLoad_ActionPerformed(e);} catch (IOException e1) {e1.printStackTrace();} } }); //按钮的动作程序 重采样 run.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ try {jRun_ActionPerformed(e);} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} } }); //按钮的动作程序 保存 save.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ try {jSave_ActionPerformed(e);} catch (IOException e1) {e1.printStackTrace();} } }); //按钮的动作程序 退出 quit.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ jQuit_ActionPerformed(e); } });}//按钮动作的实现 加载图像 public void jLoad_ActionPerformed(ActionEvent e) throws IOException{ File inputFile = new File("E:\\f2\\sc\\lena_grey.jpg"); BufferedImage input = ImageIO.read(inputFile);tmp = input; flag_load = true; repaint();} //按钮动作的实现 重采样 public void jRun_ActionPerformed(ActionEvent e) throws IOException{ if(flag_load){ File inputFile = new File("E:\\f2\\sc\\lena_grey.jpg"); BufferedImage input = ImageIO.read(inputFile); iw = input.getWidth(this); ih = input.getHeight(this); //设定N值 int N = iw; String s = JOptionPane.showInputDialog(null,"请输入N值(512/256/128/64/32/16/8)"); if(s!= null&& !s.equals("")){ N = Integer.parseInt(s); } //检查输入是否正确 if((N>512)|(N<8)){ N = 512; JOptionPane.showMessageDialog(null, "输入不正确,请重新输入!"); } //图像重采样 BufferedImage grayImage = new BufferedImage(iw, ih, BufferedImage.TYPE_BYTE_GRAY); //kao!三个循环就就能搞定,不就是重采样吗?for(int i=0; i<ih-1; i++){for(int j=0; j<iw-1; j=j+(ih/N)){for(int k=0; k<(ih/N); k++){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+k, rgb);}}} //产生图像 tmp = grayImage; flag_load = true; repaint(); }else{ JOptionPane.showMessageDialog(null, "先点击“装载图像”,3Q!","提示:", 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("E:\\f2\\sc\\save_t01.jpg"); ImageIO.write(bi, "JPG", save_path); }else{ JOptionPane.showMessageDialog(null, "先点击“装载图像”,3Q!","提示:", JOptionPane.WARNING_MESSAGE); } } //按钮动作的实现 退出 public void jQuit_ActionPerformed(ActionEvent e){ System.exit(0); } //绘图函数 public void paint(Graphics g){ //if(flag_Load){ g.drawImage(tmp,50,50,this); //}else{} } public static void main(String[] args) {chongcaiyang ti = new chongcaiyang();ti.setSize(1000,860);ti.setVisible(true);}}