Mac macOS 10.14 Mojave Xcode opencv 调用摄像头权限错误 This app has crashed because it attempted to...

Mac macOS 10.14 Mojave Xcode opencv 调用摄像头权限错误 This app has crashed because it attempted to access privacy-sensitive data without a usage description.

我的新博客地址为renzibei.com


更新了macOS 10.14 Mojave之后,使用opencv调用摄像头会产生权限错误,报错This app has crashed because it attempted to access privacy-sensitive data without a usage description.
原因是macOS 10.14加入了对所有程序的隐私权限管理,如果要涉及隐私权限,要在可执行程序旁边加入Info.plist且在里面注册摄像头权限。
在测试开发时,简单起见,只需要从别的程序里复制一个Info.plist文件移动到DerivedData\工程名\Build\Products\Debug目录下,且改动Info.plist,加入摄像头权限。

操作示例

这里从Mac自带程序Photo Booth里复制Info.plist,在应用程序文件夹里找到Photo Booth.app,右键选择显示包内容,文件夹里有一个Info.plist文件,拷贝到DerivedData\工程名\Build\Products\Debug目录下,用Xcode打开并编辑。原内容如下。Mac macOS 10.14 Mojave Xcode opencv 调用摄像头权限错误 This app has crashed because it attempted to..._第1张图片

删去其中的不必要的键值对,最后加入键Privacy - Camera Usage Description,值为YES,修改好后的Info.plist如下,其中cvModel为我的项目名。
Mac macOS 10.14 Mojave Xcode opencv 调用摄像头权限错误 This app has crashed because it attempted to..._第2张图片
然后编译,就能通过啦。
最后附上测试代码

//
//  main.cpp
//  cvModel
//
//  Created by Renzibei on 10/9/18.
//  Copyright © 2018 GF. All rights reserved.
//

#include 
#include 

using namespace cv;

int main(int argc, const char * argv[]) {
    VideoCapture cam(0);
    Mat image;
    cam >> image;
    imshow("test", image);
    waitKey(0);
    return 0;
}

你可能感兴趣的:(cv)