在固定列数的PDF417条码中,如何计算某些文本所需的行数?

在固定列数的PDF417条码中,如何计算某些文本所需的行数?,第1张

在固定列数的PDF417条码中,如何计算某些文本所需的行数?

您可以查看一些PDF417实现的源代码,例如ZXing。

文本编码不仅是每个代码字两个字符。如果您使用大写字母和空格以外的任何其他字符,则编码器将添加额外的字符以切换字符集等。您实际上必须对文本进行编码,以查看它将变成多少个代码字。

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 pre-words, and %d error correction pre-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 "tr #$%&*+,-./0123456789:=^".indexOf(ch) > -1;    }    private static boolean isPunctuation(char ch) {        return "tnr!"$'()*,-./:;<>?@[\]_`{|}~".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;    }}

输出:

"Hello, world!" requires 9 pre-words, and 2 error correction pre-words.This becomes 2 rows.



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5643230.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存