How to get a list video capture devices NAMES (web cameras) using Qt (crossplatform)? (C++)

后端 未结 2 2073
误落风尘
误落风尘 2020-12-05 21:45

So all I need is simple - a list of currently avaliable video capture devices (web cameras). I need it in simple C++ Qt console app. By list I mean something like such conso

相关标签:
2条回答
  • 2020-12-05 22:32

    I've wrote the following code to list all the USB capture devices. Remember to include webcam.h and libwebcam.h and link your code to libwecam using -lwebcam.

    bool QextCamera::listAvailableDevices(QStringList * captureDeviceList){
        CResult ret;
        CDevice *devices = NULL;
    
        quint32 req_size = 0;
        quint32 buffer_size = 0;
        quint32 count = 0;
        QStringList availableDevices;
    
        c_init();
    
        do {
            if (devices){
            free(devices);
        }
    
        if(req_size){
            devices = (CDevice *)malloc(req_size);
    
            if(devices == NULL){
                    // LOG ERROR...
            return false;
            }
    
            buffer_size = req_size;
       }
    
       // Try to enumerate. If the buffer is not large enough, the required size is returned.
       ret = c_enum_devices(devices, &req_size, &count);
    
       if(ret != C_SUCCESS && ret != C_BUFFER_TOO_SMALL){
           // LOG ERROR...
           return false;
       }
    
        } while(buffer_size < req_size);
    
        if(count == 0) {
            // LOG ERROR...
        return false;
        }
    
        for(quint32 i = 0; i < count; i++) {
            CDevice *device = &devices[i];
        availableDevices << QString("%1 : %2 : %3").arg(device->shortName).arg(device->driver).arg(device->location);
        }
    
        if(devices){
            free(devices);
        }
    
        c_cleanup();
    
        *captureDeviceList = availableDevices;
    
        return true;
    }
    
    0 讨论(0)
  • 2020-12-05 22:33

    I used this example code to list the cameras and get some info about them.

    #include <QtMultimedia/QCameraInfo>
    
    QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
    foreach (const QCameraInfo &cameraInfo, cameras) {
        qDebug() << "Name: " << cameraInfo.deviceName();
        qDebug() << "Position: " << cameraInfo.position();
        qDebug() << "Orientation: " << cameraInfo.orientation();
    }
    

    remember to include in pro file:

    QT += multimedia
    
    0 讨论(0)
提交回复
热议问题