#include <opencv2/opencv.hpp>
#include <zbar.h>
#include <stdio.h>
int main() {
// Initialize OpenCV capture object to capture from default camera (0)
cv::VideoCapture cap(0);
if (!cap.isOpened()) {
printf("Error: Could not open the camera.\n");
return -1;
}
// Create an OpenCV Mat object to hold the captured frame
cv::Mat frame;
// Initialize ZBar scanner
zbar::ImageScanner scanner;
scanner.set_config(zbar::ZBAR_NONE, zbar::ZBAR_CFG_ENABLE, 1);
while (true) {
// Capture frame from the camera
cap >> frame;
if (frame.empty()) {
printf("Error: Could not capture frame.\n");
break;
}
// Convert the frame to grayscale
cv::Mat gray;
cv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY);
// Wrap OpenCV image data in a ZBar image
zbar::Image zbarImage(gray.cols, gray.rows, "Y800", gray.data, gray.cols * gray.rows);
// Scan the image for QR codes
int n = scanner.scan(zbarImage);
if (n > 0) {
// Extract results
for (zbar::Image::SymbolIterator symbol = zbarImage.symbol_begin(); symbol != zbarImage.symbol_end(); ++symbol) {
printf("QR Code detected: %s\n", symbol->get_data().c_str());
}
}
// Display the frame
cv::imshow("QR Code Scanner", frame);
// Exit on key press
if (cv::waitKey(30) >= 0) break;
}
// Release the camera
cap.release();
return 0;
}