Issue 331

git-svn-id: https://zxing.googlecode.com/svn/trunk@1191 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2010-01-29 10:27:07 +00:00
parent 4ae280efcb
commit 171bd88c5d
4 changed files with 40 additions and 11 deletions

View file

@ -52,6 +52,11 @@ public final class DecodeHintType {
*/ */
public static final DecodeHintType TRY_HARDER = new DecodeHintType(); public static final DecodeHintType TRY_HARDER = new DecodeHintType();
/**
* Specifies what character encoding to use when decoding, where applicable (type String)
*/
public static final DecodeHintType CHARACTER_SET = new DecodeHintType();
/** /**
* Allowed lengths of encoded data -- reject anything else. Maps to an int[]. * Allowed lengths of encoded data -- reject anything else. Maps to an int[].
*/ */

View file

@ -63,11 +63,11 @@ public class QRCodeReader implements Reader {
ResultPoint[] points; ResultPoint[] points;
if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) { if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) {
BitMatrix bits = extractPureBits(image.getBlackMatrix()); BitMatrix bits = extractPureBits(image.getBlackMatrix());
decoderResult = decoder.decode(bits); decoderResult = decoder.decode(bits, hints);
points = NO_POINTS; points = NO_POINTS;
} else { } else {
DetectorResult detectorResult = new Detector(image.getBlackMatrix()).detect(hints); DetectorResult detectorResult = new Detector(image.getBlackMatrix()).detect(hints);
decoderResult = decoder.decode(detectorResult.getBits()); decoderResult = decoder.decode(detectorResult.getBits(), hints);
points = detectorResult.getPoints(); points = detectorResult.getPoints();
} }
@ -81,6 +81,10 @@ public class QRCodeReader implements Reader {
return result; return result;
} }
public void reset() {
// do nothing
}
/** /**
* This method detects a barcode in a "pure" image -- that is, pure monochrome image * This method detects a barcode in a "pure" image -- that is, pure monochrome image
* which contains only an unrotated, unskewed, image of a barcode, with some white border * which contains only an unrotated, unskewed, image of a barcode, with some white border

View file

@ -16,12 +16,14 @@
package com.google.zxing.qrcode.decoder; package com.google.zxing.qrcode.decoder;
import com.google.zxing.DecodeHintType;
import com.google.zxing.ReaderException; import com.google.zxing.ReaderException;
import com.google.zxing.common.BitSource; import com.google.zxing.common.BitSource;
import com.google.zxing.common.CharacterSetECI; import com.google.zxing.common.CharacterSetECI;
import com.google.zxing.common.DecoderResult; import com.google.zxing.common.DecoderResult;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.util.Hashtable;
import java.util.Vector; import java.util.Vector;
/** /**
@ -57,7 +59,8 @@ final class DecodedBitStreamParser {
private DecodedBitStreamParser() { private DecodedBitStreamParser() {
} }
static DecoderResult decode(byte[] bytes, Version version, ErrorCorrectionLevel ecLevel) throws ReaderException { static DecoderResult decode(byte[] bytes, Version version, ErrorCorrectionLevel ecLevel, Hashtable hints)
throws ReaderException {
BitSource bits = new BitSource(bytes); BitSource bits = new BitSource(bytes);
StringBuffer result = new StringBuffer(50); StringBuffer result = new StringBuffer(50);
CharacterSetECI currentCharacterSetECI = null; CharacterSetECI currentCharacterSetECI = null;
@ -99,7 +102,7 @@ final class DecodedBitStreamParser {
} else if (mode.equals(Mode.ALPHANUMERIC)) { } else if (mode.equals(Mode.ALPHANUMERIC)) {
decodeAlphanumericSegment(bits, result, count, fc1InEffect); decodeAlphanumericSegment(bits, result, count, fc1InEffect);
} else if (mode.equals(Mode.BYTE)) { } else if (mode.equals(Mode.BYTE)) {
decodeByteSegment(bits, result, count, currentCharacterSetECI, byteSegments); decodeByteSegment(bits, result, count, currentCharacterSetECI, byteSegments, hints);
} else if (mode.equals(Mode.KANJI)) { } else if (mode.equals(Mode.KANJI)) {
decodeKanjiSegment(bits, result, count); decodeKanjiSegment(bits, result, count);
} else { } else {
@ -147,7 +150,8 @@ final class DecodedBitStreamParser {
StringBuffer result, StringBuffer result,
int count, int count,
CharacterSetECI currentCharacterSetECI, CharacterSetECI currentCharacterSetECI,
Vector byteSegments) throws ReaderException { Vector byteSegments,
Hashtable hints) throws ReaderException {
byte[] readBytes = new byte[count]; byte[] readBytes = new byte[count];
if (count << 3 > bits.available()) { if (count << 3 > bits.available()) {
throw ReaderException.getInstance(); throw ReaderException.getInstance();
@ -162,7 +166,7 @@ final class DecodedBitStreamParser {
// upon decoding. I have seen ISO-8859-1 used as well as // upon decoding. I have seen ISO-8859-1 used as well as
// Shift_JIS -- without anything like an ECI designator to // Shift_JIS -- without anything like an ECI designator to
// give a hint. // give a hint.
encoding = guessEncoding(readBytes); encoding = guessEncoding(readBytes, hints);
} else { } else {
encoding = currentCharacterSetECI.getEncodingName(); encoding = currentCharacterSetECI.getEncodingName();
} }
@ -240,7 +244,13 @@ final class DecodedBitStreamParser {
} }
} }
private static String guessEncoding(byte[] bytes) { private static String guessEncoding(byte[] bytes, Hashtable hints) {
if (hints != null) {
String characterSet = (String) hints.get(DecodeHintType.CHARACTER_SET);
if (characterSet != null) {
return characterSet;
}
}
if (ASSUME_SHIFT_JIS) { if (ASSUME_SHIFT_JIS) {
return SHIFT_JIS; return SHIFT_JIS;
} }

View file

@ -23,6 +23,8 @@ import com.google.zxing.common.reedsolomon.GF256;
import com.google.zxing.common.reedsolomon.ReedSolomonDecoder; import com.google.zxing.common.reedsolomon.ReedSolomonDecoder;
import com.google.zxing.common.reedsolomon.ReedSolomonException; import com.google.zxing.common.reedsolomon.ReedSolomonException;
import java.util.Hashtable;
/** /**
* <p>The main class which implements QR Code decoding -- as opposed to locating and extracting * <p>The main class which implements QR Code decoding -- as opposed to locating and extracting
* the QR Code from an image.</p> * the QR Code from an image.</p>
@ -37,6 +39,10 @@ public final class Decoder {
rsDecoder = new ReedSolomonDecoder(GF256.QR_CODE_FIELD); rsDecoder = new ReedSolomonDecoder(GF256.QR_CODE_FIELD);
} }
public DecoderResult decode(boolean[][] image) throws ReaderException {
return decode(image, null);
}
/** /**
* <p>Convenience method that can decode a QR Code represented as a 2D array of booleans. * <p>Convenience method that can decode a QR Code represented as a 2D array of booleans.
* "true" is taken to mean a black module.</p> * "true" is taken to mean a black module.</p>
@ -45,7 +51,7 @@ public final class Decoder {
* @return text and bytes encoded within the QR Code * @return text and bytes encoded within the QR Code
* @throws ReaderException if the QR Code cannot be decoded * @throws ReaderException if the QR Code cannot be decoded
*/ */
public DecoderResult decode(boolean[][] image) throws ReaderException { public DecoderResult decode(boolean[][] image, Hashtable hints) throws ReaderException {
int dimension = image.length; int dimension = image.length;
BitMatrix bits = new BitMatrix(dimension); BitMatrix bits = new BitMatrix(dimension);
for (int i = 0; i < dimension; i++) { for (int i = 0; i < dimension; i++) {
@ -55,7 +61,11 @@ public final class Decoder {
} }
} }
} }
return decode(bits); return decode(bits, hints);
}
public DecoderResult decode(BitMatrix bits) throws ReaderException {
return decode(bits, null);
} }
/** /**
@ -65,7 +75,7 @@ public final class Decoder {
* @return text and bytes encoded within the QR Code * @return text and bytes encoded within the QR Code
* @throws ReaderException if the QR Code cannot be decoded * @throws ReaderException if the QR Code cannot be decoded
*/ */
public DecoderResult decode(BitMatrix bits) throws ReaderException { public DecoderResult decode(BitMatrix bits, Hashtable hints) throws ReaderException {
// Construct a parser and read version, error-correction level // Construct a parser and read version, error-correction level
BitMatrixParser parser = new BitMatrixParser(bits); BitMatrixParser parser = new BitMatrixParser(bits);
@ -97,7 +107,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, ecLevel); return DecodedBitStreamParser.decode(resultBytes, version, ecLevel, hints);
} }
/** /**