1. 程式人生 > 實用技巧 >Opencv4新屬性-二維碼識別

Opencv4新屬性-二維碼識別

int main()
{
    Mat img = imread("C:\\Users\\24731\\Desktop\\000\\001.jpg");
    if (img.empty())
    {
        cout << "請確認影象檔名稱是否正確" << endl;
        return -1;
    }
    Mat gray, qrcode_bin;
    cvtColor(img, gray, COLOR_BGR2GRAY);
    QRCodeDetector qrcodedetector;
    vector<Point> points;
    
string information; bool isQRcode; isQRcode = qrcodedetector.detect(gray, points); //識別二維碼 if (isQRcode) { //解碼二維碼 information = qrcodedetector.decode(gray, points, qrcode_bin); cout << points << endl; //輸出二維碼四個頂點的座標 } else { cout << "
無法識別二維碼,請確認影象時候含有二維碼" << endl; return -1; } //繪製二維碼的邊框 for (int i = 0; i < points.size(); i++) { if (i == points.size() - 1) { line(img, points[i], points[0], Scalar(0, 0, 255), 2, 8); break; } line(img, points[i], points[i
+ 1], Scalar(0, 0, 255), 2, 8); } //將解碼內容輸出到圖片上 putText(img, information.c_str(), Point(20, 30), 0, 1.0, Scalar(0, 0, 255), 2, 8); //利用函式直接定位二維碼並解碼 string information2; vector<Point> points2; information2 = qrcodedetector.detectAndDecode(gray, points2); cout << points2 << endl; putText(img, information2.c_str(), Point(20, 55), 0, 1.0, Scalar(0, 0, 0), 2, 8); //輸出結果 imshow("result", img); namedWindow("qrcode_bin", WINDOW_NORMAL); imshow("qrcode_bin", qrcode_bin); waitKey(0); return 0; }