set barcode 128 to type B in java

只谈情不闲聊 提交于 2019-12-14 04:22:33

问题


I am using this code to generate barcode128 :

String stb1 = "123456789";
code128.setCode(stb1);
//code128.setCodeType(Barcode128.CODE128_UCC);
//code128.setCode("1234567890");
code128.setStartStopText(true);
Image image128 = code128.createImageWithBarcode(cbd, null, Color.white);
image128.setAbsolutePosition(20 + x, 626 + y);
image128.scaleAbsolute(109, 33);
document.add(image128);

My client want the barcode 128 type B. How to set it to type B? I tried CODE128_UCC, CODE128_RAW, CODE128 in setCodyType() but three of them looks similar.


回答1:


thanks to the previous answer by pmoleri, I succeded in using Itext (Itextsharp version 5.3.3.0) for generating a barcode128 Type B. This is the c# code I wrote, hope it might help:

String stb1 = "123456789";
Barcode128 code128 = new Barcode128();
code128.CodeType = Barcode.CODE128_RAW;
code128.ChecksumText = true;
code128.GenerateChecksum = true;
code128.StartStopText = true;
code128.Code = (StringEncode128(stb1) + "\uffff");
System.Drawing.Bitmap bm = new System.Drawing.Bitmap(code128.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White));
bm.Save(Server.MapPath("barcode128.gif"), System.Drawing.Imaging.ImageFormat.Gif);
ImageBarCode128 = Image.GetInstance(Server.MapPath("barcode.gif"));

String Encoding function:

public String StringEncode128(String text) {
const int Ascii_FNC1 = 102;
const int Ascii_STARTB = 104;

int CharAscii = 0;
for (int CharPos = 0; CharPos < text.Length; ++CharPos)
{
  string letter = text.Substring(CharPos, 1);
  CharAscii = (int)Convert.ToChar(letter);
  if (CharAscii > 127 && CharAscii != Ascii_FNC1) {
    throw new Exception(text + "|" + CharAscii);
    }
}

StringBuilder outstring = new StringBuilder();
outstring.Append((char)(Ascii_STARTB));

for (int CharPos = 0; CharPos < text.Length; ++CharPos)
{
    string letter = text.Substring(CharPos, 1);
    CharAscii = (int)Convert.ToChar(letter) - 32;
    outstring.Append((char)(CharAscii));
}

return outstring.ToString();
}



回答2:


Itext probably makes the most compact version of the barcode, that's why you can't choose the charset. For example, it will choose charset C if it starts with a large sequence of digits and then change to charset B if a sequence of alpha chars follows.

Any barcode scanner able to read Code 128 must be able to read any of the charsets, so I don't see the need of sticking to charset B.

Update

I don't think you can do it with iText, unless you pass the raw code. Luckily charset B is pretty straightforward, just put toghether a string with:

  • start character for charset B: 104
  • substract 32 from the ascii code of every character in the original string
  • finish the string with "\uffff"

Then create the barcode in raw mode using: setCodeType(Barcode.CODE128_RAW)

You can get addition information here: http://grandzebu.net/informatique/codbar-en/code128.htm



来源:https://stackoverflow.com/questions/12194572/set-barcode-128-to-type-b-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!