}
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();
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
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
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;
--- /dev/null
+/*
+ * 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;
+ }
+
+}
\ No newline at end of file
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;
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);
}
/**
\r
import com.google.zxing.ReaderException;\r
import com.google.zxing.common.BitMatrix;\r
+import com.google.zxing.common.DecoderResult;\r
import com.google.zxing.common.reedsolomon.GF256;\r
import com.google.zxing.common.reedsolomon.ReedSolomonDecoder;\r
import com.google.zxing.common.reedsolomon.ReedSolomonException;\r
* "true" is taken to mean a black module.</p>\r
*\r
* @param image booleans representing white/black Data Matrix Code modules\r
- * @return text encoded within the Data Matrix Code\r
+ * @return text and bytes encoded within the Data Matrix Code\r
* @throws ReaderException if the Data Matrix Code cannot be decoded\r
*/\r
- public String decode(boolean[][] image) throws ReaderException {\r
+ public DecoderResult decode(boolean[][] image) throws ReaderException {\r
int dimension = image.length;\r
BitMatrix bits = new BitMatrix(dimension);\r
for (int i = 0; i < dimension; i++) {\r
* to mean a black module.</p>\r
*\r
* @param bits booleans representing white/black Data Matrix Code modules\r
- * @return text encoded within the Data Matrix Code\r
+ * @return text and bytes encoded within the Data Matrix Code\r
* @throws ReaderException if the Data Matrix Code cannot be decoded\r
*/\r
- public String decode(BitMatrix bits) throws ReaderException {\r
+ public DecoderResult decode(BitMatrix bits) throws ReaderException {\r
\r
// Construct a parser and read version, error-correction level\r
BitMatrixParser parser = new BitMatrixParser(bits);\r
}\r
\r
// Decode the contents of that stream of bytes\r
- return DecodedBitStreamParser.decode(resultBytes);\r
+ String text = DecodedBitStreamParser.decode(resultBytes);\r
+ return new DecoderResult(resultBytes, text);\r
}\r
\r
/**\r
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)},
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)},
}
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)},
// 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;
}
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;
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);
}
/**
\r
import com.google.zxing.ReaderException;\r
import com.google.zxing.common.BitMatrix;\r
+import com.google.zxing.common.DecoderResult;\r
import com.google.zxing.common.reedsolomon.GF256;\r
import com.google.zxing.common.reedsolomon.ReedSolomonDecoder;\r
import com.google.zxing.common.reedsolomon.ReedSolomonException;\r
* "true" is taken to mean a black module.</p>\r
*\r
* @param image booleans representing white/black QR Code modules\r
- * @return text encoded within the QR Code\r
+ * @return text and bytes encoded within the QR Code\r
* @throws ReaderException if the QR Code cannot be decoded\r
*/\r
- public String decode(boolean[][] image) throws ReaderException {\r
+ public DecoderResult decode(boolean[][] image) throws ReaderException {\r
int dimension = image.length;\r
BitMatrix bits = new BitMatrix(dimension);\r
for (int i = 0; i < dimension; i++) {\r
* <p>Decodes a QR Code represented as a {@link BitMatrix}. A 1 or "true" is taken to mean a black module.</p>\r
*\r
* @param bits booleans representing white/black QR Code modules\r
- * @return text encoded within the QR Code\r
+ * @return text and bytes encoded within the QR Code\r
* @throws ReaderException if the QR Code cannot be decoded\r
*/\r
- public String decode(BitMatrix bits) throws ReaderException {\r
+ public DecoderResult decode(BitMatrix bits) throws ReaderException {\r
\r
// Construct a parser and read version, error-correction level\r
BitMatrixParser parser = new BitMatrixParser(bits);\r
}\r
\r
// Decode the contents of that stream of bytes\r
- return DecodedBitStreamParser.decode(resultBytes, version);\r
+ String text = DecodedBitStreamParser.decode(resultBytes, version);\r
+ return new DecoderResult(resultBytes, text);\r
}\r
\r
/**\r
package com.google.zxing.client.result;
+import com.google.zxing.Result;
import junit.framework.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());
}
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.");
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;
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();