一维码ITF 25又称交插25条码,常用在序号,外箱编号等应用。交插25码是一种条和空都表示信息的条码,交插25码有两种单元宽度,每一个条码字符由五个单元组成,其中二个宽单元,三个窄单元。在一个交插25码符号中,组成条码符号的字符个数为偶数,当字符是奇数个时,应在左侧补0变为偶数。条码字符从左到右,奇数位置字符用条表示,偶数位字符用空表示。交插25码的字符集包括数字0到9.
以下是通过zxing-cpp开源库实现的对一维码ITF 25进行解码的测试代码:
#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 <zxing/oned/EAN8Reader.h>
#include <zxing/oned/EAN13Reader.h>
#include <zxing/oned/ITFReader.h>
#include <opencv2/opencv.hpp>
#include "zxing/MatSource.h"
int test_ITF25_decode()
{
std::string image_name = "E:/GitCode/BarCode_Test/test_images/ITF25.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::ITFReader);
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::ITF_HINT)));
std::string txt = "E:/GitCode/BarCode_Test/test_images/ITF25.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;
}
测试图像如下:
测试结果如下: