Added error correction level to Result

git-svn-id: https://zxing.googlecode.com/svn/trunk@1026 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2009-07-22 10:40:18 +00:00
parent 91d8e04d86
commit 4331605913
10 changed files with 32 additions and 9 deletions

View file

@ -51,6 +51,12 @@ public final class ResultMetadataType {
*/ */
public static final ResultMetadataType BYTE_SEGMENTS = new ResultMetadataType(); public static final ResultMetadataType BYTE_SEGMENTS = new ResultMetadataType();
/**
* Error correction level used, if applicable. The value type depends on the
* format, but is typically a String.
*/
public static final ResultMetadataType ERROR_CORRECTION_LEVEL = new ResultMetadataType();
private ResultMetadataType() { private ResultMetadataType() {
} }

View file

@ -16,6 +16,8 @@
package com.google.zxing.common; package com.google.zxing.common;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.util.Vector; import java.util.Vector;
/** /**
@ -30,14 +32,16 @@ public final class DecoderResult {
private final byte[] rawBytes; private final byte[] rawBytes;
private final String text; private final String text;
private final Vector byteSegments; private final Vector byteSegments;
private final ErrorCorrectionLevel ecLevel;
public DecoderResult(byte[] rawBytes, String text, Vector byteSegments) { public DecoderResult(byte[] rawBytes, String text, Vector byteSegments, ErrorCorrectionLevel ecLevel) {
if (rawBytes == null && text == null) { if (rawBytes == null && text == null) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
this.rawBytes = rawBytes; this.rawBytes = rawBytes;
this.text = text; this.text = text;
this.byteSegments = byteSegments; this.byteSegments = byteSegments;
this.ecLevel = ecLevel;
} }
public byte[] getRawBytes() { public byte[] getRawBytes() {
@ -52,4 +56,8 @@ public final class DecoderResult {
return byteSegments; return byteSegments;
} }
public ErrorCorrectionLevel getECLevel() {
return ecLevel;
}
} }

View file

@ -70,6 +70,9 @@ public final class DataMatrixReader implements Reader {
if (decoderResult.getByteSegments() != null) { if (decoderResult.getByteSegments() != null) {
result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, decoderResult.getByteSegments()); result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, decoderResult.getByteSegments());
} }
if (decoderResult.getECLevel() != null) {
result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, decoderResult.getECLevel().toString());
}
return result; return result;
} }

View file

@ -110,7 +110,7 @@ final class DecodedBitStreamParser {
if (resultTrailer.length() > 0) { if (resultTrailer.length() > 0) {
result.append(resultTrailer.toString()); result.append(resultTrailer.toString());
} }
return new DecoderResult(bytes, result.toString(), byteSegments.isEmpty() ? null : byteSegments); return new DecoderResult(bytes, result.toString(), byteSegments.isEmpty() ? null : byteSegments, null);
} }
/** /**

View file

@ -57,6 +57,9 @@ public final class QRCodeMultiReader extends QRCodeReader implements MultipleBar
if (decoderResult.getByteSegments() != null) { if (decoderResult.getByteSegments() != null) {
result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, decoderResult.getByteSegments()); result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, decoderResult.getByteSegments());
} }
if (decoderResult.getECLevel() != null) {
result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, decoderResult.getECLevel().toString());
}
results.addElement(result); results.addElement(result);
} catch (ReaderException re) { } catch (ReaderException re) {
// ignore and continue // ignore and continue

View file

@ -123,7 +123,7 @@ final class DecodedBitStreamParser {
throw ReaderException.getInstance(); throw ReaderException.getInstance();
} }
} }
return new DecoderResult(null, result.toString(), null); return new DecoderResult(null, result.toString(), null, null);
} }
/** /**

View file

@ -75,6 +75,9 @@ public class QRCodeReader implements Reader {
if (decoderResult.getByteSegments() != null) { if (decoderResult.getByteSegments() != null) {
result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, decoderResult.getByteSegments()); result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, decoderResult.getByteSegments());
} }
if (decoderResult.getECLevel() != null) {
result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, decoderResult.getECLevel().toString());
}
return result; return result;
} }

View file

@ -57,7 +57,7 @@ final class DecodedBitStreamParser {
private DecodedBitStreamParser() { private DecodedBitStreamParser() {
} }
static DecoderResult decode(byte[] bytes, Version version) throws ReaderException { static DecoderResult decode(byte[] bytes, Version version, ErrorCorrectionLevel ecLevel) throws ReaderException {
BitSource bits = new BitSource(bytes); BitSource bits = new BitSource(bytes);
StringBuffer result = new StringBuffer(); StringBuffer result = new StringBuffer();
CharacterSetECI currentCharacterSetECI = null; CharacterSetECI currentCharacterSetECI = null;
@ -109,7 +109,7 @@ final class DecodedBitStreamParser {
} }
} while (!mode.equals(Mode.TERMINATOR)); } while (!mode.equals(Mode.TERMINATOR));
return new DecoderResult(bytes, result.toString(), byteSegments.isEmpty() ? null : byteSegments); return new DecoderResult(bytes, result.toString(), byteSegments.isEmpty() ? null : byteSegments, ecLevel);
} }
private static void decodeKanjiSegment(BitSource bits, private static void decodeKanjiSegment(BitSource bits,

View file

@ -97,7 +97,7 @@ public final class Decoder {
} }
// Decode the contents of that stream of bytes // Decode the contents of that stream of bytes
return DecodedBitStreamParser.decode(resultBytes, version); return DecodedBitStreamParser.decode(resultBytes, version, ecLevel);
} }
/** /**

View file

@ -35,7 +35,7 @@ public final class DecodedBitStreamParserTestCase extends TestCase {
builder.write(0xF2, 8); builder.write(0xF2, 8);
builder.write(0xF3, 8); builder.write(0xF3, 8);
String result = DecodedBitStreamParser.decode(builder.toByteArray(), String result = DecodedBitStreamParser.decode(builder.toByteArray(),
Version.getVersionForNumber(1)).getText(); Version.getVersionForNumber(1), null).getText();
assertEquals("\u00f1\u00f2\u00f3", result); assertEquals("\u00f1\u00f2\u00f3", result);
} }
@ -47,7 +47,7 @@ public final class DecodedBitStreamParserTestCase extends TestCase {
builder.write(0xA2, 8); builder.write(0xA2, 8);
builder.write(0xA3, 8); builder.write(0xA3, 8);
String result = DecodedBitStreamParser.decode(builder.toByteArray(), String result = DecodedBitStreamParser.decode(builder.toByteArray(),
Version.getVersionForNumber(1)).getText(); Version.getVersionForNumber(1), null).getText();
assertEquals("\uff61\uff62\uff63", result); assertEquals("\uff61\uff62\uff63", result);
} }
@ -61,7 +61,7 @@ public final class DecodedBitStreamParserTestCase extends TestCase {
builder.write(0xA2, 8); builder.write(0xA2, 8);
builder.write(0xA3, 8); builder.write(0xA3, 8);
String result = DecodedBitStreamParser.decode(builder.toByteArray(), String result = DecodedBitStreamParser.decode(builder.toByteArray(),
Version.getVersionForNumber(1)).getText(); Version.getVersionForNumber(1), null).getText();
assertEquals("\u00ed\u00f3\u00fa", result); assertEquals("\u00ed\u00f3\u00fa", result);
} }