From de6f57f5cfd923b42c2c9665b2db381c3a7a3f53 Mon Sep 17 00:00:00 2001 From: srowen Date: Fri, 29 Jan 2010 10:27:07 +0000 Subject: [PATCH] Issue 331 git-svn-id: http://zxing.googlecode.com/svn/trunk@1191 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- core/src/com/google/zxing/DecodeHintType.java | 5 +++++ .../com/google/zxing/qrcode/QRCodeReader.java | 8 ++++++-- .../decoder/DecodedBitStreamParser.java | 20 ++++++++++++++----- .../google/zxing/qrcode/decoder/Decoder.java | 18 +++++++++++++---- 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/core/src/com/google/zxing/DecodeHintType.java b/core/src/com/google/zxing/DecodeHintType.java index 2c863dd0..20b922ca 100644 --- a/core/src/com/google/zxing/DecodeHintType.java +++ b/core/src/com/google/zxing/DecodeHintType.java @@ -52,6 +52,11 @@ public final class 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[]. */ diff --git a/core/src/com/google/zxing/qrcode/QRCodeReader.java b/core/src/com/google/zxing/qrcode/QRCodeReader.java index 0341f91d..52002ae7 100644 --- a/core/src/com/google/zxing/qrcode/QRCodeReader.java +++ b/core/src/com/google/zxing/qrcode/QRCodeReader.java @@ -63,11 +63,11 @@ public class QRCodeReader implements Reader { ResultPoint[] points; if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) { BitMatrix bits = extractPureBits(image.getBlackMatrix()); - decoderResult = decoder.decode(bits); + decoderResult = decoder.decode(bits, hints); points = NO_POINTS; } else { DetectorResult detectorResult = new Detector(image.getBlackMatrix()).detect(hints); - decoderResult = decoder.decode(detectorResult.getBits()); + decoderResult = decoder.decode(detectorResult.getBits(), hints); points = detectorResult.getPoints(); } @@ -81,6 +81,10 @@ public class QRCodeReader implements Reader { return result; } + public void reset() { + // do nothing + } + /** * 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 diff --git a/core/src/com/google/zxing/qrcode/decoder/DecodedBitStreamParser.java b/core/src/com/google/zxing/qrcode/decoder/DecodedBitStreamParser.java index dc6ad78c..55ff6363 100644 --- a/core/src/com/google/zxing/qrcode/decoder/DecodedBitStreamParser.java +++ b/core/src/com/google/zxing/qrcode/decoder/DecodedBitStreamParser.java @@ -16,12 +16,14 @@ package com.google.zxing.qrcode.decoder; +import com.google.zxing.DecodeHintType; import com.google.zxing.ReaderException; import com.google.zxing.common.BitSource; import com.google.zxing.common.CharacterSetECI; import com.google.zxing.common.DecoderResult; import java.io.UnsupportedEncodingException; +import java.util.Hashtable; import java.util.Vector; /** @@ -57,7 +59,8 @@ final class 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); StringBuffer result = new StringBuffer(50); CharacterSetECI currentCharacterSetECI = null; @@ -99,7 +102,7 @@ final class DecodedBitStreamParser { } else if (mode.equals(Mode.ALPHANUMERIC)) { decodeAlphanumericSegment(bits, result, count, fc1InEffect); } 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)) { decodeKanjiSegment(bits, result, count); } else { @@ -147,7 +150,8 @@ final class DecodedBitStreamParser { StringBuffer result, int count, CharacterSetECI currentCharacterSetECI, - Vector byteSegments) throws ReaderException { + Vector byteSegments, + Hashtable hints) throws ReaderException { byte[] readBytes = new byte[count]; if (count << 3 > bits.available()) { throw ReaderException.getInstance(); @@ -162,7 +166,7 @@ final class DecodedBitStreamParser { // upon decoding. I have seen ISO-8859-1 used as well as // Shift_JIS -- without anything like an ECI designator to // give a hint. - encoding = guessEncoding(readBytes); + encoding = guessEncoding(readBytes, hints); } else { 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) { return SHIFT_JIS; } diff --git a/core/src/com/google/zxing/qrcode/decoder/Decoder.java b/core/src/com/google/zxing/qrcode/decoder/Decoder.java index 26394e57..6a576f88 100644 --- a/core/src/com/google/zxing/qrcode/decoder/Decoder.java +++ b/core/src/com/google/zxing/qrcode/decoder/Decoder.java @@ -23,6 +23,8 @@ import com.google.zxing.common.reedsolomon.GF256; import com.google.zxing.common.reedsolomon.ReedSolomonDecoder; import com.google.zxing.common.reedsolomon.ReedSolomonException; +import java.util.Hashtable; + /** *

The main class which implements QR Code decoding -- as opposed to locating and extracting * the QR Code from an image.

@@ -37,6 +39,10 @@ public final class Decoder { rsDecoder = new ReedSolomonDecoder(GF256.QR_CODE_FIELD); } + public DecoderResult decode(boolean[][] image) throws ReaderException { + return decode(image, null); + } + /** *

Convenience method that can decode a QR Code represented as a 2D array of booleans. * "true" is taken to mean a black module.

@@ -45,7 +51,7 @@ public final class Decoder { * @return text and bytes encoded within the QR Code * @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; BitMatrix bits = new BitMatrix(dimension); 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 * @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 BitMatrixParser parser = new BitMatrixParser(bits); @@ -97,7 +107,7 @@ public final class Decoder { } // Decode the contents of that stream of bytes - return DecodedBitStreamParser.decode(resultBytes, version, ecLevel); + return DecodedBitStreamParser.decode(resultBytes, version, ecLevel, hints); } /** -- 2.20.1