VS2019下OpenCV环境配置
VC++目录的包含目录和库目录
链接器-输入-附加依赖项:
文件在lib下,带d结尾的是debug模式用的
#include<Windows.h>
#include <iostream>
#include<opencv.hpp>
#include<string.h>
using namespace cv;
using namespace std;//string和cout
int main()
{
bool yn_t = false;//是否检测到二维码
cv::Mat img = imread("test0.bmp");
cv::QRCodeDetector QRdetecter;
std::vector<cv::Point> list;//list:包括二维码的最小四边形的点集
cv::Mat res;//提取出的二维码图像
//QRdetecter.setEpsX(0.5);
//QRdetecter.setEpsY(0.5);
yn_t = QRdetecter.detect(img, list);//返回bool值为是否检测到二维码
cout << "是否检测到二维码:"<<yn_t << endl;
if (yn_t)//如果检测到存在二维码
{
string txt = QRdetecter.detectAndDecode(img, list, res);//返回解码后的字符串
cout << "解码内容:" << txt << endl;
for (int i = 0; i < list.size(); i++)//绘制二维码线框
{
if (i == 3)
line(img, list[i], list[0], Scalar(0, 255, 0), 3);
else
line(img, list[i], list[i + 1], Scalar(0, 255, 0), 3);
}
namedWindow("提取出二维码", 0);//可以拖动窗口大小
resizeWindow("提取出二维码", 640, 480);//设置窗口大小
moveWindow("提取出二维码", 1200, 500);//设置窗口位置
imshow("输入图像", img);
imshow("提取出二维码", res);
waitKey(0);
}
return 0;
}
运行:
差评:如果二维码角度倾斜,无法提取图像,输出会报错
但不影响检测和定位及解码