读取二维码图片
灰度处理
二值化、滤波
二维码读取
读取结果
类型:QR-Code
条码: http://v.dongyu315.com/q.aspx?c=5629060261940201
代码
Mat srcImg = img.clone();
imshow("原图", srcImg);
cvtColor(img, img, CV_BGR2GRAY);
imshow("灰度图", img);
int thre = threshold(img, img, 0, 255, cv::THRESH_OTSU);
imshow("二值化", img);
ImageScanner scanner;
scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
int width1 = img.cols;
int height1 = img.rows;
uchar *raw = (uchar *)img.data;
Image imageZbar(width1, height1, "Y800", raw, width1 * height1);
scanner.scan(imageZbar); //扫描条码
Image::SymbolIterator symbol = imageZbar.symbol_begin();
if (imageZbar.symbol_begin() == imageZbar.symbol_end())
{
cout << "查询条码失败,请检查图片!" << endl;
}
for (; symbol != imageZbar.symbol_end(); ++symbol)
{
qDebug() << "类型:" << symbol->get_type_name().c_str();
qDebug() << "条码:" << symbol->get_data().c_str();
for (int i = 0; i < symbol->get_location_size(); i++)
{
cv::Point2f start, end;
start.x = symbol->get_location_x(i%4);
start.y = symbol->get_location_y(i%4);
end.x = symbol->get_location_x((i+1) % 4);
end.y = symbol->get_location_y((i+1) % 4);
qDebug() << "X"<<i<<":" << symbol->get_location_x(i) << "Y"<<i<<":" << symbol->get_location_y(i);
line(srcImg, start, end, Scalar(255, 21, 237), 3);
}
}
imshow("result", srcImg);