UPC(Universal Product Code)码是最早大规模应用的条码,其特性是一种长度固定、连续性的条 码,目前主要在美国和加拿大使用,由于其应用范围广泛,故又被称万用条码。 UPC码仅可用来表示数字,故其字码集为数字0~9。UPC码共有A、B、C、D、E等五种版本。
UPC A码又称UPC标准码,它的结构如下图:
每个UPC A码包含部分如下图:
UPC A码具有以下特点:
1. 每个字码皆由7个模组组合成2线条2空白,其逻辑值可用7个二进制数字表示,例如逻辑值0001101代表数字1,逻辑值0为空白,1为线条,故数字1的UPC-A码为粗空白(000)-粗线条(11)-细空白(0)-细线条(1)。
2. 从空白区开始共113个模组,每个模组长0.33mm,条码符号长度为37.29mm。
3. 中间码两侧的资料码编码规则是不同的,左侧为奇,右侧为偶。奇表示线条的个数为奇数;偶表示线条的个数为偶数。
4. 起始码、终止码、中间码的线条高度长於数字码。
以下是通过zxing-cpp开源库实现的对一维码UPC A进行解码的测试代码:
#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 <zxing/oned/UPCAReader.h> #include <opencv2/opencv.hpp> #include "zxing/MatSource.h" int test_UPC_A_decode() { std::string image_name = "E:/GitCode/BarCode_Test/test_images/UPC_A.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::UPCAReader); 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::UPC_A_HINT))); std::string txt = "E:/GitCode/BarCode_Test/test_images/UPC_A.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; }
测试图像如下:
测试结果如下: