import BarcodeFormat from '../BarcodeFormat' ; import BinaryBitmap from '../BinaryBitmap' ; import BitMatrix from '../common/BitMatrix' ; import DecoderResult from '../common/DecoderResult' ; import DecodeHintType from '../DecodeHintType' ; import NotFoundException from '../NotFoundException' ; import Reader from '../Reader' ; import Result from '../Result' ; import ResultMetadataType from '../ResultMetadataType' ; import ResultPoint from '../ResultPoint' ; import System from '../util/System' ; import Decoder from './decoder/Decoder' ; import Detector from './detector/Detector' ; author bbrown google export default class DataMatrixReader implements Reader { private static NO_POINTS : ResultPoint [ ] = [ ] ; private decoder : Decoder = new Decoder ( ) ; return throws throws throws public decode ( image , hints = null ) { let decoderResult ; let points ; if ( hints != null && hints . has ( DecodeHintType . PURE_BARCODE ) ) { const bits = DataMatrixReader . extractPureBits ( image . getBlackMatrix ( ) ) ; decoderResult = this . decoder . decode ( bits ) ; points = DataMatrixReader . NO_POINTS ; } else { const detectorResult = new Detector ( image . getBlackMatrix ( ) ) . detect ( ) ; decoderResult = this . decoder . decode ( detectorResult . getBits ( ) ) ; points = detectorResult . getPoints ( ) ; } const rawBytes = decoderResult . getRawBytes ( ) ; const result = new Result ( decoderResult . getText ( ) , rawBytes , 8 * rawBytes . length , points , BarcodeFormat . DATA_MATRIX , System . currentTimeMillis ( ) ) ; const byteSegments = decoderResult . getByteSegments ( ) ; if ( byteSegments != null ) { result . putMetadata ( ResultMetadataType . BYTE_SEGMENTS , byteSegments ) ; } const ecLevel = decoderResult . getECLevel ( ) ; if ( ecLevel != null ) { result . putMetadata ( ResultMetadataType . ERROR_CORRECTION_LEVEL , ecLevel ) ; } return result ; } public reset ( ) { } see com . google . zxing . qrcode . QRCodeReader private static extractPureBits ( image ) { const leftTopBlack = image . getTopLeftOnBit ( ) ; const rightBottomBlack = image . getBottomRightOnBit ( ) ; if ( leftTopBlack == null || rightBottomBlack == null ) { throw new NotFoundException ( ) ; } const moduleSize = this . moduleSize ( leftTopBlack , image ) ; let top = leftTopBlack [ 1 ] ; const bottom = rightBottomBlack [ 1 ] ; let left = leftTopBlack [ 0 ] ; const right = rightBottomBlack [ 0 ] ; const matrixWidth = ( right - left + 1 ) / moduleSize ; const matrixHeight = ( bottom - top + 1 ) / moduleSize ; if ( matrixWidth <= 0 || matrixHeight <= 0 ) { throw new NotFoundException ( ) ; } const nudge = moduleSize / 2 ; top += nudge ; left += nudge ; const bits = new BitMatrix ( matrixWidth , matrixHeight ) ; for ( let y = 0 ; y < matrixHeight ; y ++ ) { const iOffset = top + y * moduleSize ; for ( let x = 0 ; x < matrixWidth ; x ++ ) { if ( image . get ( left + x * moduleSize , iOffset ) ) { bits . set ( x , y ) ; } } } return bits ; } private static moduleSize ( leftTopBlack , image ) { const width = image . getWidth ( ) ; let x = leftTopBlack [ 0 ] ; const y = leftTopBlack [ 1 ] ; while ( x < width && image . get ( x , y ) ) { x ++ ; } if ( x === width ) { throw new NotFoundException ( ) ; } const moduleSize = x - leftTopBlack [ 0 ] ; if ( moduleSize === 0 ) { throw new NotFoundException ( ) ; } return moduleSize ; } }