| 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 final int numBits; |
| 32 | private ResultPoint[] resultPoints; |
| 33 | private final BarcodeFormat format; |
| 34 | private Map<ResultMetadataType,Object> resultMetadata; |
| 35 | private final long timestamp; |
| 36 | |
| 37 | public Result(String text, |
| 38 | byte[] rawBytes, |
| 39 | ResultPoint[] resultPoints, |
| 40 | BarcodeFormat format) { |
| 41 | this(text, rawBytes, resultPoints, format, System.currentTimeMillis()); |
| 42 | } |
| 43 | |
| 44 | public Result(String text, |
| 45 | byte[] rawBytes, |
| 46 | ResultPoint[] resultPoints, |
| 47 | BarcodeFormat format, |
| 48 | long timestamp) { |
| 49 | this(text, rawBytes, rawBytes == null ? 0 : 8 * rawBytes.length, |
| 50 | resultPoints, format, timestamp); |
| 51 | } |
| 52 | |
| 53 | public Result(String text, |
| 54 | byte[] rawBytes, |
| 55 | int numBits, |
| 56 | ResultPoint[] resultPoints, |
| 57 | BarcodeFormat format, |
| 58 | long timestamp) { |
| 59 | this.text = text; |
| 60 | this.rawBytes = rawBytes; |
| 61 | this.numBits = numBits; |
| 62 | this.resultPoints = resultPoints; |
| 63 | this.format = format; |
| 64 | this.resultMetadata = null; |
| 65 | this.timestamp = timestamp; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @return raw text encoded by the barcode |
| 70 | */ |
| 71 | public String getText() { |
| 72 | return text; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @return raw bytes encoded by the barcode, if applicable, otherwise {@code null} |
| 77 | */ |
| 78 | public byte[] getRawBytes() { |
| 79 | return rawBytes; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @return how many bits of {@link #getRawBytes()} are valid; typically 8 times its length |
| 84 | * @since 3.3.0 |
| 85 | */ |
| 86 | public int getNumBits() { |
| 87 | return numBits; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @return points related to the barcode in the image. These are typically points |
| 92 | * identifying finder patterns or the corners of the barcode. The exact meaning is |
| 93 | * specific to the type of barcode that was decoded. |
| 94 | */ |
| 95 | public ResultPoint[] getResultPoints() { |
| 96 | return resultPoints; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @return {@link BarcodeFormat} representing the format of the barcode that was decoded |
| 101 | */ |
| 102 | public BarcodeFormat getBarcodeFormat() { |
| 103 | return format; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @return {@link Map} mapping {@link ResultMetadataType} keys to values. May be |
| 108 | * {@code null}. This contains optional metadata about what was detected about the barcode, |
| 109 | * like orientation. |
| 110 | */ |
| 111 | public Map<ResultMetadataType,Object> getResultMetadata() { |
| 112 | return resultMetadata; |
| 113 | } |
| 114 | |
| 115 | public void putMetadata(ResultMetadataType type, Object value) { |
| 116 | if (resultMetadata == null) { |
| 117 | resultMetadata = new EnumMap<>(ResultMetadataType.class); |
| 118 | } |
| 119 | resultMetadata.put(type, value); |
| 120 | } |
| 121 | |
| 122 | public void putAllMetadata(Map<ResultMetadataType,Object> metadata) { |
| 123 | if (metadata != null) { |
| 124 | if (resultMetadata == null) { |
| 125 | resultMetadata = metadata; |
| 126 | } else { |
| 127 | resultMetadata.putAll(metadata); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | public void addResultPoints(ResultPoint[] newPoints) { |
| 133 | ResultPoint[] oldPoints = resultPoints; |
| 134 | if (oldPoints == null) { |
| 135 | resultPoints = newPoints; |
| 136 | } else if (newPoints != null && newPoints.length > 0) { |
| 137 | ResultPoint[] allPoints = new ResultPoint[oldPoints.length + newPoints.length]; |
| 138 | System.arraycopy(oldPoints, 0, allPoints, 0, oldPoints.length); |
| 139 | System.arraycopy(newPoints, 0, allPoints, oldPoints.length, newPoints.length); |
| 140 | resultPoints = allPoints; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | public long getTimestamp() { |
| 145 | return timestamp; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public String toString() { |
| 150 | return text; |
| 151 | } |
| 152 | |
| 153 | } |
| 154 | |