📄 bars2symean.cpp
字号:
//
// Bars2SymEan
// Given an array of bar code widths read an EAN bar
// code, if present.
//
// This code was written based on Alexander Bogomolny's page describing the
// EAN barcode: http://www.cut-the-knot.org/do_you_know/BarcodeEncoding.shtml.
//
// 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 "Bars2SymEan.h"
using namespace Barcode;
CBars2SymEan::~CBars2SymEan()
{}
EXPORT_C CBars2SymEan* CBars2SymEan::NewL()
{
CBars2SymEan *pMe = new (ELeave) CBars2SymEan();
CleanupStack::PushL(pMe);
pMe->ConstructL();
CleanupStack::Pop(pMe);
return pMe;
}
CBars2SymEan::CBars2SymEan() : CBars2Sym()
{
}
void CBars2SymEan::ConstructL()
{
// In the below, the R codes for the digits are the 1-complements
// of the L codes. But we encode the R digits by
// encoded a white bar as 1 and a black bar as 0
// so we automatically get the 1-complement without
// while using the same tables
int i;
// initialize table to no successful lookup all characters
for (i=0; i<128; i++) {
iLookupOdd[i] = '*';
iLookupEven[i] = '*';
iLookupUpc[i] = '*';
}
// UPC table
/* The (L) codes for the ten digits are:
0 = 3-2-1-1 = 0001101 = 0x0d
1 = 2-2-2-1 = 0011001 = 0x19
2 = 2-2-1-2 = 0011011 = 0x1b
3 = 1-4-1-1 = 0111101 = 0x3d
4 = 1-1-3-2 = 0100011 = 0x23
5 = 1-2-3-1 = 0110001 = 0x31
6 = 4-1-1-1 = 0000101 = 0x05
7 = 1-3-1-2 = 0111011 = 0x3b
8 = 3-1-2-1 = 0001001 = 0x09
9 = 2-1-1-3 = 0010111 = 0x17
*/
const char char0 = 0x0d;
const char char1 = 0x19;
const char char2 = 0x1b;
const char char3 = 0x3d;
const char char4 = 0x23;
const char char5 = 0x31;
const char char6 = 0x05;
const char char7 = 0x3b;
const char char8 = 0x09;
const char char9 = 0x17;
iLookupUpc[char0] = '0';
iLookupUpc[char1] = '1';
iLookupUpc[char2] = '2';
iLookupUpc[char3] = '3';
iLookupUpc[char4] = '4';
iLookupUpc[char5] = '5';
iLookupUpc[char6] = '6';
iLookupUpc[char7] = '7';
iLookupUpc[char8] = '8';
iLookupUpc[char9] = '9';
// EAN even parity table
/* The left even parity codes for the ten digits are:
0 = 1-1-2-3 = 0100111 = 0x27
1 = 1-2-2-2 = 0110011 = 0x33
2 = 2-2-1-2 = 0011011 = 0x1b
3 = 1-1-4-1 = 0100001 = 0x21
4 = 2-3-1-1 = 0011101 = 0x1d
5 = 1-3-2-1 = 0111001 = 0x39
6 = 4-1-1-1 = 0000101 = 0x05
7 = 2-1-3-1 = 0010001 = 0x11
8 = 3-1-2-1 = 0001001 = 0x09
9 = 2-1-1-3 = 0010111 = 0x17
*/
iLookupEven[0x27] = '0';
iLookupEven[0x33] = '1';
iLookupEven[0x1b] = '2';
iLookupEven[0x21] = '3';
iLookupEven[0x1d] = '4';
iLookupEven[0x39] = '5';
iLookupEven[0x05] = '6';
iLookupEven[0x11] = '7';
iLookupEven[0x09] = '8';
iLookupEven[0x17] = '9';
// EAN odd parity table
/* The left odd parity codes for the ten digits are:
0 = 3-2-1-1 = 0001101 = 0x0d
1 = 2-2-2-1 = 0011001 = 0x19
2 = 2-1-2-2 = 0010011 = 0x13
3 = 1-4-1-1 = 0111101 = 0x3d
4 = 1-1-3-2 = 0100011 = 0x23
5 = 1-2-3-1 = 0110001 = 0x31
6 = 1-1-1-4 = 0101111 = 0x30
7 = 1-3-1-2 = 0111011 = 0x3b
8 = 1-2-1-3 = 0110111 = 0x37
9 = 3-1-1-2 = 0001011 = 0x0b
*/
iLookupOdd[0x0d] = '0';
iLookupOdd[0x19] = '1';
iLookupOdd[0x13] = '2';
iLookupOdd[0x3d] = '3';
iLookupOdd[0x23] = '4';
iLookupOdd[0x31] = '5';
iLookupOdd[0x30] = '6';
iLookupOdd[0x3b] = '7';
iLookupOdd[0x37] = '8';
iLookupOdd[0x0b] = '9';
// initialize iParityOdd
// row is the first digit from the UPC code
// column is the digit position - 1
// 0 1 2 3 4 5
// 0 odd odd odd odd odd odd
// 1 odd odd even odd even even
// 2 odd odd even even odd even
// 3 odd odd even even even odd
// 4 odd even odd odd even even
// 5 odd even even odd odd even
// 6 odd even even even odd odd
// 7 odd even odd even odd even
// 8 odd even odd even even odd
// 9 odd even even odd even odd
iParityOdd[0][0] = true;
iParityOdd[0][1] = true;
iParityOdd[0][2] = true;
iParityOdd[0][3] = true;
iParityOdd[0][4] = true;
iParityOdd[0][5] = true;
iParityOdd[1][0] = true;
iParityOdd[1][1] = true;
iParityOdd[1][2] = false;
iParityOdd[1][3] = true;
iParityOdd[1][4] = false;
iParityOdd[1][5] = false;
iParityOdd[2][0] = true;
iParityOdd[2][1] = true;
iParityOdd[2][2] = false;
iParityOdd[2][3] = false;
iParityOdd[2][4] = true;
iParityOdd[2][5] = false;
iParityOdd[3][0] = true;
iParityOdd[3][1] = true;
iParityOdd[3][2] = false;
iParityOdd[3][3] = false;
iParityOdd[3][4] = false;
iParityOdd[3][5] = true;
iParityOdd[4][0] = true;
iParityOdd[4][1] = false;
iParityOdd[4][2] = true;
iParityOdd[4][3] = true;
iParityOdd[4][4] = false;
iParityOdd[4][5] = false;
iParityOdd[5][0] = true;
iParityOdd[5][1] = false;
iParityOdd[5][2] = false;
iParityOdd[5][3] = true;
iParityOdd[5][4] = true;
iParityOdd[5][5] = false;
iParityOdd[6][0] = true;
iParityOdd[6][1] = false;
iParityOdd[6][2] = false;
iParityOdd[6][3] = false;
iParityOdd[6][4] = true;
iParityOdd[6][5] = true;
iParityOdd[7][0] = true;
iParityOdd[7][1] = false;
iParityOdd[7][2] = true;
iParityOdd[7][3] = false;
iParityOdd[7][4] = true;
iParityOdd[7][5] = false;
iParityOdd[8][0] = true;
iParityOdd[8][1] = false;
iParityOdd[8][2] = true;
iParityOdd[8][3] = false;
iParityOdd[8][4] = false;
iParityOdd[8][5] = true;
iParityOdd[9][0] = true;
iParityOdd[9][1] = false;
iParityOdd[9][2] = false;
iParityOdd[9][3] = true;
iParityOdd[9][4] = false;
iParityOdd[9][5] = true;
}
// DecodeFirstDigit
// Decode the first digit using the UPC lookup table
// and record the digit for use when determining which
// parity to use when decoding other digits.
// bars is the array of bar widths.
// pos is the position of the bars for this digit in the array.
// Returns true if the digit was decoded successfully.
// Returns decoded digit in 'digit'.
bool CBars2SymEan::DecodeFirstDigit(CArrayFix<char>& bars, int pos, TChar& digit)
{
// look up the code
digit = iLookupUpc[EncodeBars(bars, pos)];
if (digit != '*') {
iFirstDigit = int(digit) - int('0');
return true;
} else {
return false;
}
}
// DecodeOtherDigits
// Decode the other digits using the appropriate parity table
// from the first digit.
// bars is the array of bar widths.
// pos is the position of the bars for this digit in the array.
// Returns true if the digit was decoded successfully.
// Returns decoded digit in 'digit'.
bool CBars2SymEan::DecodeOtherDigits(CArrayFix<char>& bars, int pos, int digitPos, TChar& digit)
{
if (iParityOdd[iFirstDigit][digitPos]) {
// odd parity digit
digit = iLookupOdd[EncodeBars(bars, pos)];
} else {
// even parity digit
digit = iLookupEven[EncodeBars(bars, pos)];
}
// now look up the code
return (digit != '*');
}
// EncodeBars
// Encodes the four bars starting at position pos in bars
// as a bit pattern for lookup in the iLookup tables.
// Returns the encoded value.
int CBars2SymEan::EncodeBars(CArrayFix<char>& bars, int pos)
{
// encode bars as 7-bit number
int encoded = 0;
// each group of 4 bars consists of a white bar, black bar, white bar, black bar
// sequence
// below encode left (right) bars starting with white (black)
// encoded white (black) bars as 0 (1)
unsigned char j;
for (j=0x00; j<bars[pos]; j++) {
encoded = encoded << 1;
}
// encode black (white) bars as 1 (0)
for (j=0x00; j<bars[pos+1]; j++) {
encoded = encoded << 1;
encoded |= 1;
}
// encode white (black) bars as 0 (1)
for (j=0x00; j<bars[pos+2]; j++) {
encoded = encoded << 1;
}
// encode black (white) bars as 1 (0)
for (j=0x00; j<bars[pos+3]; j++) {
encoded = encoded << 1;
encoded |= 1;
}
return encoded;
}
IMPORT_C bool CBars2SymEan::DecodeL(
Core::CImage image,
CBars2Sym::CodeType*& pCode)
{
return false;
}
const TDesC& CBars2SymEan::Name() const
{
_LIT(KUpcName, "EAN");
return KUpcName;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -