一維碼Code 39簡介及其解碼實(shí)現(xiàn)
添加時間:2019-11-10 14:08:54
來源:
一維碼Code 39:由于編制簡單、能夠?qū)θ我忾L度的數(shù)據(jù)進(jìn)行編碼、支持設(shè)備廣泛等特性而被廣泛采用。
Code 39碼特點(diǎn):
1. 能夠?qū)θ我忾L度的數(shù)據(jù)進(jìn)行編碼,其局限在于印刷品的長度和條碼閱讀器的識別范圍。
2. 支持設(shè)備廣泛,目前幾乎所有的條形碼閱讀設(shè)備都能閱讀Code 39碼,打印機(jī)也是同樣情況。
3.編制簡單,簡單的開發(fā)技術(shù)就能快速生成相應(yīng)的編碼圖像。
4. 一般Code 39碼由5條線和分開它們的4條縫隙共9個元素構(gòu)成。線和縫隙有寬窄之分,而且無論線還是縫隙僅有3個比其他的元素要寬一定比例。39碼因此得名。
Code 39碼編碼規(guī)則:
1. 每五條線表示一個字符;
2. 粗線表示1,細(xì)線表示0;
3. 線條間的間隙寬的表示1,窄的表示0;
4. 五條線加上它們之間的四條間隙就是九位二進(jìn)制編碼,而且這九位中必定有三位是1,所以稱為39碼;
5、 條形碼的首尾各一個”*”標(biāo)識開始和結(jié)束。
Code 39碼只接受如下43個有效輸入字符:
1. 26個大寫字母(A – Z);
2. 十個數(shù)字(0 – 9);
3. 連接號(-),句號(.),空格,美圓符號($),斜扛(/),加號(+)以及百分號(%)。其余的輸入將被忽略。
Code 39碼通常情況下不需要校驗(yàn)碼。但是對于精確度要求高的應(yīng)用,需要在Code 39條形碼后面增加一個校驗(yàn)碼。
由于可以合并兩個字符來表達(dá)第三個字符.這樣就可以用Code 39條形碼來表示整個ASCII表.這樣就產(chǎn)生了Code 39全ASCII碼字型.
以下是通過zxing-cpp開源庫實(shí)現(xiàn)的對一維碼Code 39進(jìn)行解碼的測試代碼:
#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 <opencv2/opencv.hpp>
#include "zxing/MatSource.h"
int test_Code39_decode()
{
std::string image_name = "E:/GitCode/BarCode_Test/test_images/Code39.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 ", width, height);
zxing::Ref<zxing::Reader> reader;
reader.reset(new zxing::oned::Code39Reader);
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_39_HINT)));
std::string txt = "E:/GitCode/BarCode_Test/test_images/Code39.txt";
std::ifstream in(txt);
if (!in.is_open()) {
fprintf(stderr, "fail to open file: %s ", txt.c_str());
return -1;
}
std::string str1;
std::getline(in, str1);
fprintf(stderr, "actual result: %s ", str1.c_str());
std::string str2 = result->getText()->getText();
fprintf(stdout, "recognization result: %s ", str2.c_str());
if (str1.compare(str2) == 0) {
fprintf(stderr, "===== recognition is correct ===== ");
}
else {
fprintf(stderr, "===== recognition is wrong ===== ");
return -1;
}
in.close();
return 0;
}
2019-11
一維碼Code 39:由于編制簡單、能夠?qū)θ我忾L度的數(shù)據(jù)進(jìn)行編碼、支持設(shè)備廣泛等特性而被廣泛采用。Code 39碼特點(diǎn):1. 能夠?qū)θ我忾L度的數(shù)據(jù)進(jìn)行編碼,其局限在于印刷品的長度和條碼閱讀器的識別范圍。2. 支持設(shè)備廣泛,目前幾乎所有的條形碼閱讀設(shè)備都能閱讀Code 39碼,打印… [了解更多]
地 址:中國(上海)自由貿(mào)易試驗(yàn)區(qū)臨港新片區(qū)平港路883-885號1幢
郵政編碼:201411
電 話:13761963296
郵 箱:847098433@qq.com
投訴郵 箱:info@xtvu.com