From f3880260808aac8cb22f216ec8e5c00e391e13e6 Mon Sep 17 00:00:00 2001 From: srowen Date: Wed, 12 Mar 2008 18:51:46 +0000 Subject: [PATCH] Refactorings to allow raw bytes to be passed back with reader result, where applicable git-svn-id: http://zxing.googlecode.com/svn/trunk@270 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- .../android/BarcodeReaderCaptureActivity.java | 4 +- core/src/com/google/zxing/Result.java | 13 ++++- .../client/result/ParsedReaderResult.java | 5 +- .../google/zxing/common/DecoderResult.java | 47 +++++++++++++++++++ .../zxing/datamatrix/DataMatrixReader.java | 9 ++-- .../zxing/datamatrix/decoder/Decoder.java | 12 +++-- .../zxing/oned/AbstractUPCEANReader.java | 1 + .../com/google/zxing/oned/Code128Reader.java | 1 + .../com/google/zxing/oned/Code39Reader.java | 1 + .../zxing/oned/MultiFormatUPCEANReader.java | 2 +- .../com/google/zxing/qrcode/QRCodeReader.java | 9 ++-- .../google/zxing/qrcode/decoder/Decoder.java | 12 +++-- .../result/ParsedReaderResultTestCase.java | 4 +- .../zxing/client/j2me/SnapshotThread.java | 2 +- .../google/zxing/client/j2me/ZXingMIDlet.java | 5 +- 15 files changed, 99 insertions(+), 28 deletions(-) create mode 100644 core/src/com/google/zxing/common/DecoderResult.java diff --git a/android/src/com/google/zxing/client/android/BarcodeReaderCaptureActivity.java b/android/src/com/google/zxing/client/android/BarcodeReaderCaptureActivity.java index 52c5d685..aefc7b5d 100644 --- a/android/src/com/google/zxing/client/android/BarcodeReaderCaptureActivity.java +++ b/android/src/com/google/zxing/client/android/BarcodeReaderCaptureActivity.java @@ -177,9 +177,9 @@ public final class BarcodeReaderCaptureActivity extends Activity { } private static ParsedReaderResult parseReaderResult(Result rawResult) { - String rawText = rawResult.getText(); - ParsedReaderResult readerResult = ParsedReaderResult.parseReaderResult(rawText); + ParsedReaderResult readerResult = ParsedReaderResult.parseReaderResult(rawResult); if (readerResult.getType().equals(ParsedReaderResultType.TEXT)) { + String rawText = rawResult.getText(); AndroidIntentParsedResult androidResult = AndroidIntentParsedResult.parse(rawText); if (androidResult != null) { Intent intent = androidResult.getIntent(); diff --git a/core/src/com/google/zxing/Result.java b/core/src/com/google/zxing/Result.java index d63743fb..b9d0f5a0 100644 --- a/core/src/com/google/zxing/Result.java +++ b/core/src/com/google/zxing/Result.java @@ -24,22 +24,31 @@ package com.google.zxing; public final class Result { private final String text; + private final byte[] rawBytes; private final ResultPoint[] resultPoints; private final BarcodeFormat format; - public Result(String text, ResultPoint[] resultPoints, BarcodeFormat format) { + public Result(String text, byte[] rawBytes, ResultPoint[] resultPoints, BarcodeFormat format) { this.text = text; + this.rawBytes = rawBytes; this.resultPoints = resultPoints; this.format = format; } /** - * @return raw text encoded by the barcode, if any + * @return raw text encoded by the barcode, if applicable, otherwise null */ public String getText() { return text; } + /** + * @return raw bytes encoded by the barcode, if applicable, otherwise null + */ + public byte[] getRawBytes() { + return rawBytes; + } + /** * @return points related to the barcode in the image. These are typically points * identifying finder patterns or the corners of the barcode. The exact meaning is diff --git a/core/src/com/google/zxing/client/result/ParsedReaderResult.java b/core/src/com/google/zxing/client/result/ParsedReaderResult.java index 69cd954d..9e390e71 100644 --- a/core/src/com/google/zxing/client/result/ParsedReaderResult.java +++ b/core/src/com/google/zxing/client/result/ParsedReaderResult.java @@ -16,6 +16,8 @@ package com.google.zxing.client.result; +import com.google.zxing.Result; + /** *

Abstract class representing the result of decoding a barcode, as more than * a String -- as some type of structured data. This might be a subclass which represents @@ -41,10 +43,11 @@ public abstract class ParsedReaderResult { public abstract String getDisplayResult(); - public static ParsedReaderResult parseReaderResult(String rawText) { + public static ParsedReaderResult parseReaderResult(Result theResult) { // This is a bit messy, but given limited options in MIDP / CLDC, this may well be the simplest // way to go about this. For example, we have no reflection available, really. // Order is important here. + String rawText = theResult.getText(); ParsedReaderResult result; if ((result = BookmarkDoCoMoResult.parse(rawText)) != null) { return result; diff --git a/core/src/com/google/zxing/common/DecoderResult.java b/core/src/com/google/zxing/common/DecoderResult.java new file mode 100644 index 00000000..ef1745f5 --- /dev/null +++ b/core/src/com/google/zxing/common/DecoderResult.java @@ -0,0 +1,47 @@ +/* + * Copyright 2007 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.zxing.common; + +/** + *

Encapsulates the result of decoding a matrix of bits. This typically + * applies to 2D barcode formats. For now it contains the raw bytes obtained, + * as well as a String interpretation of those bytes, if applicable.

+ * + * @author srowen@google.com (Sean Owen) + */ +public final class DecoderResult { + + private final byte[] rawBytes; + private final String text; + + public DecoderResult(byte[] rawBytes, String text) { + if (rawBytes == null && text == null) { + throw new IllegalArgumentException(); + } + this.rawBytes = rawBytes; + this.text = text; + } + + public byte[] getRawBytes() { + return rawBytes; + } + + public String getText() { + return text; + } + +} \ No newline at end of file diff --git a/core/src/com/google/zxing/datamatrix/DataMatrixReader.java b/core/src/com/google/zxing/datamatrix/DataMatrixReader.java index 16f1c029..ac23a157 100644 --- a/core/src/com/google/zxing/datamatrix/DataMatrixReader.java +++ b/core/src/com/google/zxing/datamatrix/DataMatrixReader.java @@ -23,6 +23,7 @@ import com.google.zxing.ReaderException; import com.google.zxing.Result; import com.google.zxing.ResultPoint; import com.google.zxing.common.BitMatrix; +import com.google.zxing.common.DecoderResult; import com.google.zxing.datamatrix.decoder.Decoder; import java.util.Hashtable; @@ -50,18 +51,18 @@ public final class DataMatrixReader implements Reader { public Result decode(MonochromeBitmapSource image, Hashtable hints) throws ReaderException { - String text; + DecoderResult decoderResult; ResultPoint[] points; //if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) { BitMatrix bits = extractPureBits(image); - text = decoder.decode(bits); + decoderResult = decoder.decode(bits); points = NO_POINTS; //} else { // DetectorResult result = new Detector(image).detect(); - // text = decoder.decode(result.getBits()); + // decoderResult = decoder.decode(result.getBits()); // points = result.getPoints(); //} - return new Result(text, points, BarcodeFormat.DATAMATRIX); + return new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.DATAMATRIX); } /** diff --git a/core/src/com/google/zxing/datamatrix/decoder/Decoder.java b/core/src/com/google/zxing/datamatrix/decoder/Decoder.java index 3280eb62..8bb59bc0 100644 --- a/core/src/com/google/zxing/datamatrix/decoder/Decoder.java +++ b/core/src/com/google/zxing/datamatrix/decoder/Decoder.java @@ -18,6 +18,7 @@ package com.google.zxing.datamatrix.decoder; import com.google.zxing.ReaderException; import com.google.zxing.common.BitMatrix; +import com.google.zxing.common.DecoderResult; import com.google.zxing.common.reedsolomon.GF256; import com.google.zxing.common.reedsolomon.ReedSolomonDecoder; import com.google.zxing.common.reedsolomon.ReedSolomonException; @@ -41,10 +42,10 @@ public final class Decoder { * "true" is taken to mean a black module.

* * @param image booleans representing white/black Data Matrix Code modules - * @return text encoded within the Data Matrix Code + * @return text and bytes encoded within the Data Matrix Code * @throws ReaderException if the Data Matrix Code cannot be decoded */ - public String decode(boolean[][] image) throws ReaderException { + public DecoderResult decode(boolean[][] image) throws ReaderException { int dimension = image.length; BitMatrix bits = new BitMatrix(dimension); for (int i = 0; i < dimension; i++) { @@ -62,10 +63,10 @@ public final class Decoder { * to mean a black module.

* * @param bits booleans representing white/black Data Matrix Code modules - * @return text encoded within the Data Matrix Code + * @return text and bytes encoded within the Data Matrix Code * @throws ReaderException if the Data Matrix Code cannot be decoded */ - public String decode(BitMatrix bits) throws ReaderException { + public DecoderResult decode(BitMatrix bits) throws ReaderException { // Construct a parser and read version, error-correction level BitMatrixParser parser = new BitMatrixParser(bits); @@ -96,7 +97,8 @@ public final class Decoder { } // Decode the contents of that stream of bytes - return DecodedBitStreamParser.decode(resultBytes); + String text = DecodedBitStreamParser.decode(resultBytes); + return new DecoderResult(resultBytes, text); } /** diff --git a/core/src/com/google/zxing/oned/AbstractUPCEANReader.java b/core/src/com/google/zxing/oned/AbstractUPCEANReader.java index 26262fd5..68132d12 100644 --- a/core/src/com/google/zxing/oned/AbstractUPCEANReader.java +++ b/core/src/com/google/zxing/oned/AbstractUPCEANReader.java @@ -123,6 +123,7 @@ public abstract class AbstractUPCEANReader extends AbstractOneDReader implements return new Result( resultString, + null, // no natural byte representation for these barcodes new ResultPoint[]{ new GenericResultPoint((float) (startGuardRange[1] - startGuardRange[0]) / 2.0f, (float) rowNumber), new GenericResultPoint((float) (endRange[1] - endRange[0]) / 2.0f, (float) rowNumber)}, diff --git a/core/src/com/google/zxing/oned/Code128Reader.java b/core/src/com/google/zxing/oned/Code128Reader.java index 96bf2c95..46d1bd78 100644 --- a/core/src/com/google/zxing/oned/Code128Reader.java +++ b/core/src/com/google/zxing/oned/Code128Reader.java @@ -407,6 +407,7 @@ public final class Code128Reader extends AbstractOneDReader { String resultString = result.toString(); return new Result( resultString, + null, new ResultPoint[]{ new GenericResultPoint((float) (startPatternInfo[1] - startPatternInfo[0]) / 2.0f, (float) rowNumber), new GenericResultPoint((float) (nextStart - lastStart) / 2.0f, (float) rowNumber)}, diff --git a/core/src/com/google/zxing/oned/Code39Reader.java b/core/src/com/google/zxing/oned/Code39Reader.java index 57f1009c..6ab9510b 100644 --- a/core/src/com/google/zxing/oned/Code39Reader.java +++ b/core/src/com/google/zxing/oned/Code39Reader.java @@ -140,6 +140,7 @@ public final class Code39Reader extends AbstractOneDReader { } return new Result( resultString, + null, new ResultPoint[]{ new GenericResultPoint((float) (start[1] - start[0]) / 2.0f, (float) rowNumber), new GenericResultPoint((float) (nextStart - lastStart) / 2.0f, (float) rowNumber)}, diff --git a/core/src/com/google/zxing/oned/MultiFormatUPCEANReader.java b/core/src/com/google/zxing/oned/MultiFormatUPCEANReader.java index 835504be..b31fbf48 100644 --- a/core/src/com/google/zxing/oned/MultiFormatUPCEANReader.java +++ b/core/src/com/google/zxing/oned/MultiFormatUPCEANReader.java @@ -81,7 +81,7 @@ public final class MultiFormatUPCEANReader extends AbstractOneDReader { // Here is, therefore, where we implement this logic: if (result.getBarcodeFormat().equals(BarcodeFormat.EAN_13) && result.getText().charAt(0) == '0') { - return new Result(result.getText().substring(1), result.getResultPoints(), BarcodeFormat.UPC_A); + return new Result(result.getText().substring(1), null, result.getResultPoints(), BarcodeFormat.UPC_A); } return result; } diff --git a/core/src/com/google/zxing/qrcode/QRCodeReader.java b/core/src/com/google/zxing/qrcode/QRCodeReader.java index 2a588423..34fe999b 100644 --- a/core/src/com/google/zxing/qrcode/QRCodeReader.java +++ b/core/src/com/google/zxing/qrcode/QRCodeReader.java @@ -24,6 +24,7 @@ import com.google.zxing.ReaderException; import com.google.zxing.Result; import com.google.zxing.ResultPoint; import com.google.zxing.common.BitMatrix; +import com.google.zxing.common.DecoderResult; import com.google.zxing.common.DetectorResult; import com.google.zxing.qrcode.decoder.Decoder; import com.google.zxing.qrcode.detector.Detector; @@ -53,18 +54,18 @@ public final class QRCodeReader implements Reader { public Result decode(MonochromeBitmapSource image, Hashtable hints) throws ReaderException { - String text; + DecoderResult decoderResult; ResultPoint[] points; if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) { BitMatrix bits = extractPureBits(image); - text = decoder.decode(bits); + decoderResult = decoder.decode(bits); points = NO_POINTS; } else { DetectorResult result = new Detector(image).detect(); - text = decoder.decode(result.getBits()); + decoderResult = decoder.decode(result.getBits()); points = result.getPoints(); } - return new Result(text, points, BarcodeFormat.QR_CODE); + return new Result(decoderResult.getText(), decoderResult.getRawBytes(), points, BarcodeFormat.QR_CODE); } /** diff --git a/core/src/com/google/zxing/qrcode/decoder/Decoder.java b/core/src/com/google/zxing/qrcode/decoder/Decoder.java index 4f732457..e22fea55 100644 --- a/core/src/com/google/zxing/qrcode/decoder/Decoder.java +++ b/core/src/com/google/zxing/qrcode/decoder/Decoder.java @@ -18,6 +18,7 @@ package com.google.zxing.qrcode.decoder; import com.google.zxing.ReaderException; import com.google.zxing.common.BitMatrix; +import com.google.zxing.common.DecoderResult; import com.google.zxing.common.reedsolomon.GF256; import com.google.zxing.common.reedsolomon.ReedSolomonDecoder; import com.google.zxing.common.reedsolomon.ReedSolomonException; @@ -41,10 +42,10 @@ public final class Decoder { * "true" is taken to mean a black module.

* * @param image booleans representing white/black QR Code modules - * @return text encoded within the QR Code + * @return text and bytes encoded within the QR Code * @throws ReaderException if the QR Code cannot be decoded */ - public String decode(boolean[][] image) throws ReaderException { + public DecoderResult decode(boolean[][] image) throws ReaderException { int dimension = image.length; BitMatrix bits = new BitMatrix(dimension); for (int i = 0; i < dimension; i++) { @@ -61,10 +62,10 @@ public final class Decoder { *

Decodes a QR Code represented as a {@link BitMatrix}. A 1 or "true" is taken to mean a black module.

* * @param bits booleans representing white/black QR Code modules - * @return text encoded within the QR Code + * @return text and bytes encoded within the QR Code * @throws ReaderException if the QR Code cannot be decoded */ - public String decode(BitMatrix bits) throws ReaderException { + public DecoderResult decode(BitMatrix bits) throws ReaderException { // Construct a parser and read version, error-correction level BitMatrixParser parser = new BitMatrixParser(bits); @@ -96,7 +97,8 @@ public final class Decoder { } // Decode the contents of that stream of bytes - return DecodedBitStreamParser.decode(resultBytes, version); + String text = DecodedBitStreamParser.decode(resultBytes, version); + return new DecoderResult(resultBytes, text); } /** diff --git a/core/test/src/com/google/zxing/client/result/ParsedReaderResultTestCase.java b/core/test/src/com/google/zxing/client/result/ParsedReaderResultTestCase.java index dc28f71f..33fdfc29 100644 --- a/core/test/src/com/google/zxing/client/result/ParsedReaderResultTestCase.java +++ b/core/test/src/com/google/zxing/client/result/ParsedReaderResultTestCase.java @@ -16,6 +16,7 @@ package com.google.zxing.client.result; +import com.google.zxing.Result; import junit.framework.TestCase; /** @@ -91,7 +92,8 @@ public final class ParsedReaderResultTestCase extends TestCase { } private static void doTestResult(String text, ParsedReaderResultType type) { - ParsedReaderResult result = ParsedReaderResult.parseReaderResult(text); + Result fakeResult = new Result(text, null, null, null); + ParsedReaderResult result = ParsedReaderResult.parseReaderResult(fakeResult); assertNotNull(result); assertEquals(type, result.getType()); } diff --git a/javame/src/com/google/zxing/client/j2me/SnapshotThread.java b/javame/src/com/google/zxing/client/j2me/SnapshotThread.java index 5d822ba6..6a5c7793 100644 --- a/javame/src/com/google/zxing/client/j2me/SnapshotThread.java +++ b/javame/src/com/google/zxing/client/j2me/SnapshotThread.java @@ -76,7 +76,7 @@ final class SnapshotThread extends Thread { MonochromeBitmapSource source = new LCDUIImageMonochromeBitmapSource(capturedImage); Reader reader = new MultiFormatReader(); Result result = reader.decode(source); - zXingMIDlet.handleDecodedText(result.getText()); + zXingMIDlet.handleDecodedText(result); } catch (ReaderException re) { // Show a friendlier message on a mere failure to read the barcode zXingMIDlet.showError("Sorry, no barcode was found."); diff --git a/javame/src/com/google/zxing/client/j2me/ZXingMIDlet.java b/javame/src/com/google/zxing/client/j2me/ZXingMIDlet.java index 662a558f..fffe2ab3 100644 --- a/javame/src/com/google/zxing/client/j2me/ZXingMIDlet.java +++ b/javame/src/com/google/zxing/client/j2me/ZXingMIDlet.java @@ -16,6 +16,7 @@ package com.google.zxing.client.j2me; +import com.google.zxing.Result; import com.google.zxing.client.result.BookmarkDoCoMoResult; import com.google.zxing.client.result.EmailAddressResult; import com.google.zxing.client.result.EmailDoCoMoResult; @@ -188,8 +189,8 @@ public final class ZXingMIDlet extends MIDlet { display.setCurrent(alert, canvas); } - void handleDecodedText(String text) { - ParsedReaderResult result = ParsedReaderResult.parseReaderResult(text); + void handleDecodedText(Result theResult) { + ParsedReaderResult result = ParsedReaderResult.parseReaderResult(theResult); ParsedReaderResultType type = result.getType(); if (type.equals(ParsedReaderResultType.URI)) { String uri = ((URIParsedResult) result).getURI(); -- 2.20.1