问题描述
我需要生成一些文本PDF417条码code。我有一个API(我没有创建)生成给出的数据的PDF417条码code,行数和列数(在其他参数无关的问题)的数量。
I need to generate a PDF417 barcode from some text. I have an API (that I didn't create) that generates a PDF417 barcode given the data, number of rows and number of columns (among other parameters irrelevant to the question).
我的PDF417条码code使用文本编码。这意味着1 codeword最多可以容纳2个字符。现在,列的数量是固定的,因为我打印这个吧code在一个非常有限的空间。
My PDF417 barcode uses text encoding. This means 1 codeword can hold up to 2 characters. Now, the number of columns HAS to be fixed because I'm printing this barcode in a very constrained space.
下面是我从本文(参见第38页有推断 - 浆纱酒吧code):
The following is what I have inferred from this document (Refer page 38 - Sizing a barcode):
- 让一些每行codewords, CWPerRow = 7。
- 所需要的某些给定的文本codewords号, ReqCW =的strlen(文本)/ 2。
- 所需的行数= ReqCW / CWPerRow
- Let number of codewords per row, CWPerRow = 7.
- Number of codewords required for some given text, ReqCW = strlen(text) / 2.
- Number of rows required = ReqCW / CWPerRow
当我测试上面的算法,不显示任何内容。当我用同样的API时的数据非常小,行数= 25,酒吧code打印就好了(由不同吧code扫描仪验证)。
When I test the above algorithm, nothing displays. When I use the same API when the data is very small and the number of rows = 25, the barcode prints just fine (verified by various barcode scanners).
那么,如何计算需要时列数是已知的某些给定的文本行数?
So, how do I calculate the number of rows required for some given text when the number of columns is known?
推荐答案
您可以看看一些PDF417执行源 - code,如<一个href="https://github.com/zxing/zxing/blob/master/core/src/main/java/com/google/zxing/pdf417/en$c$cr//PDF417.java#L640"相对=nofollow> ZXing 。
You could look at the source-code of some PDF417 implementation, such as ZXing.
文本编码是不是每个code字只是两个字符。如果您使用的不是大写字母和空间的任何其它字符,EN codeR将增加额外的字符切换字符集等你真的要<一个href="https://github.com/zxing/zxing/blob/master/core/src/main/java/com/google/zxing/pdf417/en$c$cr/PDF417HighLevelEn$c$cr.java#L239"相对=nofollow>恩code文本,看看有多少code-的话就会变成。
The text encoding isn't just two characters per code-word. If you use any other character than uppercase letters and space, the encoder will add extra characters to switch character-sets etc. You really have to encode the text to see how many code-words it will become.
public class Test
{
public static void main(String[] args)
{
String msg = "Hello, world!";
int columns = 7;
int sourceCodeWords = calculateSourceCodeWords(msg);
int errorCorrectionCodeWords = getErrorCorrectionCodewordCount(0);
int rows = calculateNumberOfRows(sourceCodeWords, errorCorrectionCodeWords, columns);
System.out.printf("\"%s\" requires %d code-words, and %d error correction code-words. This becomes %d rows.%n",
msg, sourceCodeWords, errorCorrectionCodeWords, rows);
}
public static int calculateNumberOfRows(int sourceCodeWords, int errorCorrectionCodeWords, int columns) {
int rows = ((sourceCodeWords + 1 + errorCorrectionCodeWords) / columns) + 1;
if (columns * rows >= (sourceCodeWords + 1 + errorCorrectionCodeWords + columns)) {
rows--;
}
return rows;
}
public static int getErrorCorrectionCodewordCount(int errorCorrectionLevel) {
if (errorCorrectionLevel < 0 || errorCorrectionLevel > 8) {
throw new IllegalArgumentException("Error correction level must be between 0 and 8!");
}
return 1 << (errorCorrectionLevel + 1);
}
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 "\t\r #$%&*+,-./0123456789:=^".indexOf(ch) > -1;
}
private static boolean isPunctuation(char ch) {
return "\t\n\r!\"$'()*,-./:;<>?@[\\]_`{|}~".indexOf(ch) > -1;
}
private static final int SUBMODE_ALPHA = 0;
private static final int SUBMODE_LOWER = 1;
private static final int SUBMODE_MIXED = 2;
private static final int SUBMODE_PUNCTUATION = 3;
public static int calculateSourceCodeWords(String msg)
{
int len = 0;
int submode = SUBMODE_ALPHA;
int msgLength = msg.length();
for (int idx = 0; idx < msgLength;)
{
char ch = msg.charAt(idx);
switch (submode)
{
case SUBMODE_ALPHA:
if (isAlphaUpper(ch))
{
len++;
}
else
{
if (isAlphaLower(ch))
{
submode = SUBMODE_LOWER;
len++;
continue;
}
else if (isMixed(ch))
{
submode = SUBMODE_MIXED;
len++;
continue;
}
else
{
len += 2;
break;
}
}
break;
case SUBMODE_LOWER:
if (isAlphaLower(ch))
{
len++;
}
else
{
if (isAlphaUpper(ch))
{
len += 2;
break;
}
else if (isMixed(ch))
{
submode = SUBMODE_MIXED;
len++;
continue;
}
else
{
len += 2;
break;
}
}
break;
case SUBMODE_MIXED:
if (isMixed(ch))
{
len++;
}
else
{
if (isAlphaUpper(ch))
{
submode = SUBMODE_ALPHA;
len++;
continue;
}
else if (isAlphaLower(ch))
{
submode = SUBMODE_LOWER;
len++;
continue;
}
else
{
if (idx + 1 < msgLength)
{
char next = msg.charAt(idx + 1);
if (isPunctuation(next))
{
submode = SUBMODE_PUNCTUATION;
len++;
continue;
}
}
len += 2;
}
}
break;
default:
if (isPunctuation(ch))
{
len++;
}
else
{
submode = SUBMODE_ALPHA;
len++;
continue;
}
break;
}
idx++; // Don't increment if 'continue' was used.
}
return (len + 1) / 2;
}
}
输出:
你好,世界!需要9 code字,和2纠错code字。这将成为2行。
这篇关于在PDF417条码code其中列数是固定的,我怎么会计算所需的一些文本行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!