import BarcodeFormat from './../BarcodeFormat' ; import BinaryBitmap from './../BinaryBitmap' ; import DecodeHintType from './../DecodeHintType' ; import Reader from './../Reader' ; import Result from './../Result' ; import ResultMetadataType from './../ResultMetadataType' ; import ResultPoint from './../ResultPoint' ; import BitMatrix from './../common/BitMatrix' ; import DecoderResult from './../common/DecoderResult' ; import DetectorResult from './../common/DetectorResult' ; import Decoder from './decoder/Decoder' ; import QRCodeDecoderMetaData from './decoder/QRCodeDecoderMetaData' ; import Detector from './detector/Detector' ; import Exception from './../Exception' ; author Sean Owen
  export default class QRCodeReader implements Reader { private static NO_POINTS = new Array < ResultPoint > ( ) ; private decoder = new Decoder ( ) ; protected getDecoder ( ) { return this . decoder ; } return throws throws throws public decode ( image , hints ? ) { let decoderResult ; let points ; if ( hints !== undefined && hints !== null && undefined !== hints . get ( DecodeHintType . PURE_BARCODE ) ) { const bits = QRCodeReader . extractPureBits ( image . getBlackMatrix ( ) ) ; decoderResult = this . decoder . decodeBitMatrix ( bits , hints ) ; points = QRCodeReader . NO_POINTS ; } else { const detectorResult = new Detector ( image . getBlackMatrix ( ) ) . detect ( hints ) ; decoderResult = this . decoder . decodeBitMatrix ( detectorResult . getBits ( ) , hints ) ; points = detectorResult . getPoints ( ) ; } if ( decoderResult . getOther ( ) instanceof QRCodeDecoderMetaData ) { ( < QRCodeDecoderMetaData > decoderResult . getOther ( ) ) . applyMirroredCorrection ( points ) ; } const result = new Result ( decoderResult . getText ( ) , decoderResult . getRawBytes ( ) , undefined , points , BarcodeFormat . QR_CODE , undefined ) ; 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 ) ; } if ( decoderResult . hasStructuredAppend ( ) ) { result . putMetadata ( ResultMetadataType . STRUCTURED_APPEND_SEQUENCE , decoderResult . getStructuredAppendSequenceNumber ( ) ) ; result . putMetadata ( ResultMetadataType . STRUCTURED_APPEND_PARITY , decoderResult . getStructuredAppendParity ( ) ) ; } return result ; } public reset ( ) { } see com . google . zxing . datamatrix . DataMatrixReader private static extractPureBits ( image ) { const leftTopBlack = image . getTopLeftOnBit ( ) ; const rightBottomBlack = image . getBottomRightOnBit ( ) ; if ( leftTopBlack === null || rightBottomBlack === null ) { throw new Exception ( Exception . NotFoundException ) ; } const moduleSize = this . moduleSize ( leftTopBlack , image ) ; let top = leftTopBlack [ 1 ] ; let bottom = rightBottomBlack [ 1 ] ; let left = leftTopBlack [ 0 ] ; let right = rightBottomBlack [ 0 ] ; if ( left >= right || top >= bottom ) { throw new Exception ( Exception . NotFoundException ) ; } if ( bottom - top !== right - left ) { right = left + ( bottom - top ) ; if ( right >= image . getWidth ( ) ) { throw new Exception ( Exception . NotFoundException ) ; } } const matrixWidth = Math . round ( ( right - left + 1 ) / moduleSize ) ; const matrixHeight = Math . round ( ( bottom - top + 1 ) / moduleSize ) ; if ( matrixWidth <= 0 || matrixHeight <= 0 ) { throw new Exception ( Exception . NotFoundException ) ; } if ( matrixHeight != matrixWidth ) { throw new Exception ( Exception . NotFoundException ) ; } const nudge = Math . floor ( moduleSize / 2.0 ) ; top += nudge ; left += nudge ; const nudgedTooFarRight = left + Math . floor ( ( matrixWidth - 1 ) * moduleSize ) - right ; if ( nudgedTooFarRight > 0 ) { if ( nudgedTooFarRight > nudge ) { throw new Exception ( Exception . NotFoundException ) ; } left -= nudgedTooFarRight ; } const nudgedTooFarDown = top + Math . floor ( ( matrixHeight - 1 ) * moduleSize ) - bottom ; if ( nudgedTooFarDown > 0 ) { if ( nudgedTooFarDown > nudge ) { throw new Exception ( Exception . NotFoundException ) ; } top -= nudgedTooFarDown ; } const bits = new BitMatrix ( matrixWidth , matrixHeight ) ; for ( let y = 0 ; y < matrixHeight ; y ++ ) { const iOffset = top + Math . floor ( y * moduleSize ) ; for ( let x = 0 ; x < matrixWidth ; x ++ ) { if ( image . get ( left + Math . floor ( x * moduleSize ) , iOffset ) ) { bits . set ( x , y ) ; } } } return bits ; } private static moduleSize ( leftTopBlack , image ) { const height = image . getHeight ( ) ; const width = image . getWidth ( ) ; let x = leftTopBlack [ 0 ] ; let y = leftTopBlack [ 1 ] ; let inBlack = true ; let transitions = 0 ; while ( x < width && y < height ) { if ( inBlack !== image . get ( x , y ) ) { if ( ++ transitions === 5 ) { break ; } inBlack = ! inBlack ; } x ++ ; y ++ ; } if ( x === width || y === height ) { throw new Exception ( Exception . NotFoundException ) ; } return ( x - leftTopBlack [ 0 ] ) / 7.0 ; } }