| 30 | * This implementation can detect and decode a MaxiCode in an image. |
| 31 | */ |
| 32 | export default class MaxiCodeReader implements Reader { |
| 33 | |
| 34 | private static readonly NO_POINTS: ResultPoint[] = []; |
| 35 | private static readonly MATRIX_WIDTH = 30; |
| 36 | private static readonly MATRIX_HEIGHT = 33; |
| 37 | |
| 38 | private decoder: Decoder = new Decoder(); |
| 39 | |
| 40 | public decode(image: BinaryBitmap, hints: Map<DecodeHintType, any> | null = null): Result { |
| 41 | // Note that MaxiCode reader effectively always assumes PURE_BARCODE mode |
| 42 | // and can't detect it in an image |
| 43 | const bits = MaxiCodeReader.extractPureBits(image.getBlackMatrix()); |
| 44 | const decoderResult = this.decoder.decode(bits, hints); |
| 45 | const result = new Result( |
| 46 | decoderResult.getText(), |
| 47 | decoderResult.getRawBytes(), |
| 48 | 8 * decoderResult.getRawBytes().length, |
| 49 | MaxiCodeReader.NO_POINTS, |
| 50 | BarcodeFormat.MAXICODE, |
| 51 | System.currentTimeMillis() |
| 52 | ); |
| 53 | result.putMetadata(ResultMetadataType.ERRORS_CORRECTED, decoderResult.getErrorsCorrected()); |
| 54 | const ecLevel = decoderResult.getECLevel(); |
| 55 | if (ecLevel != null) { |
| 56 | result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, ecLevel); |
| 57 | } |
| 58 | return result; |
| 59 | } |
| 60 | |
| 61 | public reset(): void { |
| 62 | // do nothing |
| 63 | } |
| 64 | |
| 65 | private static extractPureBits(image: BitMatrix): BitMatrix { |
| 66 | const enclosingRectangle = image.getEnclosingRectangle(); |
| 67 | if (enclosingRectangle == null) { |
| 68 | throw new NotFoundException(); |
| 69 | } |
| 70 | |
| 71 | const left = enclosingRectangle[0]; |
| 72 | const top = enclosingRectangle[1]; |
| 73 | const width = enclosingRectangle[2]; |
| 74 | const height = enclosingRectangle[3]; |
| 75 | |
| 76 | // Now just read off the bits |
| 77 | const bits = new BitMatrix(MaxiCodeReader.MATRIX_WIDTH, MaxiCodeReader.MATRIX_HEIGHT); |
| 78 | for (let y = 0; y < MaxiCodeReader.MATRIX_HEIGHT; y++) { |
| 79 | const iy = top + Math.min(Math.floor((y * height + height / 2) / MaxiCodeReader.MATRIX_HEIGHT), height - 1); |
| 80 | for (let x = 0; x < MaxiCodeReader.MATRIX_WIDTH; x++) { |
| 81 | // srowen: I don't quite understand why the formula below is necessary, but it |
| 82 | // can walk off the image if left + width = the right boundary. So cap it. |
| 83 | const ix = left + Math.min( |
| 84 | Math.floor((x * width + width / 2 + (y & 0x01) * Math.floor(width / 2)) / MaxiCodeReader.MATRIX_WIDTH), |
| 85 | width - 1); |
| 86 | if (image.get(ix, iy)) { |
| 87 | bits.set(x, y); |
| 88 | } |
| 89 | } |
nothing calls this directly
no outgoing calls
no test coverage detected