Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

the QCameraInfo::availableCameras() method not work when deploy the mac app



  • Hi:
    I developed a mac app use qt , my app needs to get the camera which belongs to the mac, when I debug my project, everything works fine that I get the camera which belongs to my mac , then I need to deploy the app, so I add the libqcocoa.dylib to the .app/Contents/PlugIns/platforms/ directory, and add the code to the main.cpp:

           QDir dir(QCoreApplication::applicationDirPath());
           dir.cdUp();
           dir.cd("PlugIns");
           QCoreApplication::setLibraryPaths(QStringList(dir.absolutePath()));
    

    and now I can deploy my app, my app can work on other mac, but I can't get the camera list, the QCameraInfo::availableCameras() method return result is 0 what is not I expect, so I change
    the code of main.cpp to :

           QDir dir(QCoreApplication::applicationDirPath());
           dir.cdUp();
           dir.cd("PlugIns");
       // QCoreApplication::setLibraryPaths(QStringList(dir.absolutePath()));
    

    now I can get the camera list, it makes me confused, can you give me some reason about this situation, if I want to deploy my app and let the QCameraInfo::availableCameras() method work, what should I do?

    thanks a lot!


  • Lifetime Qt Champion

    @Princein said in the QCameraInfo::availableCameras() method not work when deploy the mac app:

    PlugIns

    is this really the directory name? As far as I know MacOS filesystem is case sensitive.


  • Lifetime Qt Champion

    Hi,

    On what version of macOS do you have that happening ?



  • @jsulm
    I think PlugIns is right, you can fine some information like this:

    
    Table 3-1  Standard locations for code inside a bundle
    Location
    Description
    Contents
    Top content directory of the bundle
    Contents/MacOS
    Helper apps and tools
    Contents/Frameworks
    Frameworks, dylibs
    Contents/PlugIns
    Plug-ins, both loadable and extensions
    Contents/XPCServices
    XPC services
    Contents/Helpers
    Helper apps and tools
    Contents/Library/Automator
    Automator actions
    Contents/Library/Spotlight
    Spotlight importers
    Contents/Library/LoginItems
    Installable login items
    Contents/Library/LaunchServices
    Privileged helper tools installed by the ServiceManagement framework
    
    

    in https://developer.apple.com/library/archive/documentation/Security/Conceptual/CodeSigningGuide/Procedures/Procedures.html#//apple_ref/doc/uid/TP40005929-CH4-SW2



  • @SGaist macOS Mojave Version 10.14.2 (18C54)


  • Lifetime Qt Champion

    Did you add the NSCameraUsageDescription to your Info.plist file ?



  • @SGaist yes, it seems is a mac app requesting Authorization question, so I add the ‘NSCameraUsageDescription’ to my app Info.plist by add code to my app.pro file:

    QMAKE_POST_LINK += plutil -replace NSCameraUsageDescription -string \"My usage description.\" $$DESTDIR/$${TARGET}.app/Contents/Info.plist
    
    

    and after generating the app, the 'NSCameraUsageDescription' key is in my app Info.plist which just as I want, when I rerun the app, but the app does not do anything, which I want is like
    https://docs-assets.developer.apple.com/published/663cb677e7/3d0e288c-9d77-4523-b1ec-bf42bca85cf1.png in the link :
    https://developer.apple.com/documentation/avfoundation/cameras_and_media_capture/requesting_authorization_for_media_capture_on_macos

    In macOS 10.14 and later, the user must explicitly grant permission for each app to access cameras and microphones. Before your app can use the capture system for the first time, macOS shows an alert asking the user to grant your app access to the camera, as shown below. macOS remembers the user’s response to this alert, so subsequent uses of the capture system don’t cause it to appear again.

    in the https://developer.apple.com/documentation/avfoundation/cameras_and_media_capture/requesting_authorization_for_media_capture_on_macos, I know if I want to show the 'get aceess' alert,I should use the AVCaptureDevice authorizationStatus(for:) method in OC or Swift :

    Verify and Request Authorization for Capture
    Always test the AVCaptureDevice authorizationStatus(for:) method before setting up a capture session. If the user has not yet granted or denied capture permission, the authorization status is AVAuthorizationStatus.notDetermined. In this case, use the requestAccess(for:completionHandler:) method to have macOS prompt the user:

    switch AVCaptureDevice.authorizationStatus(for: .video) {
        case .authorized: // The user has previously granted access to the camera.
            self.setupCaptureSession()
        
        case .notDetermined: // The user has not yet been asked for camera access.
            AVCaptureDevice.requestAccess(for: .video) { granted in
                if granted {
                    self.setupCaptureSession()
                }
            }
        
        case .denied: // The user has previously denied access.
            return
        case .restricted: // The user can't grant access due to restrictions.
            return
    }
    

    but my mac project is Qt project, what should I do If I want to use the mac camera when deploying my app to other mac, thanks a lot!



  • @Princein it seems the QCameraInfo::availableCameras() method has bug :

    https://bugs.launchpad.net/canonical-developer-experience/+bug/1438072

    what should I do, if I want to get the camera list in mac in c++?


  • Lifetime Qt Champion

    @Princein This bug seems only to affect Linux


  • Moderators

    @Princein I think this is similar to an issue we had recently on this forum, can't quite find it atm, where the issue is with QtCreator.

    QtCreator would also need to request camera permission, has your app, when started from QtC is a child process of QtC.

    IIRC



  • now I develope a simple Qt project which named 'task2' on mac for test the QCameraInfo::availableCameras() method, I add the libqcocoa.dylib to the .app/Contents/PlugIns/platforms/ directory, and here is main.cpp in task2:

        QApplication a(argc, argv);
        QDir dir(QCoreApplication::applicationDirPath());
     //  dir.cdUp();
     //  dir.cd("PlugIns");
     //  dir.cd("platforms");
     //  QCoreApplication::setLibraryPaths(QStringList(dir.absolutePath()));
        MainWindow w;
        w.show();
    
        return a.exec();
    

    I don't use the QCoreApplication::setLibraryPaths(QStringList(dir.absolutePath())); method now,it can deploy too, in myself mac, the QCameraInfo::availableCameras() method can return my camera on my mac which just as I expect,but when I deploy the task2 app to other mac which MacOS version is 10.13.6, the QCameraInfo::availableCameras() method can't return the camera on that mac, I really need the camera info on the mac, it seems a platform specific problem that can't use the OpenCV library get it, here is my code that gets the camera list in task2 project:

    
    void MainWindow::getCamera(){
        const QList<QCameraInfo> availableCameras = QCameraInfo::availableCameras();
        for (const QCameraInfo &cameraInfo : availableCameras) {
           QString deviceInfoStr = cameraInfo.description();
           ui->label->setText(deviceInfoStr);
           cout << "deviceInfoStr:   " << deviceInfoStr.toStdString() << endl;
         }
           QCameraInfo defaultCameraInfo = QCameraInfo::defaultCamera();
           QString defaultCameraInfoStr = defaultCameraInfo.description();
           cout << "defaultCameraInfoInfoStr:   " << defaultCameraInfoStr.toStdString() << endl;
           ui->label_2->setText(defaultCameraInfoStr);
    }
    
    

    where is wrong? could you help me, thanks a lot!



  • @Princein my Mac MacOS version is 10.14.2 (18C54)


  • Lifetime Qt Champion

    You should check the bug report system to see if there's already something related to this. If not please open a new report providing a minimal compilable example as well as your findings so that it can be fixed.


Log in to reply