mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -08:00
Fixed multibyte charset problem when encoding PDF417 with Compaction AUTO
This commit is contained in:
parent
060010253c
commit
849f81354c
|
@ -25,6 +25,7 @@ import com.google.zxing.common.CharacterSetECI;
|
||||||
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
import java.nio.charset.CharsetEncoder;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -178,12 +179,11 @@ final class PDF417HighLevelEncoder {
|
||||||
int textSubMode = SUBMODE_ALPHA;
|
int textSubMode = SUBMODE_ALPHA;
|
||||||
|
|
||||||
// User selected encoding mode
|
// User selected encoding mode
|
||||||
byte[] bytes = null; //Fill later and only if needed
|
|
||||||
if (compaction == Compaction.TEXT) {
|
if (compaction == Compaction.TEXT) {
|
||||||
encodeText(msg, p, len, sb, textSubMode);
|
encodeText(msg, p, len, sb, textSubMode);
|
||||||
|
|
||||||
} else if (compaction == Compaction.BYTE) {
|
} else if (compaction == Compaction.BYTE) {
|
||||||
bytes = msg.getBytes(encoding);
|
byte[] bytes = msg.getBytes(encoding);
|
||||||
encodeBinary(bytes, p, bytes.length, BYTE_COMPACTION, sb);
|
encodeBinary(bytes, p, bytes.length, BYTE_COMPACTION, sb);
|
||||||
|
|
||||||
} else if (compaction == Compaction.NUMERIC) {
|
} else if (compaction == Compaction.NUMERIC) {
|
||||||
|
@ -211,19 +211,17 @@ final class PDF417HighLevelEncoder {
|
||||||
textSubMode = encodeText(msg, p, t, sb, textSubMode);
|
textSubMode = encodeText(msg, p, t, sb, textSubMode);
|
||||||
p += t;
|
p += t;
|
||||||
} else {
|
} else {
|
||||||
if (bytes == null) {
|
int b = determineConsecutiveBinaryCount(msg, p, encoding);
|
||||||
bytes = msg.getBytes(encoding);
|
|
||||||
}
|
|
||||||
int b = determineConsecutiveBinaryCount(msg, bytes, p);
|
|
||||||
if (b == 0) {
|
if (b == 0) {
|
||||||
b = 1;
|
b = 1;
|
||||||
}
|
}
|
||||||
if (b == 1 && encodingMode == TEXT_COMPACTION) {
|
byte[] bytes = msg.substring(p, b).getBytes(encoding);
|
||||||
|
if (bytes.length == 1 && encodingMode == TEXT_COMPACTION) {
|
||||||
//Switch for one byte (instead of latch)
|
//Switch for one byte (instead of latch)
|
||||||
encodeBinary(bytes, p, 1, TEXT_COMPACTION, sb);
|
encodeBinary(bytes, 0, 1, TEXT_COMPACTION, sb);
|
||||||
} else {
|
} else {
|
||||||
//Mode latch performed by encodeBinary()
|
//Mode latch performed by encodeBinary()
|
||||||
encodeBinary(bytes, p, b, encodingMode, sb);
|
encodeBinary(bytes, 0, bytes.length, encodingMode, sb);
|
||||||
encodingMode = BYTE_COMPACTION;
|
encodingMode = BYTE_COMPACTION;
|
||||||
textSubMode = SUBMODE_ALPHA; //Reset after latch
|
textSubMode = SUBMODE_ALPHA; //Reset after latch
|
||||||
}
|
}
|
||||||
|
@ -530,12 +528,13 @@ final class PDF417HighLevelEncoder {
|
||||||
* Determines the number of consecutive characters that are encodable using binary compaction.
|
* Determines the number of consecutive characters that are encodable using binary compaction.
|
||||||
*
|
*
|
||||||
* @param msg the message
|
* @param msg the message
|
||||||
* @param bytes the message converted to a byte array
|
|
||||||
* @param startpos the start position within the message
|
* @param startpos the start position within the message
|
||||||
|
* @param encoding the charset used to convert the message to a byte array
|
||||||
* @return the requested character count
|
* @return the requested character count
|
||||||
*/
|
*/
|
||||||
private static int determineConsecutiveBinaryCount(CharSequence msg, byte[] bytes, int startpos)
|
private static int determineConsecutiveBinaryCount(String msg, int startpos, Charset encoding)
|
||||||
throws WriterException {
|
throws WriterException {
|
||||||
|
final CharsetEncoder encoder = encoding.newEncoder();
|
||||||
int len = msg.length();
|
int len = msg.length();
|
||||||
int idx = startpos;
|
int idx = startpos;
|
||||||
while (idx < len) {
|
while (idx < len) {
|
||||||
|
@ -556,10 +555,7 @@ final class PDF417HighLevelEncoder {
|
||||||
}
|
}
|
||||||
ch = msg.charAt(idx);
|
ch = msg.charAt(idx);
|
||||||
|
|
||||||
//Check if character is encodable
|
if (!encoder.canEncode(ch)) {
|
||||||
//Sun returns a ASCII 63 (?) for a character that cannot be mapped. Let's hope all
|
|
||||||
//other VMs do the same
|
|
||||||
if (bytes[idx] == 63 && ch != '?') {
|
|
||||||
throw new WriterException("Non-encodable character detected: " + ch + " (Unicode: " + (int) ch + ')');
|
throw new WriterException("Non-encodable character detected: " + ch + " (Unicode: " + (int) ch + ')');
|
||||||
}
|
}
|
||||||
idx++;
|
idx++;
|
||||||
|
|
Loading…
Reference in a new issue