I. Overview
QR code (Quick Response Code) is a type of two-dimensional code, which encodes data by encoding binary bits through black and white marks in a square two-digit matrix. It was first invented for tracking parts in the Japanese automobile manufacturing industry.
Two, function
detect()
Function:
Detects a QR code in an image and obtains a quadrilateral containing the code.
If the detection fails, or there are multiple QR codes in the image, the return value is false.
bool cv::QRCodeDetector::detect
(
InputArray img,
OutputArray points
)
img |
Load image, grayscale or color (BGR) |
points |
Get the vertex coordinates of the smallest quadrilateral of the QR code |
decode()
Function:
Detect the QR code through detect(), and then call decode() to decode it.
Returns the UTF8-encoded output string or the empty string if it cannot be decoded.
std::string cv::QRCodeDetector::decode
(
InputArray img,
InputArray points,
OutputArray straight_qrcode = noArray()
)
img |
Grayscale or color (BGR) image containing the QR code. |
points |
The quadrilateral vertices found by the decode() method (or other algorithm). |
straight_qrcode |
Obtain rectified and binarized QR code image. |
detectAndDecode()
Function:
Detect and decode QR codes.
std::string cv::QRCodeDetector::detectAndDecode
(
InputArray img,
OutputArray points = noArray(),
OutputArray straight_qrcode = noArray()
)
img |
Grayscale or color (BGR) image containing the QR code. |
points |
Gets the vertex coordinates of the smallest quadrilateral of the QR code; null if not found. |
straight_qrcode |
Obtain rectified and binarized QR code image. |
3. Test code
#include "widget.h"
#include "ui_widget.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect.hpp>
#include <iostream>
#include <QDebug>
using namespace cv;
using namespace std;
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//载入图像
Mat src = imread("./photo/ma3.jpg",0);
//显示图像
imshow("src",src);
//创建检测器
QRCodeDetector detector;
//检测标识
bool sign;
//二维码最小四边形的顶点坐标
vector<Point> points;
//检测二维码
sign = detector.detect(src,points);
//判断检测是否成功,如果成功,打印顶点坐标
if(sign)
{
for (unsigned int i = 0;i<points.size();i++)
{
qDebug() << points[i].x;
qDebug() << points[i].y;
}
}
else
{
qDebug() << "检测失败!";
}
//解码
string infomation1;
Mat dst;
infomation1 = detector.decode(src,points,dst);
imshow("dst",dst);
//打印解码信息
QString str1 = QString::fromStdString(infomation1);
qDebug() << str1;
//第二种方法:
string infomation2;
vector<Point> pt;
infomation2 = detector.detectAndDecode(src,pt);
//打印解码信息
QString str2 = QString::fromStdString(infomation2);
qDebug() << str2;
}
Widget::~Widget()
{
delete ui;
}
4. Test results
5. Reference
Two-dimensional code recognition of Opencv---QRCodeDetector