美颜相机开发手册
1.数字化图像底层原理
2.文件读取图片,操作图片矩阵
3.图像滤镜算法实现
4.项目结构设计
5.图像存储,附加功能实现(画笔/P图/贴纸…)
6.卷积图像处理算法实现
7.图像识别原理
8.二维码的生成与识别
9.以图搜图功能实现
美颜相机第二天:实现美颜相机的各种滤镜,以及滤镜的一些基本原理讲解。重构项目结构:
首先重构项目的基本结构:规划出一个图片显示界面,一个按钮监听界面:
package wenhao777;import wenhao666.CameraUi;import javax.swing.*;
import java.awt.*;public class CameraUI {String[] btns = {"原画","马赛克","圆马赛克","灰度","油画","二值化"};BtnsActionListener btnsActionListener = new BtnsActionListener();public void initUI(){JFrame jf = new JFrame();jf.setTitle("美颜相机");jf.setSize(1100,800);jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//设置两个界面来分别画图//图像显示界面JPanel showPanel = new JPanel();showPanel.setBackground(Color.lightGray);//按钮显示界面JPanel btnPanel = new JPanel();btnPanel.setBackground(Color.white);btnPanel.setPreferredSize(new Dimension(200,0));initBtns(btnPanel);jf.add(showPanel,BorderLayout.CENTER);jf.add(btnPanel,BorderLayout.WEST);jf.setVisible(true);btnsActionListener.setPanel(showPanel);}//为界面添加按钮public void initBtns(JPanel btnPanel){for (int i = 0; i < btns.length; i++) {JButton btn = new JButton(btns[i]);btn.setBackground(Color.white);btn.setPreferredSize(new Dimension(95,35));btn.addActionListener(btnsActionListener);//将按钮添加到页面上btnPanel.add(btn);}}public static void main(String[] args) {new CameraUI().initUI();}
}
然后是为按钮添加监听器的界面:
package wenhao777;import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class BtnsActionListener implements ActionListener {JPanel showPanel;String path = "F://新建文件夹//美颜相机复盘//img//6.png";FilterTools filterTools = new FilterTools(path);@Overridepublic void actionPerformed(ActionEvent e) {Graphics g = showPanel.getGraphics();String acation = e.getActionCommand();if(acation.equals("原画")){filterTools.drawOriginalImage(g);}if(acation.equals("灰度")){filterTools.drawGrayImage(g);}}public void setPanel(JPanel showPanel) {this.showPanel=showPanel;}
}
最后是写滤镜的实现类:
package wenhao777;import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;public class FilterTools {int[][] imgarr;BufferedImage bufferImg;int width;int height;FilterTools(String path){getImagePix(path);width = bufferImg.getWidth();height = bufferImg.getHeight();}//将图片转化为二维数组/二维矩阵public int[][] getImagePix(String path){File file = new File(path);try {bufferImg = ImageIO.read(file);imgarr= new int[bufferImg.getWidth()][bufferImg.getHeight()];for (int i = 0; i < bufferImg.getWidth(); i++) {for(int j =0;j<bufferImg.getHeight();j++){imgarr[i][j]=bufferImg.getRGB(i,j);}}return imgarr;} catch (IOException ioException) {ioException.printStackTrace();}return null;}//原图public void drawOriginalImage(Graphics g){g.drawImage(bufferImg,0,0,null);}//灰度public void drawGrayImage(Graphics g){for (int i = 0; i <width ; i++) {for(int j=0;j<height;j++){int pix = imgarr[i][j];int red = (pix>>16)&0xFF;int green = (pix>>8)&0xFF;int blue = (pix>>0)&0xFF;int gray = (red+green+blue)/3;Color color = new Color(gray,gray,gray);g.setColor(color);g.fillOval(i,j,1,1);}}}
}
这里面暂时写了实现的两个方法,一个原画,一个灰度,实现效果如下:
美颜相机原画效果
美颜相机灰度效果展示
如有更多要求,请关注B站up主兰习习,请大家为up主一键三连哦!