基于BufferedImage的图像滤镜演示

package chapter2;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import java.io.File;

import java.io.IOException;

/**
* Created by LENOVO on 18-1-28.
*/
public class MyFilterUI extends JFrame implements ActionListener{

private static final long serialVersionUID = 1L;
public static final String GRAY_CMD = "灰度";
public static final String BINARY_CMD = "黑白";
public static final String BLUR_CMD = "模糊";
public static final String ZOOM_CMD = "放缩";
public static final String BROWSER_CMD = "请选择...";

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

public BufferedImage srcImage;

public MyFilterUI(){
this.setTitle("JAVA 2D BufferedImageOp——滤镜演示");
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);

//添加组件到面板上
JPanel myPanel = new JPanel();
myPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
myPanel.add(grayBtn);
myPanel.add(binaryBtn);
myPanel.add(blurBtn);
myPanel.add(zoomBtn);
myPanel.add(browserBtn);

filters = new MyFilters();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(filters,BorderLayout.CENTER);
getContentPane().add(myPanel,BorderLayout.SOUTH);

//按钮添加监听事件
setupActionListener();

}
public void setupActionListener(){
grayBtn.addActionListener(this);
binaryBtn.addActionListener(this);
blurBtn.addActionListener(this);
zoomBtn.addActionListener(this);
browserBtn.addActionListener(this);
}
public void setFileTypeFilter(JFileChooser chooser){
FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & PNG","jpg","png");
chooser.setFileFilter(filter);
}
@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);
if(f !=null){
filters.setSourceImage(srcImage);
filters.repaint();
}
} catch(IOException e1){
e1.printStackTrace();
}

return ;
}
if(GRAY_CMD.equals(e.getActionCommand())){
//filters.doColorGray(srcImage);
filters.setDestImage( filters.doColorGray(srcImage));
filters.repaint();
}else if(BINARY_CMD.equals(e.getActionCommand())){
//filters.doBinaryImage(srcImage);
filters.setDestImage(filters.doBinaryImage(srcImage));
filters.repaint();
}else if(BLUR_CMD.equals(e.getActionCommand())){
//filters.doBlur(srcImage);
filters.setDestImage(filters.doBlur(srcImage));
filters.repaint();
}else if(ZOOM_CMD.equals(e.getActionCommand())){
//filters.doScale(srcImage,1.5,1.5);
filters.setDestImage(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);
if(f != null){
filters.setSourceImage(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);
}
public static class MyFilters extends JPanel{
public BufferedImage sourceImage;
public BufferedImage destImage;

public BufferedImage getSourceImage() {
return sourceImage;
}

public void setSourceImage(BufferedImage sourceImage) {
this.sourceImage = sourceImage;
}

public BufferedImage getDestImage() {
return destImage;
}

public void setDestImage(BufferedImage destImage) {
this.destImage = destImage;
}

public MyFilters(){
// super();
}
public void paintComponent(Graphics g){
//super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.clearRect(0,0,this.getWidth(),this.getHeight());
if(sourceImage != null){

g2d.drawImage(sourceImage,0,0, sourceImage.getWidth(), sourceImage.getHeight(),null);
if(destImage != null){
g2d.drawImage(destImage,sourceImage.getWidth()+10,0,destImage.getWidth(),
destImage.getHeight(),null);
}

}
//g2d.drawImage(srcImage,0,0, srcImage.getWidth(), srcImage.getHeight(),this);

}

/*实现灰度化
ColorConvertOp主要用于实现各种色彩空间的转换,实现灰度功能时,只需在实例化
ColorConvertOp时指定色彩空间为ColorSpace.CS_GRAY,然后调用它的filter方法返回图像即可。
*/
public BufferedImage doColorGray(BufferedImage bi){
ColorConvertOp filterObj = new ColorConvertOp(
ColorSpace.getInstance(ColorSpace.CS_GRAY),null
);
return filterObj.filter(bi,null);
}
/*实现黑白功能
* LookupOp在实例化时需要传入LookupTable实例,当前实现LookupTable接口的两个实现类为
* ByteLookupTable和ShortLookupTable
* 1)首先将图像灰度化
* 2)针对灰度图像在LookupTable中根据像素值进行索引查找,以便设置新的像素值,从而得到黑白两色图像
*/
public BufferedImage doBinaryImage(BufferedImage bi){
bi = doColorGray(bi);//图像灰度化
byte[] threshold = new byte[256];
for (int i = 0; i<256;i++){
threshold[i] = (byte) ((i<128)? 0:255);
}
BufferedImageOp thresholdOp = new LookupOp(new ByteLookupTable(0,threshold),null);
return thresholdOp.filter(bi,null);
}
/*实现模糊功能
* ConvloveOp是实现模板卷积功能操作的类,通过简单设置卷积核/卷积模板就可以实现图像模糊功能
* 多数情况下图像使用TYPE_3BYTE_BGR存储方式,而BufferedImageOp实现的滤镜不支持操作该存储方式的BufferedImage对像
* 解决方法是首先通过ColorConvertOp把图像从类型TYPE_3BYTE_BGR转换为TYPE_INT_RGB的BufferedImage对象
*/
public 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 BufferedImage doBlur(BufferedImage bi){
if(bi.getType() == BufferedImage.TYPE_3BYTE_BGR){
bi = convertType(bi,BufferedImage.TYPE_INT_RGB);
}
float ninth = 1.0f/9.0f;
float[] blurKernal = {
ninth,ninth,ninth,
ninth,ninth,ninth,
ninth,ninth,ninth
};
BufferedImageOp blurFilter = new ConvolveOp(new Kernel(3,3,blurKernal));
return blurFilter.filter(bi,null);
}

/*实现zoom in/out的功能
* AffineTransformOp支持的操作包括图像的错切、旋转、放缩、平移
* 首先要通过 AffineTransform.getScaleInstance来获取Scale实例,然后作为参数初始化AffineTransformOp实例
*/
public BufferedImage doScale(BufferedImage bi,double sx,double sy){

AffineTransformOp atFilter = 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);
return atFilter.filter(bi,result);
}
}

}

转载于:https://www.cnblogs.com/bigdream6/p/8377022.html

你可能感兴趣的:(基于BufferedImage的图像滤镜演示)