簡體   English   中英

使用OpenCV(C ++)訪問Xcode中的網絡攝像頭

[英]Accessing webcam in Xcode with OpenCV (C++)

我正在嘗試打開網絡攝像頭並使用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

然后,在項目的構建設置中,我使用完整路徑引用了剛剛編寫的文件,如下圖所示:

Info.plist文件的路徑

我確信路徑是正確的,因為我拖放文件本身,但編譯器繼續顯示相同的錯誤並退出執行。

我最終找到了解決方案; 這些是我遵循的步驟:

  • 在Project Navigator中(在Xcode IDE的左側),右鍵單擊項目 - > New File - > Property file
  • 調用文件“Info.plist”並將其保存在main.cpp所在的同一目錄中(它也應該在上層目錄中工作,但這對我有用),如下圖所示: Info.plist生成
  • 選擇Info.plist文件並根據上述問題中的說明進行編輯。
  • 現在我們需要將Info.plist鏈接到項目,因此在Project Navigator中左鍵單擊項目,選擇選項卡General,在左側窗格(“project and target list”)上,單擊“TARGET”下的可執行文件部分。 您應該能夠看到一個指示“選擇Info.plist文件”的按鈕,請參見下圖: 鏈接到Info.plist文件

我注意到該程序尚未從Xcode IDE直接啟動,但我能夠(在Finder中)導航到可執行文件所在的目錄並使用終端運行程序,我復制粘貼了Info.plist進入該文件夾,如此處所示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM