One-dimensional code Code 128 introduction and its decoding implementation (zxing-cpp)

One-dimensional code Code 128: Launched in 1981, it is a variable-length, continuous alphanumeric barcode. Compared with other one-dimensional barcodes, it is relatively complex, supports more characters, and has different coding methods for interactive use, so its application flexibility is also greater.

Code 128 Features:

1. It has three different encoding types: A, B, and C, which can provide the encoding of 128 characters in standard ASCII;

2. Allow bidirectional scanning;

3. You can decide whether to add a check digit;

4. The length of the barcode is adjustable, but including the start bit and the end bit, it cannot exceed 232 characters;

5. The same 128 code can be exchanged by three different encoding rules of A, B, and C, which can not only expand the range of character selection, but also shorten the length of the encoding.

The coding range of each coding method of Code 128:

1. Code 128 A: Standard numbers and letters, control characters, special characters;

2. Code 128 B: Standard numbers and letters, lowercase letters, special characters;

3. Code 128 C/EAN 128: The set of number pairs of [00]-[99], a total of 100, that is, only numbers of even digit length can be represented.

Code 128 coding rules: start bit + [FNC1 (add when EAN 128 code)] + data bit + check bit + end bit.

Code128 check bit calculation: (ID value corresponding to the start bit + position of each bit of data in the entire data * ID value corresponding to each bit of data) % 103.

The following is the test code for decoding the one-dimensional code Code 128 implemented by the zxing-cpp open source library:

#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 <zxing/oned/Code39Reader.h>
#include <zxing/oned/Code93Reader.h>
#include <zxing/oned/Code128Reader.h>

#include <opencv2/opencv.hpp>

#include "zxing/MatSource.h"

int test_Code128_decode()
{
	std::string image_name = "E:/GitCode/BarCode_Test/test_images/Code128.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::Code128Reader);

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

	std::string txt = "E:/GitCode/BarCode_Test/test_images/Code128.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;
}
The test image is as follows:


The test results are as follows:


GitHubhttps://github.com/fengbingchun/Barcode_Test

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324819896&siteId=291194637