• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2007 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.common;
18 
19 import java.util.List;
20 
21 /**
22  * <p>Encapsulates the result of decoding a matrix of bits. This typically
23  * applies to 2D barcode formats. For now it contains the raw bytes obtained,
24  * as well as a String interpretation of those bytes, if applicable.</p>
25  *
26  * @author Sean Owen
27  */
28 public final class DecoderResult {
29 
30   private final byte[] rawBytes;
31   private int numBits;
32   private final String text;
33   private final List<byte[]> byteSegments;
34   private final String ecLevel;
35   private Integer errorsCorrected;
36   private Integer erasures;
37   private Object other;
38   private final int structuredAppendParity;
39   private final int structuredAppendSequenceNumber;
40   private final int symbologyModifier;
41 
DecoderResult(byte[] rawBytes, String text, List<byte[]> byteSegments, String ecLevel)42   public DecoderResult(byte[] rawBytes,
43                        String text,
44                        List<byte[]> byteSegments,
45                        String ecLevel) {
46     this(rawBytes, text, byteSegments, ecLevel, -1, -1, 0);
47   }
48 
DecoderResult(byte[] rawBytes, String text, List<byte[]> byteSegments, String ecLevel, int symbologyModifier)49   public DecoderResult(byte[] rawBytes,
50                        String text,
51                        List<byte[]> byteSegments,
52                        String ecLevel,
53                        int symbologyModifier) {
54     this(rawBytes, text, byteSegments, ecLevel, -1, -1, symbologyModifier);
55   }
56 
DecoderResult(byte[] rawBytes, String text, List<byte[]> byteSegments, String ecLevel, int saSequence, int saParity)57   public DecoderResult(byte[] rawBytes,
58                        String text,
59                        List<byte[]> byteSegments,
60                        String ecLevel,
61                        int saSequence,
62                        int saParity) {
63     this(rawBytes, text, byteSegments, ecLevel, saSequence, saParity, 0);
64   }
65 
DecoderResult(byte[] rawBytes, String text, List<byte[]> byteSegments, String ecLevel, int saSequence, int saParity, int symbologyModifier)66   public DecoderResult(byte[] rawBytes,
67                        String text,
68                        List<byte[]> byteSegments,
69                        String ecLevel,
70                        int saSequence,
71                        int saParity,
72                        int symbologyModifier) {
73     this.rawBytes = rawBytes;
74     this.numBits = rawBytes == null ? 0 : 8 * rawBytes.length;
75     this.text = text;
76     this.byteSegments = byteSegments;
77     this.ecLevel = ecLevel;
78     this.structuredAppendParity = saParity;
79     this.structuredAppendSequenceNumber = saSequence;
80     this.symbologyModifier = symbologyModifier;
81   }
82 
83   /**
84    * @return raw bytes representing the result, or {@code null} if not applicable
85    */
getRawBytes()86   public byte[] getRawBytes() {
87     return rawBytes;
88   }
89 
90   /**
91    * @return how many bits of {@link #getRawBytes()} are valid; typically 8 times its length
92    * @since 3.3.0
93    */
getNumBits()94   public int getNumBits() {
95     return numBits;
96   }
97 
98   /**
99    * @param numBits overrides the number of bits that are valid in {@link #getRawBytes()}
100    * @since 3.3.0
101    */
setNumBits(int numBits)102   public void setNumBits(int numBits) {
103     this.numBits = numBits;
104   }
105 
106   /**
107    * @return text representation of the result
108    */
getText()109   public String getText() {
110     return text;
111   }
112 
113   /**
114    * @return list of byte segments in the result, or {@code null} if not applicable
115    */
getByteSegments()116   public List<byte[]> getByteSegments() {
117     return byteSegments;
118   }
119 
120   /**
121    * @return name of error correction level used, or {@code null} if not applicable
122    */
getECLevel()123   public String getECLevel() {
124     return ecLevel;
125   }
126 
127   /**
128    * @return number of errors corrected, or {@code null} if not applicable
129    */
getErrorsCorrected()130   public Integer getErrorsCorrected() {
131     return errorsCorrected;
132   }
133 
setErrorsCorrected(Integer errorsCorrected)134   public void setErrorsCorrected(Integer errorsCorrected) {
135     this.errorsCorrected = errorsCorrected;
136   }
137 
138   /**
139    * @return number of erasures corrected, or {@code null} if not applicable
140    */
getErasures()141   public Integer getErasures() {
142     return erasures;
143   }
144 
setErasures(Integer erasures)145   public void setErasures(Integer erasures) {
146     this.erasures = erasures;
147   }
148 
149   /**
150    * @return arbitrary additional metadata
151    */
getOther()152   public Object getOther() {
153     return other;
154   }
155 
setOther(Object other)156   public void setOther(Object other) {
157     this.other = other;
158   }
159 
hasStructuredAppend()160   public boolean hasStructuredAppend() {
161     return structuredAppendParity >= 0 && structuredAppendSequenceNumber >= 0;
162   }
163 
getStructuredAppendParity()164   public int getStructuredAppendParity() {
165     return structuredAppendParity;
166   }
167 
getStructuredAppendSequenceNumber()168   public int getStructuredAppendSequenceNumber() {
169     return structuredAppendSequenceNumber;
170   }
171 
getSymbologyModifier()172   public int getSymbologyModifier() {
173     return symbologyModifier;
174   }
175 
176 }
177