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