1 /* 2 * Copyright 2010 ZXing authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.zxing.aztec; 18 19 import com.google.zxing.BarcodeFormat; 20 import com.google.zxing.BinaryBitmap; 21 import com.google.zxing.DecodeHintType; 22 import com.google.zxing.FormatException; 23 import com.google.zxing.NotFoundException; 24 import com.google.zxing.Reader; 25 import com.google.zxing.Result; 26 import com.google.zxing.ResultMetadataType; 27 import com.google.zxing.ResultPoint; 28 import com.google.zxing.ResultPointCallback; 29 import com.google.zxing.aztec.decoder.Decoder; 30 import com.google.zxing.aztec.detector.Detector; 31 import com.google.zxing.common.DecoderResult; 32 33 import java.util.List; 34 import java.util.Map; 35 36 /** 37 * This implementation can detect and decode Aztec codes in an image. 38 * 39 * @author David Olivier 40 */ 41 public final class AztecReader implements Reader { 42 43 /** 44 * Locates and decodes a Data Matrix code in an image. 45 * 46 * @return a String representing the content encoded by the Data Matrix code 47 * @throws NotFoundException if a Data Matrix code cannot be found 48 * @throws FormatException if a Data Matrix code cannot be decoded 49 */ 50 @Override decode(BinaryBitmap image)51 public Result decode(BinaryBitmap image) throws NotFoundException, FormatException { 52 return decode(image, null); 53 } 54 55 @Override decode(BinaryBitmap image, Map<DecodeHintType,?> hints)56 public Result decode(BinaryBitmap image, Map<DecodeHintType,?> hints) 57 throws NotFoundException, FormatException { 58 59 NotFoundException notFoundException = null; 60 FormatException formatException = null; 61 Detector detector = new Detector(image.getBlackMatrix()); 62 ResultPoint[] points = null; 63 DecoderResult decoderResult = null; 64 int errorsCorrected = 0; 65 try { 66 AztecDetectorResult detectorResult = detector.detect(false); 67 points = detectorResult.getPoints(); 68 errorsCorrected = detectorResult.getErrorsCorrected(); 69 decoderResult = new Decoder().decode(detectorResult); 70 } catch (NotFoundException e) { 71 notFoundException = e; 72 } catch (FormatException e) { 73 formatException = e; 74 } 75 if (decoderResult == null) { 76 try { 77 AztecDetectorResult detectorResult = detector.detect(true); 78 points = detectorResult.getPoints(); 79 errorsCorrected = detectorResult.getErrorsCorrected(); 80 decoderResult = new Decoder().decode(detectorResult); 81 } catch (NotFoundException | FormatException e) { 82 if (notFoundException != null) { 83 throw notFoundException; 84 } 85 if (formatException != null) { 86 throw formatException; 87 } 88 throw e; 89 } 90 } 91 92 if (hints != null) { 93 ResultPointCallback rpcb = (ResultPointCallback) hints.get(DecodeHintType.NEED_RESULT_POINT_CALLBACK); 94 if (rpcb != null) { 95 for (ResultPoint point : points) { 96 rpcb.foundPossibleResultPoint(point); 97 } 98 } 99 } 100 101 Result result = new Result(decoderResult.getText(), 102 decoderResult.getRawBytes(), 103 decoderResult.getNumBits(), 104 points, 105 BarcodeFormat.AZTEC, 106 System.currentTimeMillis()); 107 108 List<byte[]> byteSegments = decoderResult.getByteSegments(); 109 if (byteSegments != null) { 110 result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, byteSegments); 111 } 112 String ecLevel = decoderResult.getECLevel(); 113 if (ecLevel != null) { 114 result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, ecLevel); 115 } 116 errorsCorrected += decoderResult.getErrorsCorrected(); 117 result.putMetadata(ResultMetadataType.ERRORS_CORRECTED, errorsCorrected); 118 result.putMetadata(ResultMetadataType.SYMBOLOGY_IDENTIFIER, "]z" + decoderResult.getSymbologyModifier()); 119 120 return result; 121 } 122 123 @Override reset()124 public void reset() { 125 // do nothing 126 } 127 128 } 129