Java + OpenCv calls the specified camera according to PID/VID

Question :

        The host is connected to multiple USB cameras. The traditional OpenCv is called by the subscript of the camera inserted into the usb. If only one camera is connected, then directly use capture.open(0); there is no problem in calling this way. If there is one, there will be problems, because the subscript corresponding to the camera will change when the USB is unplugged.

Solution ideas :

        Use the unique identifier of the hardware device, which is PID and VID to determine the subscript of the USB camera, so that no matter how the subscript changes, we will call it through PID and VID, and we will successfully solve our problem.

Not much to say, go directly to the steps:

1. First check the PID/VID number of the hardware device

        My computer is win10. Here I only explain the steps of win10. How to check other systems on Baidu.

        Click This Computer-->Management-->Device Manager--->Camera--->Click USB Camera Properties--->Details---->Hardware ID

You can see the PID/VID number

        

 

 

 2. Add dynamic library

        I also saw that the big guy wrote it and changed it. The big guy only wrote the calling method of python and C

        Daxie Blog: According to the camera hardware identification VID&PID, obtain the parameter index required by OpenCV to open the

camera

CvCameraIndex: According to the camera hardware identification (such as vid_1234&pid_4321) to obtain the parameter subscript index when OpenCV opens the camera,


here I explain the Java version

        You need to download jna.jar and add it to the project environment

code show as below

import com.sun.jna.Library;
import com.sun.jna.Native;

public class OpenCv {

    public interface CallMTScaleLibrary extends Library {
        CallMTScaleLibrary INSTANCE = Native.load("C:\\Users\\admin\\Desktop\\cv-camera-index-master\\cv-camera-index-master\\lib\\CvCameraIndex_x64", CallMTScaleLibrary.class);
             //public int Transfer_Ethernet_EX(String filename, int key);
             public  int getCameraIndex(String hwid) ;        
        }
    public static void main(String[] args) {
        
        int n = CallMTScaleLibrary.INSTANCE.getCameraIndex("vid_0BC8&pid_5880");
        System.out.println("相机编号:"+n);
    }
}

In this way, we have realized that Java uses opencv to call the specified USB camera

Thanks for reading, leave a free like

Guess you like

Origin blog.csdn.net/JavaLLU/article/details/128615072