mirror of
https://github.com/zxing/zxing.git
synced 2024-11-09 20:44:03 -08:00
Issue #105 avoid accessing Charset Cp437 until absolutely necessary, as it won't work on Android sometimes
This commit is contained in:
parent
699471b018
commit
b09290c7dc
|
@ -528,7 +528,7 @@ public final class PDF417 {
|
|||
public PDF417(boolean compact) {
|
||||
this.compact = compact;
|
||||
compaction = Compaction.AUTO;
|
||||
encoding = PDF417HighLevelEncoder.DEFAULT_ENCODING;
|
||||
encoding = null; // Use default
|
||||
minCols = 2;
|
||||
maxCols = 30;
|
||||
maxRows = 30;
|
||||
|
|
|
@ -25,7 +25,9 @@ import com.google.zxing.common.CharacterSetECI;
|
|||
|
||||
import java.math.BigInteger;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.UnsupportedCharsetException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* PDF417 high-level encoder following the algorithm described in ISO/IEC 15438:2001(E) in
|
||||
|
@ -125,7 +127,7 @@ final class PDF417HighLevelEncoder {
|
|||
private static final byte[] MIXED = new byte[128];
|
||||
private static final byte[] PUNCTUATION = new byte[128];
|
||||
|
||||
static final Charset DEFAULT_ENCODING = Charset.forName("Cp437");
|
||||
private static final List<String> DEFAULT_ENCODING_NAMES = Arrays.asList("Cp437", "IBM437");
|
||||
|
||||
private PDF417HighLevelEncoder() {
|
||||
}
|
||||
|
@ -154,6 +156,9 @@ final class PDF417HighLevelEncoder {
|
|||
* is used.
|
||||
*
|
||||
* @param msg the message
|
||||
* @param compaction compaction mode to use
|
||||
* @param encoding character encoding used to encode in default or byte compaction
|
||||
* or {@code null} for default / not applicable
|
||||
* @return the encoded message (the char values range from 0 to 928)
|
||||
*/
|
||||
static String encodeHighLevel(String msg, Compaction compaction, Charset encoding) throws WriterException {
|
||||
|
@ -161,7 +166,7 @@ final class PDF417HighLevelEncoder {
|
|||
//the codewords 0..928 are encoded as Unicode characters
|
||||
StringBuilder sb = new StringBuilder(msg.length());
|
||||
|
||||
if (!DEFAULT_ENCODING.equals(encoding)) {
|
||||
if (encoding != null || !DEFAULT_ENCODING_NAMES.contains(encoding.name())) {
|
||||
CharacterSetECI eci = CharacterSetECI.getCharacterSetECIByName(encoding.name());
|
||||
if (eci != null) {
|
||||
encodingECI(eci.getValue(), sb);
|
||||
|
@ -178,7 +183,7 @@ final class PDF417HighLevelEncoder {
|
|||
encodeText(msg, p, len, sb, textSubMode);
|
||||
|
||||
} else if (compaction == Compaction.BYTE) {
|
||||
bytes = msg.getBytes(encoding);
|
||||
bytes = toBytes(msg, encoding);
|
||||
encodeBinary(bytes, p, bytes.length, BYTE_COMPACTION, sb);
|
||||
|
||||
} else if (compaction == Compaction.NUMERIC) {
|
||||
|
@ -207,7 +212,7 @@ final class PDF417HighLevelEncoder {
|
|||
p += t;
|
||||
} else {
|
||||
if (bytes == null) {
|
||||
bytes = msg.getBytes(encoding);
|
||||
bytes = toBytes(msg, encoding);
|
||||
}
|
||||
int b = determineConsecutiveBinaryCount(msg, bytes, p);
|
||||
if (b == 0) {
|
||||
|
@ -231,6 +236,24 @@ final class PDF417HighLevelEncoder {
|
|||
return sb.toString();
|
||||
}
|
||||
|
||||
private static byte[] toBytes(String msg, Charset encoding) throws WriterException {
|
||||
// Defer instantiating default Charset until needed, since it may be for an unsupported
|
||||
// encoding. For example the default of Cp437 doesn't seem to exist on Android.
|
||||
if (encoding == null) {
|
||||
for (String encodingName : DEFAULT_ENCODING_NAMES) {
|
||||
try {
|
||||
encoding = Charset.forName(encodingName);
|
||||
} catch (UnsupportedCharsetException uce) {
|
||||
// continue
|
||||
}
|
||||
}
|
||||
if (encoding == null) {
|
||||
throw new WriterException("No support for any encoding: " + DEFAULT_ENCODING_NAMES);
|
||||
}
|
||||
}
|
||||
return msg.getBytes(encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode parts of the message using Text Compaction as described in ISO/IEC 15438:2001(E),
|
||||
* chapter 4.4.2.
|
||||
|
|
Loading…
Reference in a new issue