import ChecksumException from '../../ChecksumException' ; import BitMatrix from '../../common/BitMatrix' ; import DecoderResult from '../../common/DecoderResult' ; import GenericGF from '../../common/reedsolomon/GenericGF' ; import ReedSolomonDecoder from '../../common/reedsolomon/ReedSolomonDecoder' ; import BitMatrixParser from './BitMatrixParser' ; import DataBlock from './DataBlock' ; import DecodedBitStreamParser from './DecodedBitStreamParser' ; author bbrown google export default class Decoder { private rsDecoder : ReedSolomonDecoder ; constructor ( ) { this . rsDecoder = new ReedSolomonDecoder ( GenericGF . DATA_MATRIX_FIELD_256 ) ; } /**
     * <p>Decodes a Data Matrix Code represented as a  BitMatrix . A 1 or "true" is taken
     * to mean a black module.</p>
     *
     *  param bits return throws throws public decode ( bits ) { const parser = new BitMatrixParser ( bits ) ; const version = parser . getVersion ( ) ; const codewords = parser . readCodewords ( ) ; const dataBlocks = DataBlock . getDataBlocks ( codewords , version ) ; let totalBytes = 0 ; for ( let db of dataBlocks ) { totalBytes += db . getNumDataCodewords ( ) ; } const resultBytes = new Uint8Array ( totalBytes ) ; const dataBlocksCount = dataBlocks . length ; for ( let j = 0 ; j < dataBlocksCount ; j ++ ) { const dataBlock = dataBlocks [ j ] ; const codewordBytes = dataBlock . getCodewords ( ) ; const numDataCodewords = dataBlock . getNumDataCodewords ( ) ; this . correctErrors ( codewordBytes , numDataCodewords ) ; for ( let i = 0 ; i < numDataCodewords ; i ++ ) { resultBytes [ i * dataBlocksCount + j ] = codewordBytes [ i ] ; } } return DecodedBitStreamParser . decode ( resultBytes ) ; } param codewordBytes param numDataCodewords throws private correctErrors ( codewordBytes , numDataCodewords ) { const codewordsInts = new Int32Array ( codewordBytes ) ; try { this . rsDecoder . decode ( codewordsInts , codewordBytes . length - numDataCodewords ) ; } catch ( ignored ) { throw new ChecksumException ( ) ; } for ( let i = 0 ; i < numDataCodewords ; i ++ ) { codewordBytes [ i ] = codewordsInts [ i ] ; } } }