code128 编码字符串错误
Posted
技术标签:
【中文标题】code128 编码字符串错误【英文标题】:code128 encoded string error 【发布时间】:2017-05-23 13:30:18 【问题描述】:我已经从this website 在 C:/windows/fonts 中安装了 code128 字体 我已经使用java方法生成条形码字符串但是当我将结果字符串放入MS word中时我有this
知道我已经安装了字体并使用来自同一网站的代码,问题出在哪里
【问题讨论】:
【参考方案1】:我已经将这个 page 的 c# 代码转换为 java 并且它可以工作
这里是java代码:
public class NewCoder128
public NewCoder128()
public String StringToBarcode(String value)
// Parameters : a string
// Return : a string which give the bar code when it is dispayed with CODE128.TTF font
// : an empty string if the supplied parameter is no good
int charPos, minCharPos;
int currentChar, checksum;
Boolean isTableB = true, isValid = true;
String returnValue = "";
if (value.length() > 0)
// Check for valid characters
for (int charCount = 0; charCount < value.length(); charCount++)
//currentChar = char.GetNumericValue(value, charPos);
// (int)char.Parse(value.Substring(charCount, 1));
System.out.println("count "+charCount);
currentChar = value.charAt(charCount);
if (!(currentChar >= 32 && currentChar <= 126))
isValid = false;
break;
// Barcode is full of ascii characters, we can now process it
if (isValid)
charPos = 0;
while (charPos < value.length())
if (isTableB)
// See if interesting to switch to table C
// yes for 4 digits at start or end, else if 6 digits
if (charPos == 0 || charPos + 4 == value.length())
minCharPos = 4;
else
minCharPos = 6;
BarcodeConverter128 brc128 = new BarcodeConverter128();
minCharPos = brc128.IsNumber(value, charPos, minCharPos);
if (minCharPos < 0)
// Choice table C
if (charPos == 0)
// Starting with table C
returnValue = String.valueOf((char) 205); // char.ConvertFromUtf32(205);
else
// Switch to table C
returnValue = returnValue + String.valueOf((char) 199);
isTableB = false;
else
if (charPos == 0)
// Starting with table B
returnValue = String.valueOf((char) 204); // char.ConvertFromUtf32(204);
if (!isTableB)
// We are on table C, try to process 2 digits
minCharPos = 2;
BarcodeConverter128 brcd = new BarcodeConverter128();
minCharPos = brcd.IsNumber(value, charPos, minCharPos);
if (minCharPos < 0) // OK for 2 digits, process it
currentChar = Integer.parseInt(value.substring(charPos, charPos+2));
currentChar = currentChar < 95 ? currentChar + 32 : currentChar + 100;
returnValue = returnValue + String.valueOf((char) currentChar);
charPos += 2;
else
// We haven't 2 digits, switch to table B
returnValue = returnValue + String.valueOf((char) 200);
isTableB = true;
if (isTableB)
// Process 1 digit with table B
returnValue = returnValue + value.substring(charPos, charPos+1);
charPos++;
// Calculation of the checksum
checksum = 0;
for (int loop = 0; loop < returnValue.length(); loop++)
currentChar = returnValue.charAt(loop);
currentChar = currentChar < 127 ? currentChar - 32 : currentChar - 100;
if (loop == 0)
checksum = currentChar;
else
checksum = (checksum + (loop * currentChar)) % 103;
// Calculation of the checksum ASCII code
checksum = checksum < 95 ? checksum + 32 : checksum + 100;
// Add the checksum and the STOP
returnValue = returnValue +
String.valueOf((char) checksum) +
String.valueOf((char) 206);
return returnValue;
class BarcodeConverter128
/// <summary>
/// Converts an input string to the equivilant string, that need to be produced using the 'Code 128' font.
/// </summary>
/// <param name="value">String to be encoded</param>
/// <returns>Encoded string start/stop and checksum characters included</returns>
public BarcodeConverter128()
public int IsNumber(String InputValue, int CharPos, int MinCharPos)
// if the MinCharPos characters from CharPos are numeric, then MinCharPos = -1
MinCharPos--;
if (CharPos + MinCharPos < InputValue.length())
while (MinCharPos >= 0)
if ((int)(InputValue.charAt(CharPos + MinCharPos)) < 48
||(int)(InputValue.charAt(CharPos + MinCharPos)) > 57)
break;
MinCharPos--;
return MinCharPos;
使用这个类你只需要下载code128 font,安装到你的系统上粘贴到windows/fonts 并如下调用此代码,生成的条形码将是可读的
NewCoder128 newCoder128 = new NewCoder128();
String encoded_string = newCoder128.StringToBarcode("123456789")
// encoded string is "Í,BXnÈ9oÎ"
【讨论】:
复制您的输出Í,BXnÈ9oÎ
并粘贴到 excel 中并将其更改为代码 128,然后当我尝试扫描它时,它就不会扫描。它对你有用吗?以上是关于code128 编码字符串错误的主要内容,如果未能解决你的问题,请参考以下文章
条形码 Code128Auto 是如何自动在三个子集A、B、C中切换的,或者 Code128Auto 的编码规则,请举例说明