import { NotFoundException } from '@zxing/library' ; import { AztecDetectorResult } from '@zxing/library' ; import { AztecDecoder } from '@zxing/library' ; import { AztecDetector , AztecPoint } from '@zxing/library' ; import { AztecCode } from '@zxing/library' ; import { AztecEncoder } from '@zxing/library' ; import { BitMatrix } from '@zxing/library' ; import { DecoderResult } from '@zxing/library' ; import { assertEquals , assertNotNull , assertThrow } from '../../util/AssertUtils' ; import { fail } from 'assert' ; import { ZXingArrays } from '@zxing/library' ; import Random from '../../../core/util/Random' ; import { StringUtils } from '@zxing/library' ; import { ZXingStandardCharsets } from '@zxing/library' ; import { ZXingInteger } from '@zxing/library' ; author Frank Yellin
  describe ( 'DetectorTest' , ( ) => { Test throws it ( 'testErrorInParameterLocatorZeroZero' , ( ) => { testErrorInParameterLocator ( 'X' ) ; } ) ; Test throws it ( 'testErrorInParameterLocatorCompact' , ( ) => { testErrorInParameterLocator ( 'This is an example Aztec symbol for Wikipedia.' ) ; } ) ; Test it ( 'testErrorInParameterLocatorNotCompact' , ( ) => { const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxyz' ; testErrorInParameterLocator ( alphabet + alphabet + alphabet ) ; } ) ; throws function testErrorInParameterLocator ( data ) { let aztec = AztecEncoder . encode ( StringUtils . getBytes ( data , ZXingStandardCharsets . ISO_8859_1 ) , 25 , AztecEncoder . DEFAULT_AZTEC_LAYERS ) ; let random = new Random ( aztec . getMatrix ( ) . hashCode ( ) . toString ( ) ) ; let layers = aztec . getLayers ( ) ; let compact = aztec . isCompact ( ) ; let orientationPoints = getOrientationPoints ( aztec ) ; for ( const isMirror of [ false , true ] ) { for ( const matrix of getRotations ( aztec . getMatrix ( ) ) ) { for ( let error1 = 0 ; error1 < orientationPoints . length ; error1 ++ ) { for ( let error2 = error1 ; error2 < orientationPoints . length ; error2 ++ ) { let copy = isMirror ? transpose ( matrix ) : clone ( matrix ) ; copy . flip ( orientationPoints [ error1 ] . getX ( ) , orientationPoints [ error1 ] . getY ( ) ) ; if ( error2 > error1 ) { copy . flip ( orientationPoints [ error2 ] . getX ( ) , orientationPoints [ error2 ] . getY ( ) ) ; } let r = new AztecDetector ( makeLarger ( copy , 3 ) ) . detectMirror ( isMirror ) ; assertNotNull ( r ) ; assertEquals ( r . getNbLayers ( ) , layers ) ; assertEquals ( r . isCompact ( ) , compact ) ; let res = new AztecDecoder ( ) . decode ( r ) ; assertEquals ( data , res . getText ( ) ) ; } } for ( let i = 0 ; i < 5 ; i ++ ) { let copy = clone ( matrix ) ; let errors = new Set ( ) ; while ( errors . size < 3 ) { errors . add ( random . nextInt ( orientationPoints . length ) ) ; } for ( const error of errors ) { copy . flip ( orientationPoints [ error ] . getX ( ) , orientationPoints [ error ] . getY ( ) ) ; } try { new AztecDetector ( makeLarger ( copy , 3 ) ) . detectMirror ( false ) ; fail ( 'Should not reach here' ) ; } catch ( expected ) { if ( ! ( expected instanceof NotFoundException ) ) { throw expected ; } } } } } } function makeLarger ( input , factor ) { let width = input . getWidth ( ) ; let output = new BitMatrix ( width * factor ) ; for ( let inputY = 0 ; inputY < width ; inputY ++ ) { for ( let inputX = 0 ; inputX < width ; inputX ++ ) { if ( input . get ( inputX , inputY ) ) { output . setRegion ( inputX * factor , inputY * factor , factor , factor ) ; } } } return output ; } function getRotations ( matrix0 ) { let matrix90 = rotateRight ( matrix0 ) ; let matrix180 = rotateRight ( matrix90 ) ; let matrix270 = rotateRight ( matrix180 ) ; return ZXingArrays . asList ( matrix0 , matrix90 , matrix180 , matrix270 ) ; } function rotateRight ( input ) { let width = input . getWidth ( ) ; let result = new BitMatrix ( width ) ; for ( let x = 0 ; x < width ; x ++ ) { for ( let y = 0 ; y < width ; y ++ ) { if ( input . get ( x , y ) ) { result . set ( y , width - x - 1 ) ; } } } return result ; } function transpose ( input ) { let width = input . getWidth ( ) ; let result = new BitMatrix ( width ) ; for ( let x = 0 ; x < width ; x ++ ) { for ( let y = 0 ; y < width ; y ++ ) { if ( input . get ( x , y ) ) { result . set ( y , x ) ; } } } return result ; } function clone ( input ) { let width = input . getWidth ( ) ; let result = new BitMatrix ( width ) ; for ( let x = 0 ; x < width ; x ++ ) { for ( let y = 0 ; y < width ; y ++ ) { if ( input . get ( x , y ) ) { result . set ( x , y ) ; } } } return result ; } function getOrientationPoints ( code ) { let center = ZXingInteger . truncDivision ( code . getMatrix ( ) . getWidth ( ) , 2 ) ; let offset = code . isCompact ( ) ? 5 : 7 ; let result = [ ] ; for ( let xSign = - 1 ; xSign <= 1 ; xSign += 2 ) { for ( let ySign = - 1 ; ySign <= 1 ; ySign += 2 ) { result . push ( new AztecPoint ( center + xSign * offset , center + ySign * offset ) ) ; result . push ( new AztecPoint ( center + xSign * ( offset - 1 ) , center + ySign * offset ) ) ; result . push ( new AztecPoint ( center + xSign * offset , center + ySign * ( offset - 1 ) ) ) ; } } return result ; } } ) ;