一维码Codabar:由4条黑色线条,3条白色线条,合计7条线条所组成,每一个字元与字元之间有一间隙Gap做区隔。

条形码Codabar包含21个字元:

(1)、10个数字0~9;

(2)、”+”, ”-”,”*”, ”/”, ”$”, .”, ”:”等7个特殊符号;

(3)、A、B、C、D四个英文字母。

Codabar编码方式与125码及Code 39码相同,只有二种粗细比例。

Codabar其起始码/结束码有4*4=16种组合。

Codabar一般应用于物料管理、图书馆、血站和当前的机场包裹发送中,空白区比窄条宽10,非连续性条形码,每个字符表示为4条3空。

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

#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 <zxing/oned/CodaBarReader.h>

#include <opencv2/opencv.hpp>

#include "zxing/MatSource.h"

int test_Codabar_decode()
{
	std::string image_name = "E:/GitCode/BarCode_Test/test_images/Codabar.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::oned::CodaBarReader);

	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::CODABAR_HINT)));

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

测试图像如下:

一维码Codabar简介及其解码实现(zxing-cpp)-LMLPHP

测试结果如下:

一维码Codabar简介及其解码实现(zxing-cpp)-LMLPHP

GitHubhttps://github.com/fengbingchun/Barcode_Test

05-11 20:07