二维码QR Code简介及其解码实现(zxing-cpp)

二维码QR Code(Quick Response Code)是由Denso公司于1994年9月研制的一种矩阵二维码符号,它具有一维条码及其它二维条码所具有的信息容量大、可靠性高、可表示汉字及图象多种文字信息、保密防伪性强等优点。

二维码QR Code呈正方形,常见的是黑白两色。在3个角落,印有较小,像”回”字的的正方图案。这3个是帮助解码软件定位的图案,用户不需要对准,无论以任何角度扫描,数据仍可正确被读取。

由于QR Code码用特定的数据压缩模式表示汉字,它仅用13 bit可表示一个汉字,而PDF417、Data Martix等二维码没有特定的汉字表示模式,因此仅用字节表示模式来表示汉字,在用字节模式表示汉字时,需用16 bit(二个字节)表示一个汉字,因此QR Code码比其它的二维条码表示汉字的效率提高了20%。

QR Code码主要特点:

1、符号规格从版本1(21*21模块)到版本40(177*177模块),每提高一个版本,每边增加4个模块。

2、数据类型与容量(参照最大规格符号版本40-L级):

(1)、数字数据:7,089个字符;

(2)、字母数据: 4,296个字符;

(3)、8位字节数据: 2,953个字符;

(4)、汉字数据:1,817个字符。

3、数据表示方法:深色模块表示二进制"1",浅色模块表示二进制"0"。

4、纠错能力:

(1)、L级:约可纠错7%的数据码字;

(2)、M级:约可纠错15%的数据码字;

(3)、Q级:约可纠错25%的数据码字;

(4)、H级:约可纠错30%的数据码字。

5、结构链接(可选):可用1-16个QR Code码符号表示一组信息。每一符号表示100个字符的信息。

以下是通过zxing-cpp开源库实现的对二维码QR Code进行解码的测试代码:

#include "funset.hpp"
#include <string>
#include <fstream>
#include <Windows.h>

#include <zxing/LuminanceSource.h>
#include <zxing/common/Counted.h>
#include <zxing/Reader.h>
#include <zxing/aztec/AztecReader.h>
#include <zxing/common/GlobalHistogramBinarizer.h>
#include <zxing/DecodeHints.h>
#include <zxing/datamatrix/DataMatrixReader.h>
#include <zxing/MultiFormatReader.h>
#include <zxing/pdf417/PDF417Reader.h>
#include <zxing/qrcode/QRCodeReader.h>

#include <opencv2/opencv.hpp>

#include "zxing/MatSource.h"

int test_QRCode_decode()
{
	std::string image_name = "E:/GitCode/BarCode_Test/test_images/QRCode.png";
	cv::Mat matSrc = cv::imread(image_name, 1);
	if (!matSrc.data) {
		fprintf(stderr, "read image error: %s", image_name.c_str());
		return -1;
	}

	cv::Mat matGray;
	cv::cvtColor(matSrc, matGray, CV_BGR2GRAY);

	zxing::Ref<zxing::LuminanceSource> source = MatSource::create(matGray);
	int width = source->getWidth();
	int height = source->getHeight();
	fprintf(stderr, "image width: %d, height: %d\n", width, height);

	zxing::Ref<zxing::Reader> reader;
	reader.reset(new zxing::qrcode::QRCodeReader);

	zxing::Ref<zxing::Binarizer> binarizer(new zxing::GlobalHistogramBinarizer(source));
	zxing::Ref<zxing::BinaryBitmap> bitmap(new zxing::BinaryBitmap(binarizer));
	zxing::Ref<zxing::Result> result(reader->decode(bitmap, zxing::DecodeHints(zxing::DecodeHints::QR_CODE_HINT)));

	std::string txt = "E:/GitCode/BarCode_Test/test_images/QRCode.txt";
	std::ifstream in(txt);
	if (!in.is_open()) {
		fprintf(stderr, "fail to open file: %s\n", txt.c_str());
		return -1;
	}

	std::string str1;
	std::getline(in, str1);
	fprintf(stderr, "actual        result: %s\n", str1.c_str());
	std::string str2 = result->getText()->getText();
	fprintf(stdout, "recognization result: %s\n", str2.c_str());

	if (str1.compare(str2) == 0) {
		fprintf(stderr, "=====  recognition is correct  =====\n");
	}
	else {
		fprintf(stderr, "=====  recognition is wrong =====\n");
		return -1;
	}

	in.close();

	return 0;
}
测试图像如下:


测试结果如下:

  • 0
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 29
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 29
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值