On the eve of the Spring Festival, under the guidance of Chen Ge, some filter effects in the beauty camera were added, but the use of filter effects before was based on individual pictures, and our video is also composed of pictures frame by frame Composition, so when we want to complete the beauty camera, the first thing we need to learn is how to get the video (javaOpenCV library, or WebCamp). The following uses WebCamp as an example!
The functions to be implemented include but are not limited to: taking pictures, video acquisition, video recording, screenshots, face recognition, special effects. …
1. The writing of the main program mainly uses awt and swing components
package CL2.Draw0117.Video0123; import javax.swing.*; import java.awt.*; public class VDraw { public void initt() { //Form settings JFrame jf = new JFrame(); //Form size setting jf.setSize(810,900); //Form name setting jf.setTitle("Video camera capture"); //exit process settings jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setLocationRelativeTo(null); //Form layout settings BorderLayout borderLayout=new BorderLayout(); jf.setLayout(borderLayout); //Center form background setting JPanel center=new JPanel(); center.setBackground(Color.white); jf.add(center,BorderLayout.CENTER); //The creation of the Vpixelmouse object is convenient for adding listeners Vpixelmouse mouse=new Vpixelmouse(); //Menu Bar JMenuBar jmb = new JMenuBar(); // Fix the position of the menu bar jf.add(jmb,BorderLayout.NORTH); // menu options JMenu jmn = new JMenu("Video"); jmb.add(jmn); //Submenu JMenuItem jmi = new JMenuItem("Open"); JMenuItem jmi2 = new JMenuItem("Close"); jmn. add(jmi); jmn. add(jmi2); //Visibility must be in front of the brush jf.setVisible(true); mouse.g=center.getGraphics(); //add listener jmi.addActionListener(mouse); jmi2.addActionListener(mouse); } public static void main(String[] args) { VDraw vDraw=new VDraw(); vDraw.initt(); } }
2.
package CL2.Draw0117.Video0123; import javax.imageio.ImageIO; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class Vpixelmouse implements ActionListener { public Graphics g; // class to pass data public Vthreadpixel vp; public boolean flag; public void actionPerformed(ActionEvent e){ String st = e.getActionCommand(); if ("Open".equals(st)) { // Start multithreading // Create a thread object, showing that the thread is created once if (vp == null) { vp=new Vthreadpixel(g); // start the thread vp.start(); } } else if ("Close".equals(st)) { vp.flag = false; System.out.println("Close"); } } public int[][] getImagePixel(String path) { File file = new File(path); // ImageIO: I/O to image files // Get the buffered image BufferedImage buffImage = null; try { buffImage = ImageIO.read(file); } catch (IOException e1) { e1.printStackTrace(); } int w = buffImage.getWidth(); int h = buffImage.getHeight(); int[][] pixelArr = new int[w][h]; // Get each pixel value in the picture and save it in a two-dimensional array for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { int pixel = buffImage.getRGB(i, j); pixelArr[i][j] = pixel; } } return pixelArr; } }
3. The use of multithreading
package CL2.Draw0117.Video0123; import com.github.sarxos.webcam.Webcam; import java.awt.*; import java.awt.image.BufferedImage; public class Vthreadpixel extends Thread { public Graphics g; / / control thread mark public boolean flag = true; //Initialize the brush object public Vthreadpixel(Graphics g) { this.g = g; } // The method of self-execution after starting the thread // After the run method is executed, the thread ends. Once the thread ends, it cannot be started again public void run() { System.out.println("Start thread.."+this.getName()); // Start the camera and start it in the thread Webcam webcam=Webcam.getDefault(); webcam.open(); // while (flag) { // Get the data captured by the camera BufferedImage bufferedImage=webcam.getImage(); // size setting g.drawImage(bufferedImage,100,100,400,300,null); } } }
4. The effect is as shown in the figure