Factor out ECI-related code for reuse with Data Matrix later.

git-svn-id: https://zxing.googlecode.com/svn/trunk@534 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2008-07-24 21:37:37 +00:00
parent a43e928575
commit 124130f6a3
3 changed files with 143 additions and 1 deletions

View file

@ -0,0 +1,77 @@
/*
* 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.common;
import java.util.Hashtable;
/**
* Encapsulates a Character Set ECI, according to "Extended Channel Interpretations" 5.3.1.1
* of ISO 18004.
*
* @author srowen@google.com (Sean Owen)
*/
public final class CharacterSetECI extends ECI {
private static final Hashtable VALUE_TO_ECI;
static {
VALUE_TO_ECI = new Hashtable(29);
// TODO figure out if these values are even right!
addCharacterSet(0, "Cp437");
addCharacterSet(1, "ISO8859_1");
addCharacterSet(2, "Cp437");
addCharacterSet(3, "ISO8859_1");
addCharacterSet(4, "ISO8859_2");
addCharacterSet(5, "ISO8859_3");
addCharacterSet(6, "ISO8859_4");
addCharacterSet(7, "ISO8859_5");
addCharacterSet(8, "ISO8859_6");
addCharacterSet(9, "ISO8859_7");
addCharacterSet(10, "ISO8859_8");
addCharacterSet(11, "ISO8859_9");
addCharacterSet(12, "ISO8859_10");
addCharacterSet(13, "ISO8859_11");
addCharacterSet(15, "ISO8859_13");
addCharacterSet(16, "ISO8859_14");
addCharacterSet(17, "ISO8859_15");
addCharacterSet(18, "ISO8859_16");
addCharacterSet(20, "SJIS");
}
private final String encodingName;
private CharacterSetECI(int value, String encodingName) {
super(value);
this.encodingName = encodingName;
}
public String getEncodingName() {
return encodingName;
}
private static void addCharacterSet(int value, String encodingName) {
VALUE_TO_ECI.put(new Integer(value), new CharacterSetECI(value, encodingName));
}
public static CharacterSetECI getCharacterSetECIByValue(int value) {
CharacterSetECI eci = (CharacterSetECI) VALUE_TO_ECI.get(new Integer(value));
if (eci == null) {
throw new IllegalArgumentException("Unsupported value: " + value);
}
return eci;
}
}

View file

@ -0,0 +1,47 @@
/*
* 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.common;
/**
* Superclass of classes encapsulating types ECIs, according to "Extended Channel Interpretations" 5.3
* of ISO 18004.
*
* @author srowen@google.com (Sean Owen)
*/
public abstract class ECI {
private final int value;
ECI(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public static ECI getECIByValue(int value) {
if (value < 0 || value > 999999) {
throw new IllegalArgumentException("Bad ECI value: " + value);
}
if (value < 900) { // Character set ECIs use 000000 - 000899
return CharacterSetECI.getCharacterSetECIByValue(value);
}
throw new IllegalArgumentException("Unsupported ECI value: " + value);
}
}

View file

@ -18,6 +18,7 @@ package com.google.zxing.qrcode.decoder;
import com.google.zxing.ReaderException;
import com.google.zxing.common.BitSource;
import com.google.zxing.common.CharacterSetECI;
import java.io.UnsupportedEncodingException;
@ -74,7 +75,7 @@ final class DecodedBitStreamParser {
fc1InEffect = true;
} else if (mode.equals(Mode.ECI)) {
// Count doesn't apply to ECI
int value = ECI.parseECI(bits);
int value = parseECIValue(bits);
try {
currentCharacterSetECI = CharacterSetECI.getCharacterSetECIByValue(value);
} catch (IllegalArgumentException iae) {
@ -290,5 +291,22 @@ final class DecodedBitStreamParser {
}
return canBeISO88591 ? ISO88591 : SHIFT_JIS;
}
private static int parseECIValue(BitSource bits) {
int firstByte = bits.readBits(8);
if ((firstByte & 0x80) == 0) {
// just one byte
return firstByte & 0x7F;
} else if ((firstByte & 0xC0) == 0x80) {
// two bytes
int secondByte = bits.readBits(8);
return ((firstByte & 0x3F) << 8) | secondByte;
} else if ((firstByte & 0xE0) == 0xC0) {
// three bytes
int secondThirdBytes = bits.readBits(16);
return ((firstByte & 0x1F) << 16) | secondThirdBytes;
}
throw new IllegalArgumentException("Bad ECI bits starting with byte " + firstByte);
}
}