1、窗体初始化方法
public void InitUI(){
this.setTitle("图像处理");
this.setSize(1800,1800);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
2、创建一个缓冲图片,把缓冲图片的每个像素点的颜色遍历,读取到一个二维数组
public int[][] getPixelArray(String imgPath) {
File file=new File(imgPath);
BufferedImage buffimg=null;
try {
buffimg=ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
int w=buffimg.getWidth();
int h=buffimg.getHeight();
int[][] imgarr=new int[w][h];
for(int i=0;i<w;i++) {
for(int j=0;j<h;j++) {
imgarr[i][j]=buffimg.getRGB(i, j);
}
}
return imgarr;
}
3、重写paint方法 把二维数组的像素点画出来
public void paint(Graphics g) {
super.paint(g);
int[][]imgarr=getPixelArray("C:\\Users\\86158\\Desktop\\微信图片_20220120191028.jpg");
int w=imgarr.length;
int h=imgarr[0].length;
for(int i=0;i<w;i++) {
for(int j=0;j<h;j++) {
int rgb=imgarr[i][j];
Color color=new Color(rgb);
g.setColor(color);
g.fillRect(i, j, 1, 1); }
4、写出许多不同的滤镜效果
温暖
int red=color.getRed();
int green=color.getGreen();
int blue=color.getBlue();
Color warmness=new Color(red,green,blue/3);
g.setColor(warmness);
梦幻
Color dreamland=new Color(red,green/3*2,blue);
g.setColor(dreamland);
底片
Color negative=new Color(255-red,255-green,255-green);
g.setColor(negative);
马赛克
public void paint(Graphics g) {
super.paint(g);
int[][]imgarr=getPixelArray("C:\\Users\\86158\\Desktop\\微信图片_20220120191028.jpg");
int w=imgarr.length;
int h=imgarr[0].length;
for(int i=0;i<w;i+=10) {
for(int j=0;j<h;j+=10) {
int rgb=imgarr[i][j];
Color color=new Color(rgb);
g.setColor(color);
g.fillRect(i, j, 10, 10);
}}
}
黑白
int av=(red+blue+green)/3;
Color grey=new Color(av,av,av);
g.setColor(grey);