9223ceb72ba383355cc26e0ebd298e5fa85ea51d
[zxing.git] / core / src / com / google / zxing / Result.java
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;
18
19 import java.util.Enumeration;
20 import java.util.Hashtable;
21
22 /**
23  * <p>Encapsulates the result of decoding a barcode within an image.</p>
24  *
25  * @author Sean Owen
26  */
27 public final class Result {
28
29   private final String text;
30   private final byte[] rawBytes;
31   private final ResultPoint[] resultPoints;
32   private final BarcodeFormat format;
33   private Hashtable resultMetadata;
34   private final long timestamp;
35
36   public Result(String text,
37                 byte[] rawBytes,
38                 ResultPoint[] resultPoints,
39                 BarcodeFormat format) {
40     this(text, rawBytes, resultPoints, format, System.currentTimeMillis());
41   }
42
43   public Result(String text,
44                 byte[] rawBytes,
45                 ResultPoint[] resultPoints,
46                 BarcodeFormat format,
47                 long timestamp) {
48     if (text == null && rawBytes == null) {
49       throw new IllegalArgumentException("Text and bytes are null");
50     }
51     this.text = text;
52     this.rawBytes = rawBytes;
53     this.resultPoints = resultPoints;
54     this.format = format;
55     this.resultMetadata = null;
56     this.timestamp = timestamp;
57   }
58
59   /**
60    * @return raw text encoded by the barcode, if applicable, otherwise <code>null</code>
61    */
62   public String getText() {
63     return text;
64   }
65
66   /**
67    * @return raw bytes encoded by the barcode, if applicable, otherwise <code>null</code>
68    */
69   public byte[] getRawBytes() {
70     return rawBytes;
71   }
72
73   /**
74    * @return points related to the barcode in the image. These are typically points
75    *         identifying finder patterns or the corners of the barcode. The exact meaning is
76    *         specific to the type of barcode that was decoded.
77    */
78   public ResultPoint[] getResultPoints() {
79     return resultPoints;
80   }
81
82   /**
83    * @return {@link BarcodeFormat} representing the format of the barcode that was decoded
84    */
85   public BarcodeFormat getBarcodeFormat() {
86     return format;
87   }
88
89   /**
90    * @return {@link Hashtable} mapping {@link ResultMetadataType} keys to values. May be
91    *   <code>null</code>. This contains optional metadata about what was detected about the barcode,
92    *   like orientation.
93    */
94   public Hashtable getResultMetadata() {
95     return resultMetadata;
96   }
97
98   public void putMetadata(ResultMetadataType type, Object value) {
99     if (resultMetadata == null) {
100       resultMetadata = new Hashtable(3);
101     }
102     resultMetadata.put(type, value);
103   }
104
105   public void putAllMetadata(Hashtable metadata) {
106     if (metadata != null) {
107       if (resultMetadata == null) {
108         resultMetadata = metadata;
109       } else {
110         Enumeration e = metadata.keys();
111         while (e.hasMoreElements()) {
112           ResultMetadataType key = (ResultMetadataType) e.nextElement();
113           Object value = metadata.get(key);
114           resultMetadata.put(key, value);
115         }
116       }
117     }
118   }
119
120   public long getTimestamp() {
121     return timestamp;
122   }
123
124   public String toString() {
125     if (text == null) {
126       return "[" + rawBytes.length + " bytes]";
127     } else {
128       return text;
129     }
130   }
131
132 }