Java图像处理进行的图像缩放,灰度处理,黑白转换

最近学校在学习图像处理,但是工作中经常以Java去开发。所以用Java进行去实现图像处理,刚刚接触就用了一些基本的处理,希望自己能够好好学习图像处理,把这门考试给过了!

以下是简单的操作:

public class MyFilters extends JPanel {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private BufferedImage image = null;
    private BufferedImage destImage = null;
    public MyFilters()
    {
    }

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.clearRect(0, 0, this.getWidth(), this.getHeight());
        if(image != null)
        {
            g2d.drawImage(image, 0, 0, image.getWidth(),
                    image.getHeight(), null);
            if(destImage != null)
            {
                g2d.drawImage(destImage, image.getWidth() + 10 ,
                        0, destImage.getWidth(),
                        destImage.getHeight(), null);
            }
        }
    }

    public void doColorGray(BufferedImage bi)
    {
        ColorConvertOp filterObj = new ColorConvertOp(
                ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
        destImage = filterObj.filter(bi, null);
    }

    public void doBinaryImage(BufferedImage bi)
    {
        doColorGray(bi);
        byte[] threshold = new byte[256];
        for (int i = 0; i < 256; i++)
        {
            threshold[i] = (i < 128) ? (byte)0 : (byte)255;
        }
        BufferedImageOp thresholdOp =
                new LookupOp(new ByteLookupTable(0, threshold), null);
        destImage = thresholdOp.filter(destImage, null);
    }

//进行BufferedImageOp 实现多处理图片
    public void doBlur(BufferedImage bi)
    {
        // fix issue - unable to convolve src image
        if (bi.getType()==BufferedImage.TYPE_3BYTE_BGR)
        {
            bi=convertType(bi, BufferedImage.TYPE_INT_RGB);
        }

        float ninth = 1.0f / 9.0f;
        float[] blurKernel = {
                ninth, ninth, ninth,
                ninth, ninth, ninth,
                ninth, ninth, ninth
        };
        BufferedImageOp blurFilter =
                new ConvolveOp(new Kernel(3, 3, blurKernel));
        destImage = blurFilter.filter(bi, null);
    }

    public BufferedImage doLookUp(BufferedImage bi)
    {
        byte[][] lookupData = new byte[3][256];
        for (int cnt = 0; cnt < 256; cnt++){
            lookupData[0][cnt] = (byte)(255-cnt);
            lookupData[1][cnt] = (byte)(255-cnt);
            lookupData[2][cnt] = (byte)(255-cnt);
        }
        ByteLookupTable lookupTable =
                new ByteLookupTable(0,lookupData);
        BufferedImageOp filterObj =
                new LookupOp(lookupTable,null);
        return filterObj.filter(bi, null);
    }

    public void doScale(BufferedImage bi, double sx, double sy)
    {
        AffineTransformOp atfFilter = new AffineTransformOp(
                AffineTransform.getScaleInstance(sx, sy),
                AffineTransformOp.TYPE_BILINEAR);
        int nw = (int)(bi.getWidth() * sx);
        int nh = (int)(bi.getHeight() * sy);
        BufferedImage result = new BufferedImage(
                nw, nh, BufferedImage.TYPE_3BYTE_BGR);
        destImage = atfFilter.filter(bi, result);
    }

    /**
     * you always get this issue - unable to convolve src image
     *
     * @param src
     * @param type
     * @return
     */
    private BufferedImage convertType(BufferedImage src, int type)
    {
        ColorConvertOp cco=new ColorConvertOp(null);
        BufferedImage dest=new BufferedImage(
                src.getWidth(), src.getHeight(), type);
        cco.filter(src, dest);
        return dest;
    }
}

 

public class MyFilterUI extends JFrame implements ActionListener {
    /**
     *
     * @param e
     */

    private static final long serialVersionUID=1L;

    private static final String GRAY_CMD="灰度";
    private static final String BINARY_CMD="黑白";
    private static final String BLUR_CMD="模糊";
    private static final String ZOOM_CMD="放缩";
    private static final String BROWSER_CMD="选择...";

    //不同的按钮

    private JButton grayBtn;
    private JButton binaryBtn;
    private JButton blurBtn;
    private JButton zoomBtn;
    private JButton browserBtn;
    private MyFilters filters;

    //image 图片

    private BufferedImage srcImage;

    private MyFilterUI(){
        this.setTitle("Java --滤镜演示");
        grayBtn =new JButton(GRAY_CMD);
        binaryBtn=new JButton(BINARY_CMD);
        blurBtn=new JButton(BLUR_CMD);
        zoomBtn=new JButton(ZOOM_CMD);
        browserBtn=new JButton(BROWSER_CMD);
        //frame
        JPanel btnPanel=new JPanel();
        btnPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        //想容器内添加按钮
        btnPanel.add(grayBtn);
        btnPanel.add(binaryBtn);
        btnPanel.add(blurBtn);
        btnPanel.add(zoomBtn);
        btnPanel.add(browserBtn);

        //filters
        filters=new MyFilters();
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(filters,BorderLayout.CENTER);
        getContentPane().add(btnPanel,BorderLayout.SOUTH);
        //setup listener
        setupActionListener();
    }

    /**
     * 添加监听事件
     */
    private void setupActionListener() {
        grayBtn.addActionListener(this);
        binaryBtn.addActionListener(this);
        blurBtn.addActionListener(this);
        zoomBtn.addActionListener(this);
        browserBtn.addActionListener(this);
    }


    @Override
    public void actionPerformed(ActionEvent e) {

        if (srcImage == null)
        {
            JOptionPane.showMessageDialog(this,"请先选择图像源文件");

            try {
                JFileChooser chooser=new JFileChooser();
                chooser.showOpenDialog(null);
                File f=chooser.getSelectedFile();
                srcImage= ImageIO.read(f);
                filters.setImage(srcImage);
                filters.repaint();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            return;
        }
        else if (GRAY_CMD.equals(e.getActionCommand())){
            filters.doColorGray(srcImage);
            filters.repaint();
        }
       else if (BINARY_CMD.equals(e.getActionCommand())){
            filters.doBinaryImage(srcImage);
            filters.repaint();
        }
       else if (BLUR_CMD.equals(e.getActionCommand())){
            filters.doBlur(srcImage);
            filters.repaint();
        }
       else if (ZOOM_CMD.equals(e.getActionCommand())){
            filters.doScale(srcImage,1.5,1.5);
            filters.repaint();
        }
        else if (BROWSER_CMD.equals(e.getActionCommand())){
            try{
                JFileChooser chooser=new JFileChooser();
                chooser.showOpenDialog(null);
                File f=chooser.getSelectedFile();
                srcImage=ImageIO.read(f);
                filters.setImage(srcImage);
                filters.repaint();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        MyFilterUI ui=new MyFilterUI();
        //默认退出
        ui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //窗体大小
        ui.setPreferredSize(new Dimension(800,600));
        ui.pack();
        ui.setVisible(true);
    }
}

 

虽然代码很简单,但以前只是做java开发,从来不接触GUI的东西,其实还是挺不错的。

你可能感兴趣的:(Java图像处理进行的图像缩放,灰度处理,黑白转换)