[Java] multithreading (taking java webcam as an example)

[Java] multithreading (taking the realization of video function as an example)

step1: how to realize video

Video is realized by pictures, so we only do two things:
1) Find an interface that can call the camera
2) Use this interface to get the picture of every moment
3) Draw these pictures in a continuous cycle

Step 2: why can't you turn it off? (why multithreading is required)

After opening the video in the first step of realizing step3, we found that although we can realize this function, it is off! no Come on! It's over!
WHY?
Because the program is executed sequentially, we wrote an endless loop
This dead cycle has not been executed. How can it be executed later?
So we fell into a deep thought... Because we can't produce the video without looping, but as long as we are in the loop, we can't click
Because the clicked program is now occupied!!! Click from the program is drawing now. How can I handle clicking for you?
Therefore, we think, in this case, can two programs be executed at the same time?
One responds to my instructions at any time, and the other works to draw the images captured by the camera
Therefore, the concept of thread is introduced. Our computers are basically multithreaded to execute commands, ah, using this idea. That's why you can open multiple web pages.
The thread that responds to your command returns the parameters to the thread that draws the picture, and the thread stops working after receiving the signal. In this way, our thinking and design are completed!

step3: Implementation

Open video

First of all, we need to introduce the interface to open the video. This requires us to introduce the corresponding jar packages. What we need are these jar packages

Link: https://pan.baidu.com/s/14o45g2hAnc40-wsi5Zl-xQ
Extraction code: wm1f

It's sorted out here and can be used directly if necessary~
Then import the jar package with IDEA:
File ----- project structure ----- modules ----- dependencies ------ + "-------" jars or directors... "Can be imported again~
After importing, these lines of code can complete the video function (of course, the premise is that you have to write a JFrame yourself! That's the variable g in it)

 Webcam webcam = Webcam.getDefault();
            webcam.open();

            while (true) {
                // Get the data captured by the camera
                BufferedImage bufferedImage = webcam.getImage();
                g.drawImage(bufferedImage, 100, 100,500,400, null);
            }

Start multithreading

Introduce thread class knowledge

First, we use a class to implement threads, so we need this class to inherit threads

public class ThreadPixel extends Thread{}

1) The name of the parent class of a Thread is called Thread
2) When the thread just starts, it needs to use start to start
3) After the thread is started, it will automatically call its own run method, so we need to use the nature of run. Therefore, we must rewrite run. If there is no run method, this class is no different from other ordinary classes
4) When run() is executing, it needs to call the start() method with a thread. At this time, run() has the function of thread, or it is a simple method
5) After everything in run() is executed, it will die. There is no way to execute with start again

In addition to the above properties, thread is no different from others, no difference!!!
run() without start() is just an ordinary method
Another thing that needs to be clear is that we only need one more thread here. Don't create it many times! (if you need more than one, just create more. After you create more than one, remember to manage each one)

Related code

On the outside: set up the thread and start the thread

 else if(name.equals("open")){
            if(tp == null){
                tp = new ThreadPixel(g);
                tp.start();
            }
        }
        else if(name.equals("close")){
            if(tp != null){
                tp.flag = false;
                tp = null;
            }

Inside: writing method

package beautiful1;
import com.github.sarxos.webcam.Webcam;
import java.awt.*;
import java.awt.image.BufferedImage;

public class ThreadPixel extends Thread{
    public Graphics g;
    public boolean flag = true;

    public ThreadPixel(Graphics g){
        this.g = g;
    }

    // Responsible for getting photos of cameras
    public void run(){
        System.out.println("Start thread:"+this.getName());

        Webcam webcam = Webcam.getDefault();
        webcam.open();

        while(flag){
            BufferedImage bufferedImage = webcam.getImage();
            g.drawImage(bufferedImage,100,100,null);
        }
    }
}

Code description

\\ null It is only established when it indicates that there is no thread, so we have only one thread here
if(tp == null){
                tp = new ThreadPixel(g);
                tp.start();
            }
\\ Start calling in the thread run
 tp.start();  
\\ If you close, eliminate this class, because when you let flag by false After that,
This thread will die directly, and it can be destroyed directly outside. If it is not destroyed,
Next time you can't be name When it's time to open, the opening function is completed
  else if(name.equals("close")){
            if(tp != null){
                tp.flag = false;
                tp = null;
            }
\\ Create a camera object and open it
Webcam webcam = Webcam.getDefault();
        webcam.open();
\\ use flag To control the operation of the thread, when flag by false The thread ends and dies
 while(flag){
            BufferedImage bufferedImage = webcam.getImage();
            g.drawImage(bufferedImage,100,100,null);
        }

Programming cold knowledge

When programming, we will set some properties to public in the hope that this parameter can be passed between multiple classes, or that this parameter can be easily modified by other classes externally

Keywords: Java intellij-idea

Added by hellangel on Wed, 23 Feb 2022 10:12:56 +0200