Nicola
Nicola

Reputation: 297

How to convert a string to a barcode code 128

I need to convert a string to a barcode Code 128 I found a procedure which converts the original string into the formatted string, including the special characters of the Code 128 From Here. I didn't directly use the code on this page, but I translated it into the language used by genexus, maybe there was some error in this conversion?

By doing a test, the string "1401" is converted into "ÒQPLÓ" but when I go to print the string setting the code128 font, it is badly formatted, in particular the initial and final characters remain untranslated. I tested this both in word and in a pdf document generated with the genexus program and I get this barcode

enter image description here

(word add the three line after the second O too, I don't know why) I believe that the two accented O's are the characters set by the procedure with the ASCII codes (210 for example is the first O), perhaps there was a misunderstanding here? What am I doing wrong? Is the procedure incorrect? Is it a character set problem? Or what else? Did I misunderstand those lines and don't they represent ASCII codes?

I tried with a barcode reader and there is no way to read this. If I generate the barcode with an online generator everything works fine

Upvotes: 1

Views: 2417

Answers (1)

K J
K J

Reputation: 11739

Barcode 128 is an oddity for programming as it is non-conventional encoding in its application. Let's start with the output.

enter image description here

You can check that is correct here https://zxing.org/w/decode?u=https%3A%2F%2Fi.stack.imgur.com%2F8tQm7.png

enter image description here

You can also see to get that font as output we need in PDF binary terms an input of 0110100100001110000000010001001001101010 which does not match the pixel output. However 01101001 00001110 00000001 00010010 01101010 as hex is 69 0e 01 12 6a which as ASCII / ANSI = Ò.!2Ó.

So the rules of generation are

  • Use the start numeric character Ò
  • add a ligature glyph for 14 = .
  • add a ligature glyph for 01 = !
  • add the calculated check character 2
  • round of with a stop character Ó

write those 5 characters using any of several Code 128 fonts.

It is a lot easier if you use a cut and paste tool that calculates ligatures and Mod 103 check bytes.

enter image description here

Here we see it as text in a PDF where the text is now 6C 11 04 15 6D since it gets remapped characters (by the MS PDF generator) which in this case seems to have added +3 to each glyph!

enter image description here

13 0 obj
<</Length 158>>
stream
q
q
.75 0 0 -.75 0 841.92007 cm
0 0 0 rg
BT
/F1 96.0037 Tf
1 0 0 -1 94.56 150.4 Tm
[<006C>-.695313<0011>-.554688<0004>-.554688<0015>-.554688<006D>] TJ
ET
Q
Q

Calculations in Excel (see https://en.wikipedia.org/wiki/Code_128#Bar_code_widths)

enter image description here

What is odd is that the concept of using Code128 is compaction yet that method of fonts requires many times the resources compared to pixels

Here even with overheads the 54 bars (I added extra 4 white ÿ bars each end, for guarding) is a total file size of just over 750 bytes, but as a font embedded file it is heading for 4 times bigger e.g. 3,199 bytes for the bars without the normal embedded 1401 text.

%PDF-1.3
1 0 obj <</Pages 2 0 R/Type/Catalog>> endobj
2 0 obj <</Count 1/Kids[3 0 R]/Type/Pages>> endobj
3 0 obj <</Contents 5 0 R/CropBox[0 0 595.32 841.92]/MediaBox[0 0 595.32 841.92]/Parent 2 0 R/Resources<</XObject<</Image1 4 0 R>>>>/Rotate 0/Type/Page>> endobj
4 0 obj <</BitsPerComponent 8/ColorSpace/DeviceGray/Width 54/Height 1/Length 54/Subtype/Image/Type/XObject>>
stream
ÿÿÿÿ  ÿ ÿÿ   ÿÿ ÿÿ  ÿÿ   ÿ  ÿÿ  ÿ  ÿÿ  ÿÿÿ   ÿ ÿ  ÿÿÿÿ
endstream
endobj
5 0 obj <</Length 81>>
stream
q 0 g 108 0 0 60 160 700 cm /Image1 Do Q
q BT /F1 18 Tf 195 675 Td (1401) Tj ET Q
endstream
endobj

xref
0 6
0000000000 65536 f 
0000000009 00000 n 
0000000054 00000 n 
0000000105 00000 n 
0000000266 00000 n 
0000000454 00000 n 

trailer
<</Size 6/Root 1 0 R>>
startxref
584
%%EOF

Note cut and paste above will usually distort the bars due to bitrot(ation).

enter image description here

So it will be cleaner white than this (ensure you save as ANSI NOT UTF-8).

So here is how the binary pattern looks as binary on off values, top is the odds and even pairing then shown using * as whitespace for convenience where the black1 will be nul and the white * will become ÿ

enter image description here

Upvotes: 2

Related Questions