Add minimal support for FNC1 mode in QR Code

git-svn-id: https://zxing.googlecode.com/svn/trunk@489 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2008-06-27 18:47:31 +00:00
parent 90297efc57
commit 1eab5aba49
3 changed files with 33 additions and 70 deletions

View file

@ -1,64 +0,0 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.client.result;
import com.google.zxing.Result;
/**
* Parses an "sms:" URI result, which specifies a number to SMS and optional
* "via" number. See <a href="http://gbiv.com/protocols/uri/drafts/draft-antti-gsm-sms-url-04.txt">
* the IETF draft</a> on this.
*
* @author srowen@google.com (Sean Owen)
*/
public final class SMSResultParser extends ResultParser {
private SMSResultParser() {
}
public static SMSParsedResult parse(Result result) {
String rawText = result.getText();
if (rawText == null || !rawText.startsWith("sms:")) {
return null;
}
// Drop sms, query portion
int queryStart = rawText.indexOf('?', 4);
String smsURIWithoutQuery;
if (queryStart < 0) {
smsURIWithoutQuery = rawText.substring(4);
} else {
smsURIWithoutQuery = rawText.substring(4, queryStart);
}
int numberEnd = smsURIWithoutQuery.indexOf(';');
String number;
String via;
if (numberEnd < 0) {
number = smsURIWithoutQuery;
via = null;
} else {
number = smsURIWithoutQuery.substring(0, numberEnd);
String maybeVia = smsURIWithoutQuery.substring(numberEnd + 1);
if (maybeVia.startsWith("via=")) {
via = maybeVia.substring(4);
} else {
via = null;
}
}
return new SMSParsedResult(rawText, number, via, null, null, null);
}
}

View file

@ -58,6 +58,7 @@ final class DecodedBitStreamParser {
BitSource bits = new BitSource(bytes);
StringBuffer result = new StringBuffer();
CharacterSetECI currentCharacterSetECI = null;
boolean fc1InEffect = false;
Mode mode;
do {
// While still another segment to read...
@ -68,7 +69,10 @@ final class DecodedBitStreamParser {
mode = Mode.forBits(bits.readBits(4)); // mode is encoded by 4 bits
}
if (!mode.equals(Mode.TERMINATOR)) {
if (mode.equals(Mode.ECI)) {
if (mode.equals(Mode.FNC1_FIRST_POSITION) || mode.equals(Mode.FNC1_SECOND_POSITION)) {
// We do little with FNC1 except alter the parsed result a bit according to the spec
fc1InEffect = true;
} else if (mode.equals(Mode.ECI)) {
// Count doesn't apply to ECI
int value = ECI.parseECI(bits);
try {
@ -82,7 +86,7 @@ final class DecodedBitStreamParser {
if (mode.equals(Mode.NUMERIC)) {
decodeNumericSegment(bits, result, count);
} else if (mode.equals(Mode.ALPHANUMERIC)) {
decodeAlphanumericSegment(bits, result, count);
decodeAlphanumericSegment(bits, result, count, fc1InEffect);
} else if (mode.equals(Mode.BYTE)) {
decodeByteSegment(bits, result, count, currentCharacterSetECI);
} else if (mode.equals(Mode.KANJI)) {
@ -168,8 +172,10 @@ final class DecodedBitStreamParser {
private static void decodeAlphanumericSegment(BitSource bits,
StringBuffer result,
int count) {
int count,
boolean fc1InEffect) {
// Read two characters at a time
int start = result.length();
while (count > 1) {
int nextTwoCharsBits = bits.readBits(11);
result.append(ALPHANUMERIC_CHARS[nextTwoCharsBits / 45]);
@ -180,6 +186,21 @@ final class DecodedBitStreamParser {
// special case: one character left
result.append(ALPHANUMERIC_CHARS[bits.readBits(6)]);
}
// See section 6.4.8.1, 6.4.8.2
if (fc1InEffect) {
// We need to massage the result a bit if in an FNC1 mode:
for (int i = start; i < result.length(); i++) {
if (result.charAt(i) == '%') {
if (i < result.length() - 1 && result.charAt(i + 1) == '%') {
// %% is rendered as %
result.deleteCharAt(i + 1);
} else {
// In alpha mode, % should be converted to FNC1 separator 0x1D
result.setCharAt(i, (char) 0x1D);
}
}
}
}
}
private static void decodeNumericSegment(BitSource bits,

View file

@ -34,6 +34,8 @@ final class Mode {
static final Mode BYTE = new Mode(new int[]{8, 16, 16});
static final Mode ECI = new Mode(null); // character counts don't apply
static final Mode KANJI = new Mode(new int[]{8, 10, 12});
static final Mode FNC1_FIRST_POSITION = new Mode(null);
static final Mode FNC1_SECOND_POSITION = new Mode(null);
private final int[] characterCountBitsForVersions;
@ -56,12 +58,16 @@ final class Mode {
return ALPHANUMERIC;
case 0x4:
return BYTE;
case 0x5:
return FNC1_FIRST_POSITION;
case 0x7:
return ECI;
case 0x8:
return KANJI;
case 0x9:
return FNC1_SECOND_POSITION;
default:
throw new ReaderException("Illegal mode bits: " + bits);
throw new ReaderException("Unsupported mode bits: " + bits);
}
}
@ -71,8 +77,8 @@ final class Mode {
* count of characters that will follow encoded in this {@link Mode}
*/
int getCharacterCountBits(Version version) {
if (this.equals(ECI)) {
throw new IllegalArgumentException("Character count doesn't apply to ECI mode");
if (characterCountBitsForVersions == null) {
throw new IllegalArgumentException("Character count doesn't apply to this mode");
}
int number = version.getVersionNumber();
int offset;