From 1a5c747153f91b65b9e5a188e2d8027a659c30c5 Mon Sep 17 00:00:00 2001 From: srowen Date: Wed, 27 May 2009 09:14:17 +0000 Subject: [PATCH 1/1] Improvement to Shift_JIS encoding detector to avoid detecting some UTF-8 strings as Shift_JIS git-svn-id: http://zxing.googlecode.com/svn/trunk@955 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- .../qrcode/decoder/DecodedBitStreamParser.java | 17 +++++++++++------ .../zxing/qrcode/QRCodeWriterTestCase.java | 4 ++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/core/src/com/google/zxing/qrcode/decoder/DecodedBitStreamParser.java b/core/src/com/google/zxing/qrcode/decoder/DecodedBitStreamParser.java index 8ce3ee3b..c3a440b9 100644 --- a/core/src/com/google/zxing/qrcode/decoder/DecodedBitStreamParser.java +++ b/core/src/com/google/zxing/qrcode/decoder/DecodedBitStreamParser.java @@ -285,10 +285,9 @@ final class DecodedBitStreamParser { if (!lastWasPossibleDoubleByteStart && ((value >= 0xF0 && value <= 0xFF) || value == 0x80 || value == 0xA0)) { canBeShiftJIS = false; } - if (((value >= 0x81 && value <= 0x9F) || (value >= 0xE0 && value <= 0xEF)) && i < length - 1) { + if (((value >= 0x81 && value <= 0x9F) || (value >= 0xE0 && value <= 0xEF))) { // These start double-byte characters in Shift_JIS. Let's see if it's followed by a valid // second byte. - sawDoubleByteStart = true; if (lastWasPossibleDoubleByteStart) { // If we just checked this and the last byte for being a valid double-byte // char, don't check starting on this byte. If this and the last byte @@ -299,12 +298,18 @@ final class DecodedBitStreamParser { // ... otherwise do check to see if this plus the next byte form a valid // double byte pair encoding a character. lastWasPossibleDoubleByteStart = true; - int nextValue = bytes[i + 1] & 0xFF; - if (nextValue < 0x40 || nextValue > 0xFC) { + if (i >= bytes.length - 1) { canBeShiftJIS = false; + } else { + int nextValue = bytes[i + 1] & 0xFF; + if (nextValue < 0x40 || nextValue > 0xFC) { + canBeShiftJIS = false; + } else { + sawDoubleByteStart = true; + } + // There is some conflicting information out there about which bytes can follow which in + // double-byte Shift_JIS characters. The rule above seems to be the one that matches practice. } - // There is some conflicting information out there about which bytes can follow which in - // double-byte Shift_JIS characters. The rule above seems to be the one that matches practice. } } else { lastWasPossibleDoubleByteStart = false; diff --git a/core/test/src/com/google/zxing/qrcode/QRCodeWriterTestCase.java b/core/test/src/com/google/zxing/qrcode/QRCodeWriterTestCase.java index c50b7da3..c63b3757 100644 --- a/core/test/src/com/google/zxing/qrcode/QRCodeWriterTestCase.java +++ b/core/test/src/com/google/zxing/qrcode/QRCodeWriterTestCase.java @@ -41,6 +41,10 @@ public final class QRCodeWriterTestCase extends TestCase { private static BufferedImage loadImage(String fileName) { try { File file = new File(BASE_IMAGE_PATH + fileName); + if (!file.exists()) { + // try starting with 'core' since the test base is often given as the project root + file = new File("core/" + BASE_IMAGE_PATH + fileName); + } assertTrue("Please run from the 'core' directory", file.exists()); return ImageIO.read(file); } catch (IOException e) { -- 2.20.1