UPC(Universal Product Code)码是最早大规模应用的条码,其特性是一种长度固定、连续性的条  码,目前主要在美国和加拿大使用,由于其应用范围广泛,故又被称万用条码。 UPC码仅可用来表示数字,故其字码集为数字0~9。UPC码共有A、B、C、D、E等五种版本。

UPC E码又称UPC缩短码。

UPC E码是UPC A码的简化型式,其编码方式是将UPC A码整体压缩成短码以方便使用,因此其编码形式须经由UPC A码来转换。

UPC E由6位数码与左右护线组成,无中间线。6位数字码的排列为3奇3偶,其排列方法取决于检查码的值。UPC-E码只用于国别码为0的商品。

UPC E码结构如下图:

1.      左护线:为辅助码,不具任何意义,仅供列印时作为识别之用,逻辑型态为010101,其中0代表细白,1代表细黑。

2.      右护线:同UPC A码,逻辑型态为101。

3.      检查码:为UPC A码原形的检查码,其作用为一导入值,并不属於资料码的一部份。

4.      资料码:扣除第一码固定为0外,UPC E实际参与编码的部份只有六码,其编码方式,视检查码的值来决定。

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

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

#include <opencv2/opencv.hpp>

#include "zxing/MatSource.h"

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

	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_E_HINT)));

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

测试图像如下:

测试结果如下:

GitHubhttps://github.com/fengbingchun/Barcode_Test

一维码UPC E简介及其解码实现(zxing-cpp)的更多相关文章

  1. 一维码UPC A简介及其解码实现(zxing-cpp)

    UPC(Universal Product Code)码是最早大规模应用的条码,其特性是一种长度固定.连续性的条  码,目前主要在美国和加拿大使用,由于其应用范围广泛,故又被称万用条码. UPC码仅可 ...

  2. 一维码ITF 25简介及其解码实现(zxing-cpp)

    一维码ITF 25又称交插25条码,常用在序号,外箱编号等应用.交插25码是一种条和空都表示信息的条码,交插25码有两种单元宽度,每一个条码字符由五个单元组成,其中二个宽单元,三个窄单元.在一个交插2 ...

  3. 一维码EAN 13简介及其解码实现(zxing-cpp)

    一维码EAN 13:属于国际标准条码, 由13个数字组成,为EAN的标准编码型式(EAN标准码). 依结构的不同,EAN条码可区分为: 1.  EAN 13码: 由13个数字组成,为EAN的标准编码型 ...

  4. 一维码EAN 8简介及其解码实现(zxing-cpp)

    一维码EAN 8:属于国际标准条码,由8个数字组成,属EAN的简易编码形式(EAN缩短码).当包装面积小于120平方公分以下无法使用标准码时,可以申请使用缩短码. 依结构的不同,EAN条码可区分为: ...

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

    一维码Code 128:1981年推出,是一种长度可变.连续性的字母数字条码.与其他一维条码比较起来,相对较为复杂,支持的字元也相对较多,又有不同的编码方式可供交互运用,因此其应用弹性也较大. Cod ...

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

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

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

    一维码Code 39:由于编制简单.能够对任意长度的数据进行编码.支持设备广泛等特性而被广泛采用. Code 39码特点: 1. 能够对任意长度的数据进行编码,其局限在于印刷品的长度和条码阅读器的识别 ...

  8. (zxing.net)一维码ITF的简介、实现与解码

    一.简介 一维码ITF 25又称交插25条码,常用在序号,外箱编号等应用.交插25码是一种条和空都表示信息的条码,交插25码有两种单元宽度,每一个条码字符由五个单元组成,其中二个宽单元,三个窄单元.在 ...

  9. (zxing.net)一维码Codabar的简介、实现与解码

    一.简介 一维码Codabar:由4条黑色线条,3条白色线条,合计7条线条所组成,每一个字元与字元之间有一间隙Gap做区隔. 条形码Codabar包含21个字元: (1).10个数字0~9; (2). ...

随机推荐

  1. 简单解析Spring核心IOC容器原理

    将大体流程解析了一边,具体可以看源代码一个方法一个方法的跟下 XmlBeanFactory的功能是建立在DefaultListableBeanFactory这个基本容器的基础上的,并在这个基本容器的基 ...

  2. [翻译] CHTCollectionViewWaterfallLayout

    CHTCollectionViewWaterfallLayout https://github.com/chiahsien/CHTCollectionViewWaterfallLayout CHTCo ...

  3. [翻译] DCPathButton

    DCPathButton https://github.com/Tangdixi/DCPathButton DCPathButton 2.0 is a menu button for iOS. Des ...

  4. qt的共享内存

    https://blog.csdn.net/gdutlyp/article/details/50468677

  5. Python学习---Django的request.post源码分析

    request.post源码分析: 可以看到传递json后会帮我们dumps处理一次最后一字节形式传递过去

  6. Windows未能启动:0xc00000e9错误

    问题:计算机无法启动,错误代码为:0xc00000e9 解决方法: 1.如报错所示,\Windows\System31\config\system 文件丢失或损坏: 2.如许修复此问题,需在打开此目录 ...

  7. win7 64 &vs2010 与 opengl配置

    http://blog.csdn.net/lixam/article/details/7618015 http://blog.sina.com.cn/s/blog_7745fc8601017m36.h ...

  8. zabbix日常监控项java(四)

    yum install net-tools netstat命令 yum -y install bash-completion 命令自动补全包 https://github.com/qiueer/zab ...

  9. webshell扫描

    可扫描 weevelyshell 生成 或加密的shell 及各种变异webshell 目前仅支持php 支持扫描 weevelyshell 生成 或加密的shell 支持扫描callback一句话s ...

  10. [EffectiveC++]item27:尽量少做转型动作