📄 bars2sym2of5.cpp
字号:
//
// Bars2Sym2of5
// Given an array of edge positions compute a string
// representing the bar code widths corresponding to those edge
// positions.
//
// Copyright (C) 2006 by Jon A. Webb
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
//
#include "Bars2Sym2of5.h"
CBars2Sym2of5::CBars2Sym2of5(bool allowSkipped) :
CBars2Sym(allowSkipped)
{
// iLookup translates from a bit representation
// of the bar (0=narrow, 1=wide) into the
// encoded digit, or * if there is no such digit
iLookup[000] = '*'; // NNNNN
iLookup[001] = '*'; // NNNNW
iLookup[002] = '*'; // NNNWN
iLookup[003] = '7'; // NNNWW
iLookup[004] = '*'; // NNWNN
iLookup[005] = '4'; // NNWNW
iLookup[006] = '0'; // NNWWN
iLookup[007] = '*'; // NNWWW
iLookup[010] = '*'; // NWNNN
iLookup[011] = '2'; // NWNNW
iLookup[012] = '9'; // NWNWN
iLookup[013] = '*'; // NWNWW
iLookup[014] = '6'; // NWWNN
iLookup[015] = '*'; // NWWNW
iLookup[016] = '*'; // NWWWN
iLookup[017] = '*'; // NWWWW
iLookup[020] = '*'; // WNNNN
iLookup[021] = '1'; // WNNNW
iLookup[022] = '8'; // WNNWN
iLookup[023] = '*'; // WNNWW
iLookup[024] = '5'; // WNWNN
iLookup[025] = '*'; // WNWNW
iLookup[026] = '*'; // WNWWN
iLookup[027] = '*'; // WNWWW
iLookup[030] = '3'; // WWNNN
iLookup[031] = '*'; // WWNNW
iLookup[032] = '*'; // WWNWN
iLookup[033] = '*'; // WWNWW
iLookup[034] = '*'; // WWWNN
iLookup[035] = '*'; // WWWNW
iLookup[036] = '*'; // WWWWN
iLookup[037] = '*'; // WNWWW
}
CBars2Sym2of5::~CBars2Sym2of5(void)
{}
IMPORT_C CBars2Sym2of5* CBars2Sym2of5::NewL(bool allowSkipped)
{
return new (ELeave) CBars2Sym2of5(allowSkipped);
}
// FindBarsL
// Given an array of bar widths (one byte per bar) decode the
// bar widths according to interlaced 2 of 5 and return the decoded
// string, or false if the bars don't appear to be an interlaced 2
// of 5 code.
//
// Input
// pBars is an array of bar widths
// Output
// pCode is the decoded string
IMPORT_C bool CBars2Sym2of5::DecodeL(
CArrayFix<char>* pBars,
CBars2Sym::CodeType*& pCode)
{
// some reasonableness checks.
// the number of bars equals
// 4 start bars (narrow narrow narrow narrow)
// d*5 digit bars, with d even and at least 2
// 3 stop bars (wide skip narrow)
// so if n is the number of bars d mod 10 = 7
if (pBars->Count() <= 17) {
return false;
}
int nDigits = ((pBars->Count() - 7) / 10) * 2;
if (pBars->Count() != 7 + nDigits * 5) {
return false;
}
// the number of bars is at least reasonable.
// set the narrow threshold based on the first four bars
char threshold = pBars->At(0);
int i;
for (i=1; i<4; i++) {
if (pBars->At(i) > threshold) {
threshold = pBars->At(i);
}
}
// check for correct terminator
// last should be narrow
if (pBars->At(pBars->Count() - 1) > threshold) {
return false;
}
// 2 bars before should be wide
if (pBars->At(pBars->Count() - 3) <= threshold) {
return false;
}
// everything longer than threshold is considered wide;
// everything shorter or equal, narrow.
// now work through the bars and build the digit codes,
// two at a time.
pCode = new (ELeave) TBuf<255>();
CleanupStack::PushL(pCode);
for (i=0; i<nDigits; i+=2) {
// code1 and code2 are the first and second encoded
// bar sequences, de-interlaced, with wide bars represented
// with a 1 bit and narrow bars represented by 0
int code1 = 0, code2 = 0;
int j;
for (j=0; j<5; j++) {
code1 = code1 << 1;
// decode even bars
if (pBars->At(i*5 + j*2 + 4) > threshold) {
code1 |= 1;
}
code2 = code2 << 1;
// decode odd bars
if (pBars->At(i*5 + j*2 + 5) > threshold) {
code2 |= 1;
}
}
// translate the bar sequences to digits
TChar digit1, digit2;
if (!Code2Digit(code1, digit1) || iAllowSkipped) {
// lookup failed, not an interlated 2 / 5 bar code
CleanupStack::PopAndDestroy(pCode);
return false;
}
if (!Code2Digit(code2, digit2) || iAllowSkipped) {
// lookup failed, not an interlated 2 / 5 bar code
CleanupStack::PopAndDestroy(pCode);
return false;
}
// add the decoded digits to the decoded string
pCode->Append(digit1);
pCode->Append(digit2);
}
CleanupStack::Pop(pCode);
return true;
}
bool CBars2Sym2of5::Code2Digit(int code, TChar& digit) const
{
ASSERT(code >= 0);
ASSERT(code < 32);
digit = iLookup[code];
// '*' is used to mark non-coded bar sequences
return (digit != '*');
}
const TDesC& CBars2Sym2of5::Name() const
{
_LIT(KI2o5Name, "I25");
return KI2o5Name;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -