import UPCEANReader from './UPCEANReader' ; import BitArray from '../common/BitArray' ; import StringBuilder from '../util/StringBuilder' ; import NotFoundException from '../NotFoundException' ; import BarcodeFormat from '../BarcodeFormat' ; import { int , char } from 'src/customTypings' ; author Sean Owen
 *
 *  source experimental export default class UPCEReader extends UPCEANReader { /**
     * The pattern that marks the middle, and end, of a UPC-E pattern.
     * There is no "second half" to a UPC-E barcode.
     */ private static MIDDLE_END_PATTERN : Int32Array = Int32Array . from ( [ 1 , 1 , 1 , 1 , 1 , 1 ] ) ; /**
     * See  {@link #L_AND_G_PATTERNS} ; these values similarly represent patterns of
     * even-odd parity encodings of digits that imply both the number system (0 or 1)
     * used, and the check digit.
      static NUMSYS_AND_CHECK_DIGIT_PATTERNS : Int32Array [ ] = [ Int32Array . from ( [ 0x38 , 0x34 , 0x32 , 0x31 , 0x2C , 0x26 , 0x23 , 0x2A , 0x29 , 0x25 ] ) , Int32Array . from ( [ 0x07 , 0x0B , 0x0D , 0x0E , 0x13 , 0x19 , 0x1C , 0x15 , 0x16 , 0x1 ] ) , ] ; private decodeMiddleCounters : Int32Array ; public constructor ( ) { super ( ) ; this . decodeMiddleCounters = new Int32Array ( 4 ) ; } throws public decodeMiddle ( row , startRange , result ) { const counters = this . decodeMiddleCounters . map ( x => x ) ; counters [ 0 ] = 0 ; counters [ 1 ] = 0 ; counters [ 2 ] = 0 ; counters [ 3 ] = 0 ; const end = row . getSize ( ) ; let rowOffset = startRange [ 1 ] ; let lgPatternFound = 0 ; for ( let x = 0 ; x < 6 && rowOffset < end ; x ++ ) { const bestMatch = UPCEReader . decodeDigit ( row , counters , rowOffset , UPCEReader . L_AND_G_PATTERNS ) ; result += String . fromCharCode ( ( '0' . charCodeAt ( 0 ) + bestMatch % 10 ) ) ; for ( let counter of counters ) { rowOffset += counter ; } if ( bestMatch >= 10 ) { lgPatternFound |= 1 << ( 5 - x ) ; } } UPCEReader . determineNumSysAndCheckDigit ( new StringBuilder ( result ) , lgPatternFound ) ; return rowOffset ; } throws protected decodeEnd ( row , endStart ) { return UPCEReader . findGuardPatternWithoutCounters ( row , endStart , true , UPCEReader . MIDDLE_END_PATTERN ) ; } throws protected checkChecksum ( s ) { return UPCEANReader . checkChecksum ( UPCEReader . convertUPCEtoUPCA ( s ) ) ; } throws private static determineNumSysAndCheckDigit ( resultString , lgPatternFound ) { for ( let numSys = 0 ; numSys <= 1 ; numSys ++ ) { for ( let d = 0 ; d < 10 ; d ++ ) { if ( lgPatternFound === this . NUMSYS_AND_CHECK_DIGIT_PATTERNS [ numSys ] [ d ] ) { resultString . insert ( 0 , ( '0' + numSys ) ) ; resultString . append ( ( '0' + d ) ) ; return ; } } } throw NotFoundException . getNotFoundInstance ( ) ; } getBarcodeFormat ( ) { return BarcodeFormat . UPC_E ; } param upce return public static convertUPCEtoUPCA ( upce ) { const upceChars = upce . slice ( 1 , 7 ) . split ( '' ) . map ( x => x . charCodeAt ( 0 ) ) ; const result = new StringBuilder ( ) ; result . append ( upce . charAt ( 0 ) ) ; let lastChar = upceChars [ 5 ] ; switch ( lastChar ) { case 0 : case 1 : case 2 : result . appendChars ( upceChars , 0 , 2 ) ; result . append ( lastChar ) ; result . append ( '0000' ) ; result . appendChars ( upceChars , 2 , 3 ) ; break ; case 3 : result . appendChars ( upceChars , 0 , 3 ) ; result . append ( '00000' ) ; result . appendChars ( upceChars , 3 , 2 ) ; break ; case 4 : result . appendChars ( upceChars , 0 , 4 ) ; result . append ( '00000' ) ; result . append ( upceChars [ 4 ] ) ; break ; default : result . appendChars ( upceChars , 0 , 5 ) ; result . append ( '0000' ) ; result . append ( lastChar ) ; break ; } if ( upce . length >= 8 ) { result . append ( upce . charAt ( 7 ) ) ; } return result . toString ( ) ; } }