我正在尝试打开网络摄像头并使用 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
然后,在项目的build设置中,我通过使用完整路径引用了我刚刚编写的文件,如下图所示:
我确定路径是正确的,因为我拖放了文件本身,但编译器继续显示相同的错误并退出执行。
最佳答案
最后我找到了解决方案;这些是我遵循的步骤:
- 在 Project Navigator(在 Xcode IDE 的左侧)中右键单击项目 -> New File -> Property file
- 调用文件“Info.plist”并将其保存在与 main.cpp 相同的目录中(它应该也可以在上层目录中工作,但这对我有用)如图所示以下:
- 选择 Info.plist 文件并根据上述问题中的说明进行编辑。
- 现在我们需要将 Info.plist 链接到项目,所以左键单击项目导航器中的项目,选择选项卡常规,然后在左 Pane (“项目和目标列表”)上单击“目标”部分。您应该能够看到一个按钮,上面写着“选择 Info.plist 文件”,请参见下图:
我注意到该程序尚未直接从 Xcode IDE 启动,但我能够(在 Finder 中)导航到可执行文件所在的目录并使用终端运行该程序,因此我复制粘贴了按照建议将 Info.plist 放入该文件夹中 here
关于c++ - 使用 OpenCV (C++) 在 Xcode 中访问网络摄像头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53190412/