mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -08:00
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:
parent
90297efc57
commit
1eab5aba49
|
@ -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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -58,6 +58,7 @@ final class DecodedBitStreamParser {
|
||||||
BitSource bits = new BitSource(bytes);
|
BitSource bits = new BitSource(bytes);
|
||||||
StringBuffer result = new StringBuffer();
|
StringBuffer result = new StringBuffer();
|
||||||
CharacterSetECI currentCharacterSetECI = null;
|
CharacterSetECI currentCharacterSetECI = null;
|
||||||
|
boolean fc1InEffect = false;
|
||||||
Mode mode;
|
Mode mode;
|
||||||
do {
|
do {
|
||||||
// While still another segment to read...
|
// 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
|
mode = Mode.forBits(bits.readBits(4)); // mode is encoded by 4 bits
|
||||||
}
|
}
|
||||||
if (!mode.equals(Mode.TERMINATOR)) {
|
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
|
// Count doesn't apply to ECI
|
||||||
int value = ECI.parseECI(bits);
|
int value = ECI.parseECI(bits);
|
||||||
try {
|
try {
|
||||||
|
@ -82,7 +86,7 @@ final class DecodedBitStreamParser {
|
||||||
if (mode.equals(Mode.NUMERIC)) {
|
if (mode.equals(Mode.NUMERIC)) {
|
||||||
decodeNumericSegment(bits, result, count);
|
decodeNumericSegment(bits, result, count);
|
||||||
} else if (mode.equals(Mode.ALPHANUMERIC)) {
|
} else if (mode.equals(Mode.ALPHANUMERIC)) {
|
||||||
decodeAlphanumericSegment(bits, result, count);
|
decodeAlphanumericSegment(bits, result, count, fc1InEffect);
|
||||||
} else if (mode.equals(Mode.BYTE)) {
|
} else if (mode.equals(Mode.BYTE)) {
|
||||||
decodeByteSegment(bits, result, count, currentCharacterSetECI);
|
decodeByteSegment(bits, result, count, currentCharacterSetECI);
|
||||||
} else if (mode.equals(Mode.KANJI)) {
|
} else if (mode.equals(Mode.KANJI)) {
|
||||||
|
@ -168,8 +172,10 @@ final class DecodedBitStreamParser {
|
||||||
|
|
||||||
private static void decodeAlphanumericSegment(BitSource bits,
|
private static void decodeAlphanumericSegment(BitSource bits,
|
||||||
StringBuffer result,
|
StringBuffer result,
|
||||||
int count) {
|
int count,
|
||||||
|
boolean fc1InEffect) {
|
||||||
// Read two characters at a time
|
// Read two characters at a time
|
||||||
|
int start = result.length();
|
||||||
while (count > 1) {
|
while (count > 1) {
|
||||||
int nextTwoCharsBits = bits.readBits(11);
|
int nextTwoCharsBits = bits.readBits(11);
|
||||||
result.append(ALPHANUMERIC_CHARS[nextTwoCharsBits / 45]);
|
result.append(ALPHANUMERIC_CHARS[nextTwoCharsBits / 45]);
|
||||||
|
@ -180,6 +186,21 @@ final class DecodedBitStreamParser {
|
||||||
// special case: one character left
|
// special case: one character left
|
||||||
result.append(ALPHANUMERIC_CHARS[bits.readBits(6)]);
|
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,
|
private static void decodeNumericSegment(BitSource bits,
|
||||||
|
|
|
@ -34,6 +34,8 @@ final class Mode {
|
||||||
static final Mode BYTE = new Mode(new int[]{8, 16, 16});
|
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 ECI = new Mode(null); // character counts don't apply
|
||||||
static final Mode KANJI = new Mode(new int[]{8, 10, 12});
|
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;
|
private final int[] characterCountBitsForVersions;
|
||||||
|
|
||||||
|
@ -56,12 +58,16 @@ final class Mode {
|
||||||
return ALPHANUMERIC;
|
return ALPHANUMERIC;
|
||||||
case 0x4:
|
case 0x4:
|
||||||
return BYTE;
|
return BYTE;
|
||||||
|
case 0x5:
|
||||||
|
return FNC1_FIRST_POSITION;
|
||||||
case 0x7:
|
case 0x7:
|
||||||
return ECI;
|
return ECI;
|
||||||
case 0x8:
|
case 0x8:
|
||||||
return KANJI;
|
return KANJI;
|
||||||
|
case 0x9:
|
||||||
|
return FNC1_SECOND_POSITION;
|
||||||
default:
|
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}
|
* count of characters that will follow encoded in this {@link Mode}
|
||||||
*/
|
*/
|
||||||
int getCharacterCountBits(Version version) {
|
int getCharacterCountBits(Version version) {
|
||||||
if (this.equals(ECI)) {
|
if (characterCountBitsForVersions == null) {
|
||||||
throw new IllegalArgumentException("Character count doesn't apply to ECI mode");
|
throw new IllegalArgumentException("Character count doesn't apply to this mode");
|
||||||
}
|
}
|
||||||
int number = version.getVersionNumber();
|
int number = version.getVersionNumber();
|
||||||
int offset;
|
int offset;
|
||||||
|
|
Loading…
Reference in a new issue