Refactorings to allow raw bytes to be passed back with reader result, where applicable

git-svn-id: https://zxing.googlecode.com/svn/trunk@270 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2008-03-12 18:51:46 +00:00
parent 727cc95858
commit ea13f8b052
15 changed files with 99 additions and 28 deletions

View file

@ -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();

View file

@ -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 <code>null</code>
*/
public String getText() {
return text;
}
/**
* @return raw bytes encoded by the barcode, if applicable, otherwise <code>null</code>
*/
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

View file

@ -16,6 +16,8 @@
package com.google.zxing.client.result;
import com.google.zxing.Result;
/**
* <p>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;

View file

@ -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;
/**
* <p>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.</p>
*
* @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;
}
}

View file

@ -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);
}
/**

View file

@ -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.</p>
*
* @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.</p>
*
* @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);
}
/**

View file

@ -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)},

View file

@ -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)},

View file

@ -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)},

View file

@ -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;
}

View file

@ -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);
}
/**

View file

@ -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.</p>
*
* @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 {
* <p>Decodes a QR Code represented as a {@link BitMatrix}. A 1 or "true" is taken to mean a black module.</p>
*
* @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);
}
/**

View file

@ -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());
}

View file

@ -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.");

View file

@ -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();