📄 pdf417highlevelencoder.java
字号:
}
}
if ((len % 2) != 0) {
sb.append((char)((h * 30) + 29)); //ps
}
}
/**
* Encode parts of the message using Byte Compaction as described in ISO/IEC 15438:2001(E),
* chapter 4.4.3. The Unicode characters will be converted to to binary using the cp437
* codepage.
* @param msg the message
* @param bytes the message converted to a byte array
* @param startpos the start position within the message
* @param count the number of bytes to encode
* @param sb receives the encoded codewords
*/
public static void encodeBinary(String msg, byte[] bytes, int startpos, int count, int startmode, StringBuffer sb) {
if (count == 1 && startmode == TEXT_COMPACTION) {
sb.append((char)SHIFT_TO_BYTE);
} else {
boolean sixpack = ((count % 6) == 0);
if (sixpack) {
sb.append((char)LATCH_TO_BYTE);
} else {
sb.append((char)LATCH_TO_BYTE_PADDED);
}
}
char[] chars = new char[5];
int idx = startpos;
while ((startpos + count - idx) >= 6) {
long t = 0;
for (int i = 0; i < 6; i++) {
t <<= 8;
t += bytes[idx + i] & 0xff;
}
for (int i = 0; i < 5; i++) {
chars[i] = (char)(t % 900);
t /= 900;
}
for (int i = chars.length - 1; i >= 0; i--) {
sb.append(chars[i]);
}
idx += 6;
}
//Encode rest (remaining n<5 bytes if any)
for (int i = idx; i < startpos + count; i++) {
int ch = bytes[i] & 0xff;
sb.append((char)ch);
}
}
public static void encodeNumeric(String msg, int startpos, int count, StringBuffer sb) {
int idx = 0;
StringBuffer tmp = new StringBuffer(count / 3 + 1);
final BigInteger num900 = BigInteger.valueOf(900);
final BigInteger num0 = BigInteger.valueOf(0);
while (idx < count - 1) {
tmp.setLength(0);
int len = Math.min(44, count - idx);
String part = "1" + msg.substring(startpos + idx, startpos + idx + len);
BigInteger bigint = new BigInteger(part);
do {
BigInteger c = bigint.mod(num900);
tmp.append((char)(c.intValue()));
bigint = bigint.divide(num900);
} while (!bigint.equals(num0));
//Reverse temporary string
for (int i = tmp.length() - 1; i >= 0; i--) {
sb.append(tmp.charAt(i));
}
idx += len;
}
}
private static boolean isDigit(char ch) {
return ch >= '0' && ch <= '9';
}
private static boolean isAlphaUpper(char ch) {
return (ch == ' ' || (ch >= 'A' && ch <= 'Z'));
}
private static boolean isAlphaLower(char ch) {
return (ch == ' ' || (ch >= 'a' && ch <= 'z'));
}
private static boolean isMixed(char ch) {
return (MIXED[ch] != -1);
}
private static boolean isPunctuation(char ch) {
return (PUNCTUATION[ch] != -1);
}
private static boolean isText(char ch) {
return (ch == 9 //TAB
|| ch == 10 //LF
|| ch == 13 //CR
|| (ch >= 32 && ch <= 126));
}
/*
private boolean isByte(int pos) {
char ch = msg.charAt(pos);
//Sun returns a ASCII 31 (?) for a character that cannot be mapped. Let's hope all
//other VMs do the same
return (byteMap[pos] != 31 || ch == '?');
}
private boolean isEncodableCharacter(int pos) {
char ch = msg.charAt(pos);
return isText(ch) || isByte(pos);
}*/
/**
* Determines the number of consecutive characters that are encodable using numeric compaction.
* @param msg the message
* @param startpos the start position within the message
* @return the requested character count
*/
public static int determineConsecutiveDigitCount(String msg, int startpos) {
int count = 0;
int len = msg.length();
int idx = startpos;
if (idx < len) {
char ch = msg.charAt(idx);
while (isDigit(ch) && idx < len) {
count++;
idx++;
if (idx < len) {
ch = msg.charAt(idx);
}
}
}
return count;
}
/**
* Determines the number of consecutive characters that are encodable using text compaction.
* @param msg the message
* @param startpos the start position within the message
* @return the requested character count
*/
public static int determineConsecutiveTextCount(String msg, int startpos) {
int len = msg.length();
int idx = startpos;
while (idx < len) {
char ch = msg.charAt(idx);
int numericCount = 0;
while (numericCount < 13 && isDigit(ch) && idx < len) {
numericCount++;
idx++;
if (idx < len) {
ch = msg.charAt(idx);
}
}
if (numericCount >= 13) {
return idx - startpos - numericCount;
}
if (numericCount > 0) {
//Heuristic: All text-encodable chars or digits are binary encodable
continue;
}
ch = msg.charAt(idx);
//Check if character is encodable
if (!isText(ch)) {
break;
}
idx++;
}
return idx - startpos;
}
/**
* Determines the number of consecutive characters that are encodable using binary compaction.
* @param msg the message
* @param bytes the message converted to a byte array
* @param startpos the start position within the message
* @return the requested character count
*/
public static int determineConsecutiveBinaryCount(String msg, byte[] bytes, int startpos) {
int len = msg.length();
int idx = startpos;
while (idx < len) {
char ch = msg.charAt(idx);
int numericCount = 0;
int textCount = 0;
while (numericCount < 13 && isDigit(ch)) {
numericCount++;
//textCount++;
int i = idx + numericCount;
if (i < len) {
ch = msg.charAt(i);
} else {
break;
}
}
if (numericCount >= 13) {
return idx - startpos;
}
while (textCount < 5 && isText(ch)) {
textCount++;
int i = idx + textCount;
if (i < len) {
ch = msg.charAt(i);
} else {
break;
}
}
if (textCount >= 5) {
return idx - startpos;
}
ch = msg.charAt(idx);
//Check if character is encodable
//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 IllegalArgumentException("Non-encodable character detected: "
+ ch + " (Unicode: " + (int)ch + ")");
}
idx++;
}
return idx - startpos;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -