#include <iostream>
#include <zbar.h>
#include <opencv2/opencv.hpp>

int main(int argc, char* argv[]) {
    if (argc != 2) {
        std::cerr << "Usage: " << argv[0] << " <image_path>" << std::endl;
        return -1;
    }

    std::string image_path = argv[1];
    cv::Mat image = cv::imread(image_path, cv::IMREAD_GRAYSCALE);
    if (image.empty()) {
        std::cerr << "Could not open or find the image: " << image_path << std::endl;
        return -1;
    }

    // Initialize ZBar scanner
    zbar::ImageScanner scanner;
    scanner.set_config(zbar::ZBAR_NONE, zbar::ZBAR_CFG_ENABLE, 1);

    // Wrap OpenCV image data in a ZBar image
    zbar::Image zbar_image(image.cols, image.rows, "Y800", image.data, image.cols * image.rows);

    // Scan the image for barcodes and QR codes
    int n = scanner.scan(zbar_image);
    if (n > 0) {
        for (auto symbol = zbar_image.symbol_begin(); symbol != zbar_image.symbol_end(); ++symbol) {
            std::cout << "Decoded " << symbol->get_type_name() << " symbol \"" << symbol->get_data() << '"' << std::endl;
        }
    } else {
        std::cout << "QR Code not detected" << std::endl;
    }

    return 0;
}