]> Pileus Git - ~andy/freeotp/blob - src/com/google/zxing/Result.java
Add native camera support
[~andy/freeotp] / 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.EnumMap;
20 import java.util.Map;
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 ResultPoint[] resultPoints;
32   private final BarcodeFormat format;
33   private Map<ResultMetadataType,Object> 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     this.text = text;
49     this.rawBytes = rawBytes;
50     this.resultPoints = resultPoints;
51     this.format = format;
52     this.resultMetadata = null;
53     this.timestamp = timestamp;
54   }
55
56   /**
57    * @return raw text encoded by the barcode
58    */
59   public String getText() {
60     return text;
61   }
62
63   /**
64    * @return raw bytes encoded by the barcode, if applicable, otherwise {@code null}
65    */
66   public byte[] getRawBytes() {
67     return rawBytes;
68   }
69
70   /**
71    * @return points related to the barcode in the image. These are typically points
72    *         identifying finder patterns or the corners of the barcode. The exact meaning is
73    *         specific to the type of barcode that was decoded.
74    */
75   public ResultPoint[] getResultPoints() {
76     return resultPoints;
77   }
78
79   /**
80    * @return {@link BarcodeFormat} representing the format of the barcode that was decoded
81    */
82   public BarcodeFormat getBarcodeFormat() {
83     return format;
84   }
85
86   /**
87    * @return {@link Map} mapping {@link ResultMetadataType} keys to values. May be
88    *   {@code null}. This contains optional metadata about what was detected about the barcode,
89    *   like orientation.
90    */
91   public Map<ResultMetadataType,Object> getResultMetadata() {
92     return resultMetadata;
93   }
94
95   public void putMetadata(ResultMetadataType type, Object value) {
96     if (resultMetadata == null) {
97       resultMetadata = new EnumMap<ResultMetadataType, Object>(ResultMetadataType.class);
98     }
99     resultMetadata.put(type, value);
100   }
101
102   public void putAllMetadata(Map<ResultMetadataType,Object> metadata) {
103     if (metadata != null) {
104       if (resultMetadata == null) {
105         resultMetadata = metadata;
106       } else {
107         resultMetadata.putAll(metadata);
108       }
109     }
110   }
111
112   public void addResultPoints(ResultPoint[] newPoints) {
113     ResultPoint[] oldPoints = resultPoints;
114     if (oldPoints == null) {
115       resultPoints = newPoints;
116     } else if (newPoints != null && newPoints.length > 0) {
117       ResultPoint[] allPoints = new ResultPoint[oldPoints.length + newPoints.length];
118       System.arraycopy(oldPoints, 0, allPoints, 0, oldPoints.length);
119       System.arraycopy(newPoints, 0, allPoints, oldPoints.length, newPoints.length);
120       resultPoints = allPoints;
121     }
122   }
123
124   public long getTimestamp() {
125     return timestamp;
126   }
127
128   @Override
129   public String toString() {
130     return text;
131   }
132
133 }