Improvement to Shift_JIS encoding detector to avoid detecting some UTF-8 strings as Shift_JIS

git-svn-id: https://zxing.googlecode.com/svn/trunk@955 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2009-05-27 09:14:17 +00:00
parent 70eab118c1
commit 58c367d9ea
2 changed files with 15 additions and 6 deletions

View file

@ -285,10 +285,9 @@ final class DecodedBitStreamParser {
if (!lastWasPossibleDoubleByteStart && ((value >= 0xF0 && value <= 0xFF) || value == 0x80 || value == 0xA0)) { if (!lastWasPossibleDoubleByteStart && ((value >= 0xF0 && value <= 0xFF) || value == 0x80 || value == 0xA0)) {
canBeShiftJIS = false; 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 // These start double-byte characters in Shift_JIS. Let's see if it's followed by a valid
// second byte. // second byte.
sawDoubleByteStart = true;
if (lastWasPossibleDoubleByteStart) { if (lastWasPossibleDoubleByteStart) {
// If we just checked this and the last byte for being a valid double-byte // 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 // char, don't check starting on this byte. If this and the last byte
@ -299,13 +298,19 @@ final class DecodedBitStreamParser {
// ... otherwise do check to see if this plus the next byte form a valid // ... otherwise do check to see if this plus the next byte form a valid
// double byte pair encoding a character. // double byte pair encoding a character.
lastWasPossibleDoubleByteStart = true; lastWasPossibleDoubleByteStart = true;
if (i >= bytes.length - 1) {
canBeShiftJIS = false;
} else {
int nextValue = bytes[i + 1] & 0xFF; int nextValue = bytes[i + 1] & 0xFF;
if (nextValue < 0x40 || nextValue > 0xFC) { if (nextValue < 0x40 || nextValue > 0xFC) {
canBeShiftJIS = false; canBeShiftJIS = false;
} else {
sawDoubleByteStart = true;
} }
// There is some conflicting information out there about which bytes can follow which in // 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. // double-byte Shift_JIS characters. The rule above seems to be the one that matches practice.
} }
}
} else { } else {
lastWasPossibleDoubleByteStart = false; lastWasPossibleDoubleByteStart = false;
} }

View file

@ -41,6 +41,10 @@ public final class QRCodeWriterTestCase extends TestCase {
private static BufferedImage loadImage(String fileName) { private static BufferedImage loadImage(String fileName) {
try { try {
File file = new File(BASE_IMAGE_PATH + fileName); 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()); assertTrue("Please run from the 'core' directory", file.exists());
return ImageIO.read(file); return ImageIO.read(file);
} catch (IOException e) { } catch (IOException e) {