📄 bars2symupc.h
字号:
//
// Given an array of edge positions and strengths 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
//
#ifndef Bars2SymUpc_h
#define Bars2SymUpc_h
#include "Array.h"
#include "Bars2Sym.h"
#include "HashTable.h"
#include <e32cons.h>
#include <e32math.h>
class CodeConv;
class CodeValue;
namespace Barcode
{
class CBars2SymUpc: public CBars2Sym
{
public:
static const int Digits = 6; // number of digits in a barcode
static const int LeftWidth = 3;
static const int RightWidth = 3;
static const int MidWidth = 5;
static const int DigitWidth = 7;
static const int MidOffset = LeftWidth + Digits*DigitWidth;
static const int RightOffset = LeftWidth + Digits*DigitWidth*2 + MidWidth;
static const int TotalWidth =
LeftWidth + DigitWidth * Digits + MidWidth + DigitWidth * Digits + RightWidth;
typedef TUint8 Code[Digits];
// Lifecycle
public:
IMPORT_C static CBars2SymUpc* NewL(TInt nWidth);
~CBars2SymUpc(void);
private:
// First-phase constructor
CBars2SymUpc(TInt nWidth);
// Second-phase constructor
void ConstructL();
// Operations
public:
// ClearHash clears the hash table
void ClearHash();
// FindBarsL
// Given an array of bar widths (one byte per bar) produce an array
// representing the string encoded, or return false if nothing was found.
//
// Input
// pPositions is an array of bar widths
// Output
// pCode is the decoded string
IMPORT_C bool DecodeL(
Core::CImage image,
CodeType*& pCode,
TInt32 &goodness);
private:
// BestCode
// Given an array of CodeConv objects, return the
// index of the object that has the highest (best)
// convolution value.
static TInt BestCode(CArrayFixFlat<CodeConv>& codes);
// BlurPatternL
// Given a vector of length nLength blur the coefficients
// using the Gaussian coefficients stored in ipfCoeff
void BlurPatternL(TReal* vector, TInt nLength);
// builds a mask from a given code which is our best estimate of what
// the image should look like, given that code. pnMask is the returned pointer
// to a vector of weights, of length nWidth.
void BuildLeftSideMaskL(Code& pCode, TInt16*& pnMask, TInt &nWidth);
void BuildRightSideMaskL(Code& pCode, TInt16*& pnMask, TInt &nWidth);
// ComputeCoefficientsL
// Computes Gaussian coefficients with sigma = ifSigma
// and stores them in the created array ipfCoeff
void ComputeCoefficientsL();
// Convolve
// Convolves a 1-dimensional array of weights (pnWeights)
// of length nLength with the image indexed by index
// at row nRow and starting at column nPos, expanding
// the weights array by nPixelsPerBar.
static TInt32 Convolve(
Core::IGrayIndex index,
TInt16 *pnWeights,
TInt nLength,
TInt nPixelsPerBar,
TInt nRow,
TInt nPos);
TInt32 Convolve(
Core::CArray<unsigned char>& row,
TInt16 *pnWeights,
TInt nLength,
TInt nPixelsPerBar,
TInt nCol);
// EstimateBarcodesL
// Estimates all the barcodes in the image and
// puts them into the hash tables.
// Returns the average left edge of the barcode.
TBool EstimateBarcodesL(Core::CImage image, TInt &avgLeftEdge);
// EstimateCodeLeftL and EstimateCodeRightL
// estimate the 6-digit code on the left
// and right side of the barcode, at row
// nRow of the image, starting at position
// nCol. code is the returned code.
void EstimateCodeLeftL(
Core::CImage image,
TInt nRow,
TInt nCol, // the left edge of the barcode
TBuf8<Digits> &code);
void EstimateCodeRightL(
Core::CImage image,
TInt nRow,
TInt nCol, // the left edge of the middle pattern
TBuf8<Digits> &code);
// EstimateLeftEdge
// Finds the left edge of the barcode pattern at row nRow
int EstimateLeftEdge(Core::CImage image, TInt nRow);
// GrabBestBarcodes
// Takes the top few barcodes from the hash table and
// convolves them with an average of the image. Stores
// the convolution results and the codes into pCodes.
//
TBool GrabBestBarcodesL(
Core::CHashTable<Code, TInt>& hash,
Core::CArray<unsigned char> *pSum,
TInt nHeight,
TInt nPixelsPerBar,
TInt avgLeftEdge,
CArrayFixFlat<CodeConv> *& pCodes,
TBool bLeft);
// NormalizePattern
// Scales the floating point vector of weights in pfCoeff
// (length nLength) so that the sum is 0.0 (if bNormZero)
// and the sum of absolute values is 1.0. Also scales
// by 256.0 and assigns to pnCoeff if bConvInt.
static void NormalizePattern(
TReal* pfCoeff,
TInt nLength,
TInt16 *pnCoeff,
bool bNormZero,
bool bConvInt);
// TurnDigitsToText
// Translates decoded barcode into a string.
static CBars2Sym::CodeType* CBars2SymUpc::TurnDigitsToText(TBuf8<Digits*2>& code);
// VerifyCheckDigit
// Returns true if the check digit matches what it is supposed to be.
//
static bool VerifyCheckDigit(TBuf8<Digits*2>& digits);
private:
// Accessors
public:
int BarcodeWidth() const;
const TDesC& Name() const;
private:
// Attributes
TReal iDigitCodes[10][DigitWidth];
// This is the convoluion table
TInt16 iLeftConv[10][10][LeftWidth+DigitWidth*2];
TInt16 iMidConv[10][10][MidWidth+DigitWidth*2];
TInt16 iPairConv[10][10][DigitWidth*2];
Core::CHashTable<Code, TInt> *iHashLeft, *iHashRight;
CArrayFixFlat<TBuf8<Digits>*>* iCodes;
CArrayFixFlat<TReal> *ipfCoeff;
TReal ifSigma;
};
};
#endif // Bars2SymUpc_h
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -