一维码Code 93简介及其解码实现(zxing-cpp)

一维码Code 93: Code 93码与Code 39码的字符集相同,但93码的密度要比39码高,因而在面积不足的情况下,可以用93码代替39码。它没有自校验功能,为了确保数据安全性,采用了双校验字符,其可靠性比39条码还要高.

一维码Code 39的介绍可以参考:http://blog.csdn.net/fengbingchun/article/details/54587385

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

#include "funset.hpp"
#include 
#include 
#include 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 

#include "zxing/MatSource.h"

int test_Code93_decode()
{
	std::string image_name = "E:/GitCode/BarCode_Test/test_images/Code93.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 source = MatSource::create(matGray);
	int width = source->getWidth();
	int height = source->getHeight();
	fprintf(stderr, "image width: %d, height: %d\n", width, height);

	zxing::Ref reader;
	reader.reset(new zxing::oned::Code93Reader);

	zxing::Ref binarizer(new zxing::GlobalHistogramBinarizer(source));
	zxing::Ref bitmap(new zxing::BinaryBitmap(binarizer));
	zxing::Ref result(reader->decode(bitmap, zxing::DecodeHints(zxing::DecodeHints::CODE_93_HINT)));

	std::string txt = "E:/GitCode/BarCode_Test/test_images/Code93.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;

}
测试图像如下:

一维码Code 93简介及其解码实现(zxing-cpp)_第1张图片

测试结果如下:

一维码Code 93简介及其解码实现(zxing-cpp)_第2张图片
GitHub: https://github.com/fengbingchun/Barcode_Test

你可能感兴趣的:(Bar,Code)