/* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /*namespace com.google.zxing.qrcode.decoder {*/ import DecodeHintType from './../../DecodeHintType'; 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 QRCodeDecoderMetaData from './QRCodeDecoderMetaData'; import DataBlock from './DataBlock'; import DecodedBitStreamParser from './DecodedBitStreamParser'; import Exception from './../../Exception'; /*import java.util.Map;*/ /** *
The main class which implements QR Code decoding -- as opposed to locating and extracting * the QR Code from an image.
* * @author Sean Owen */ export default class Decoder { private rsDecoder: ReedSolomonDecoder; public constructor() { this.rsDecoder = new ReedSolomonDecoder(GenericGF.QR_CODE_FIELD_256); } // public decode(image: boolean[][]): DecoderResult /*throws ChecksumException, FormatException*/ { // return decode(image, null) // } /** *Convenience method that can decode a QR Code represented as a 2D array of booleans. * "true" is taken to mean a black module.
* * @param image booleans representing white/black QR Code modules * @param hints decoding hints that should be used to influence decoding * @return text and bytes encoded within the QR Code * @throws FormatException if the QR Code cannot be decoded * @throws ChecksumException if error correction fails */ public decodeBooleanArray(image: boolean[][], hints?: MapDecodes a QR Code represented as a {@link BitMatrix}. A 1 or "true" is taken to mean a black module.
* * @param bits booleans representing white/black QR Code modules * @param hints decoding hints that should be used to influence decoding * @return text and bytes encoded within the QR Code * @throws FormatException if the QR Code cannot be decoded * @throws ChecksumException if error correction fails */ public decodeBitMatrix(bits: BitMatrix, hints?: MapGiven data and error-correction codewords received, possibly corrupted by errors, attempts to * correct the errors in-place using Reed-Solomon error correction.
* * @param codewordBytes data and error correction codewords * @param numDataCodewords number of codewords that are data bytes * @throws ChecksumException if error correction fails */ private correctErrors(codewordBytes: Uint8Array, numDataCodewords: number /*int*/): void /*throws ChecksumException*/ { const numCodewords = codewordBytes.length; // First read into an array of ints const codewordsInts = new Int32Array(codewordBytes); // TYPESCRIPTPORT: not realy necessary to transform to ints? could redesign everything to work with unsigned bytes? // const codewordsInts = new Int32Array(numCodewords) // for (let i = 0; i < numCodewords; i++) { // codewordsInts[i] = codewordBytes[i] & 0xFF // } try { this.rsDecoder.decode(codewordsInts, codewordBytes.length - numDataCodewords); } catch (ignored/*: ReedSolomonException*/) { throw new Exception(Exception.ChecksumException); } // Copy back into array of bytes -- only need to worry about the bytes that were data // We don't care about errors in the error-correction codewords for (let i = 0; i < numDataCodewords; i++) { codewordBytes[i] = /*(byte) */codewordsInts[i]; } } }