美颜相机开发
UI的使用:
JFrame JButton
Listener:
加入按钮实现点击什么滤镜特效就绘制什么滤镜!
代码:
首先对UI进行编写
public class ImagePad {//使用字符串储存按钮名称String[] strs = {"打开", "保存", "原图", "灰度", "二值化", "马赛克", "原点马赛克", "怀旧", "轮廓", "素描", "锐化", "缩小", "放大"};//创建监听器ImageListener img1=new ImageListener();public void showUI(){//主体的布局JFrame jf = new JFrame("图像处理V1.0");jf.setSize(800, 600);jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jf.setLayout(new FlowLayout());Font font = new Font("黑体", Font.BOLD, 16);//将按钮名称循环加入按钮并对其加入监听for (int i = 0; i < strs.length; i++) {JButton btn = new JButton(strs[i]);btn.setBackground(Color.WHITE);btn.setFont(font);btn.addActionListener(img1);jf.add(btn);}jf.setVisible(true);//可视化后要创建绘画img1.g= jf.getGraphics();}
绘画工具函数的编写
获取图片像素值
public int[][] getImagePixeArray(String path){//对图片路径进行读入File file = new File(path);BufferedImage buffimg=null;try {buffimg=ImageIO.read(file);} catch (IOException e) {throw new RuntimeException(e);}//获取图片像素值int width =buffimg.getWidth();int height=buffimg.getHeight();//定义和图片宽高相等的数组来储存像素值int[][] imgArr = new int[width][height];for (int i = 0; i < imgArr.length; i++) {for (int j = 0; j < imgArr[i].length; j++) {imgArr[i][j]=buffimg.getRGB(i,j);}}return imgArr;}
编写不同滤镜的特效函数
原图
/*** 原图* @param imgArr* @param g*/public void drawImage(int[][] imgArr, Graphics g){for (int i = 0; i < imgArr.length ; i++) {for (int j = 0; j < imgArr[i].length; j++) {int pixnum=imgArr[i][j];Color color = new Color(pixnum);g.setColor(color);g.fillRect(X+i,Y+j,1,1);}}}
灰度图
/*** 灰度图* @param imgArr* @param g*/public void drawGrayImage(int[][] imgArr, Graphics g){for (int i = 0; i < imgArr.length ; i++) {for (int j = 0; j < imgArr[i].length; j++) {int pixnum=imgArr[i][j];//拆分像素值int red=(pixnum>>16)&255;int green=(pixnum>>8)&255;int blue=(pixnum>>0)&255;//灰度图RGB值相等int gray=(red+green+blue)/3;Color color = new Color(gray);g.setColor(color);g.fillRect(X+i,Y+j,1,1);}}}
二值化图
/*** 二值化图* @param imgArr* @param g*/public void drawBinaryImage(int[][] imgArr, Graphics g){for (int i = 0; i < imgArr.length ; i++) {for (int j = 0; j < imgArr[i].length; j++) {int pixnum=imgArr[i][j];//拆分像素值int red=(pixnum>>16)&255;int green=(pixnum>>8)&255;int blue=(pixnum>>0)&255;int gray=(red+green+blue)/3;//利用灰度值做二分if(gray<127){g.setColor(Color.black);}else {g.setColor(Color.WHITE);}g.fillRect(X+i,Y+j,1,1);}}}
马赛克图
/*** 马赛克* @param imgArr* @param g*/public void drawMosaicImage(int[][] imgArr, Graphics g){for (int i = 0; i < imgArr.length ; i+=10) {for (int j = 0; j < imgArr[i].length; j+=10) {//间距采样//均值法int pixnum=imgArr[i][j];Color color = new Color(pixnum);g.setColor(color);g.fillRect(X+i,Y+j,10,10);}}}
在监听器中实现
public class ImageListener implements ActionListener {ImageTools imgtools=new ImageTools();Graphics g;//定义数组等于获取的照片数组int [][] imagArr;{String path="F:\\java learning\\gaoyuhao.png";imagArr=imgtools.getImagePixeArray(path);}//对actionPerformed函数进行重写@Overridepublic void actionPerformed(ActionEvent e) {String btnStr=e.getActionCommand();System.out.println("点击了"+btnStr);switch (btnStr){case "原图":imgtools.drawImage(imagArr,g);break;case "灰度":imgtools.drawGrayImage(imagArr,g);break;case "二值化":imgtools.drawBinaryImage(imagArr,g);break;case "马赛克":imgtools.drawMosaicImage(imagArr,g);break;}}
}