OpenCV
QR Code detect and decode - QR 코드 리더
J-sean
2019. 12. 15. 11:38
반응형
It shows how to detect and decode a QR Code.
OpenCV에서 QR코드를 감지하고 내용을 확인할 수 있다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #include <Windows.h> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; int main() { VideoCapture cap(0); if (!cap.isOpened()) { cerr << "Camera open failed." << endl; return -1; } QRCodeDetector detector; Mat frame; vector<Point> points; String msg; while (true) { cap >> frame; if (frame.empty()) { cerr << "Empty frame." << endl; break; } msg = detector.detectAndDecode(frame, points); // Both detects and decodes QR code. if (!msg.empty()) { polylines(frame, points, true, Scalar(0, 0, 255), 2); putText(frame, msg, Point(10, 30), FONT_HERSHEY_PLAIN, 2, Scalar(0, 0, 255), 2); if (msg.substr(0, 4) == "http") { imshow("QR Code", frame); if (MessageBox(NULL, (msg + " is a website address.\nDo you want to visit?").c_str(), "QR Code", MB_YESNO) == IDYES) { ShellExecute(NULL, "open", msg.c_str(), NULL, NULL, SW_SHOW); // Performs an operation on a specified file. } else { continue; } } } else { putText(frame, "No QR code detected", Point(10, 30), FONT_HERSHEY_PLAIN, 2, Scalar(0, 0, 255), 2); } imshow("QR Code", frame); if (waitKey(10) == 27) break; } return 0; } |
Run the program. It says 'No QR code detected' at first.
QR code with a message.
QR code with a website address.
If the first 4 letters of the message are 'http', it opens the website with the default web browser.
반응형