我正在尝试打开网络摄像头并使用OpenCV显示一个短暂的捕获。我目前正在使用C ++语言编写Xcode。
代码非常简单:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main(int argc, const char * argv[]) {
// variable initialization
Mat frame;
VideoCapture cap; // 0 is the webcam
// try to open camera
cap.open(0);
if (!cap.isOpened()) return -1; // check if there is no video or no way to display it
// create a window and display the video capture
namedWindow("Video Capture", CV_WINDOW_AUTOSIZE);
for (int i=0; i<100; i++) {
cap >> frame;
imshow("Video Capture", frame);
waitKey(1);
}
return 0;
}
当我运行代码时,返回以下错误:
[access] This app has crashed because it attempted to access privacy-sensitive
data without a usage description. The app's Info.plist must contain an
NSCameraUsageDescription key with a string value explaining to the
user how the app uses this data.
因此,我向项目添加了一个Info.plist文件(当前位于main.cpp所在的目录中),并添加了编译器建议的描述:
Key: Privacy - Camera Usage Description
Value: $(PRODUCT_NAME) camera use
然后,在项目的构建设置中,我使用完整路径引用了刚刚编写的文件,如下图所示:
我确信路径是正确的,因为我拖放文件本身,但编译器继续显示相同的错误并退出执行。
我最终找到了解决方案;这些是我遵循的步骤:
我注意到该程序尚未从Xcode IDE直接启动,但我能够(在Finder中)导航到可执行文件所在的目录并使用终端运行程序,我复制粘贴了Info.plist进入该文件夹,正如here建议的那样