二维码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;
}
測試圖像如下:
測試結果如下:
總結
以上是生活随笔為你收集整理的二维码QR Code简介及其解码实现(zxing-cpp)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 二维码Data Matrix的解码实现(
- 下一篇: 一维码Codabar简介及其解码实现(z