#include #include "zbar.h"
int main(int argc, char** argv){
if(argc < 2){
std::cout << "Usage: ./test_zbar barcode.png" << std::endl;
return -1;
}
cv::Mat src = cv::imread(argv[1], 1);
cv::Mat gray;
if (src.channels() == 1) gray = src;
else cv::cvtColor(src, gray, CV_RGB2GRAY);
int width = gray.cols;
int height = gray.rows;
// create a reader
zbar::ImageScanner scanner;
// configure the reader
scanner.set_config(zbar::ZBAR_NONE, zbar::ZBAR_CFG_ENABLE, 1);
unsigned char *pdata = (unsigned char *)gray.data;
zbar::Image imageZbar(width, height, "Y800", pdata, width * height);
int n = scanner.scan(imageZbar);
if (n > 0){
// extract results
for (zbar::Image::SymbolIterator symbol = imageZbar.symbol_begin();
symbol != imageZbar.symbol_end();
++symbol) {
// do something useful with results
std::string decodedFmt = symbol->get_type_name();
std::string symbolData = symbol->get_data();
cv::putText(src, decodedFmt, cv::Point(10, 20), cv::FONT_HERSHEY_PLAIN, 1.2, cv::Scalar(255, 0, 0), 1);
cv::putText(src, symbolData, cv::Point(10, 60), cv::FONT_HERSHEY_PLAIN, 1.2, cv::Scalar(255, 0, 0), 1);
std::cout << decodedFmt << std::endl;
std::cout << symbolData << std::endl;
}
}else{
char error_str[64];
sprintf(error_str, "Not got a barcode!");
cv::putText(src, error_str,
cv::Point(10, 20),
cv::FONT_HERSHEY_PLAIN, 1.2, cv::Scalar(0, 0, 255), 1);
std::cout << "Not got a barcode!" << std::endl;
}
cv::imwrite("result.png", src);
// clean up
imageZbar.set_data(NULL, 0);
}