How to get a list video capture devices NAMES (web

2019-01-14 14:01发布

问题:

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 console output:

1) Asus Web Camera
2) Sony Web Camera

So my question is how to cout such list using Qt C++? (if it is possible I'd love to see how to do it in pure Qt - no extra libs...)


also from this series:

  • How to get a list of video capture devices on linux? and special details on getting cameras NAMES with correct, tested answers
  • How to get a list of video capture devices on Mac OS? with correct, not yet tested by my answers
  • How to get a list of video capture devices on windows? with correct, tested answers
  • How to get a list video capture devices NAMES using Qt (crossplatform)?

回答1:

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;
}


回答2:

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