From ac64595afaef7135b2d190c4d19adc8886520b50 Mon Sep 17 00:00:00 2001 From: srowen Date: Fri, 13 Nov 2009 09:49:49 +0000 Subject: [PATCH] Be more conservative about choosing Kanji mode. Since we don't support mixed mode, can only use it when the input is all double-byte Kanji. git-svn-id: https://zxing.googlecode.com/svn/trunk@1107 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- .../google/zxing/qrcode/encoder/Encoder.java | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/core/src/com/google/zxing/qrcode/encoder/Encoder.java b/core/src/com/google/zxing/qrcode/encoder/Encoder.java index 1bfd1b055..6d59f32eb 100644 --- a/core/src/com/google/zxing/qrcode/encoder/Encoder.java +++ b/core/src/com/google/zxing/qrcode/encoder/Encoder.java @@ -174,11 +174,12 @@ public final class Encoder { /** * Choose the best mode by examining the content. Note that 'encoding' is used as a hint; - * if it is Shift_JIS then we assume the input is Kanji and return {@link Mode#KANJI}. + * if it is Shift_JIS, and the input is only double-byte Kanji, then we return {@link Mode#KANJI}. */ public static Mode chooseMode(String content, String encoding) { if ("Shift_JIS".equals(encoding)) { - return Mode.KANJI; + // Choose Kanji mode if all input are double-byte characters + return isOnlyDoubleByteKanji(content) ? Mode.KANJI : Mode.BYTE; } boolean hasNumeric = false; boolean hasAlphanumeric = false; @@ -200,6 +201,26 @@ public final class Encoder { return Mode.BYTE; } + private static boolean isOnlyDoubleByteKanji(String content) { + byte[] bytes; + try { + bytes = content.getBytes("Shift_JIS"); + } catch (UnsupportedEncodingException uee) { + return false; + } + int length = bytes.length; + if (length % 2 != 0) { + return false; + } + for (int i = 0; i < length; i += 2) { + int byte1 = bytes[i] & 0xFF; + if ((byte1 < 0x81 || byte1 > 0x9F) && (byte1 < 0xE0 || byte1 > 0xEB)) { + return false; + } + } + return true; + } + private static int chooseMaskPattern(BitVector bits, ErrorCorrectionLevel ecLevel, int version, ByteMatrix matrix) throws WriterException {