Something, something, something C/Side

EAN 8/13 Code 39/128 Barcode Creator

12th July 2010

A couple of weeks ago I saw this post on Mibuso. This got my attention because this solution to make barcodes required no extra hassle with external components or fonts. A BMP containing the barcode is made using OutStreams, pretty neat stuff!

So I’ve started to use this piece of code to make barcodes of the type EAN8, EAN13, Code 128 & Code 39. In brief, all barcodes that I’ve already used in my (short) career.

A test with my mobile phone (Android :) ) and a handheld scanner quickly learned me that the barcodes were readable. At this point I decided to upload my code to Mibuso, and like this my first personal download on Mibuso is a fact!

Many thanks to Igor Pchelnikov for his initial post on Mibuso and the help he offered me to achieve this!

You can find the source code here: http://mibuso.com/dlinfo.asp?FileID=1192

All comments are off course more than welcome, this also counts for ratings. :)

20 Responses to “EAN 8/13 Code 39/128 Barcode Creator”

  1. Thomas Franzen Says:

    Hi lyot,

    I also had the requirement to produce barcodes on NAV. And your solution is great!!
    I have used this solution too http://mibuso.com/dlinfo.asp?FileID=1043
    but I had to produce Code128-Barcodes as well.
    I recognized a few problems when using your code: when initialising the table for Code128-encoding there is missing the entry for the value 66.
    I also got a problem when trying to read the produced barcode: there was an error in calculating the WeightSum. My barcode only exitst of numbers (e.g. 1000910736) and the WeightSum and hence the CheckDigit have been wrong.
    Perhaps you can have a look at these two things.

    Best regards
    Thomas

  2. lyot Says:

    Sure thing!

  3. lyot Says:

    Version v1.1:
    - Missing Code128 for value 66
    - Bugfix in checksum calculation for Code 128

  4. Flemming Petersen Says:

    Fix size (DPI), so it is very fast to scale up/down the picture

    In function CreateBMPHeader add code to start

    PS := ROUND(2835 / pintSize,1,’=');
    pintSize := 1;

    And change code

    From:
    poutBmpHeader.WRITE(2835, 4); //HORIZONTAL RESOLUTION
    poutBmpHeader.WRITE(2835, 4); //VERTICAL RESOLUTION

    To:

    poutBmpHeader.WRITE(PS, 4); //HORIZONTAL RESOLUTION
    poutBmpHeader.WRITE(PS, 4); //VERTICAL RESOLUTION

  5. lyot Says:

    Yep, had some troubles with scaling up the picture & performance.
    I will try your solution for a next release.
    Thanks for the tip!

  6. Carlos_pt Says:

    Hello, i would like to thank you, your code works great. While testing i believe i found an error with the encoding for precTmpCode128.CharC := ‘84′;

    //precTmpCode128.Encoding := ‘10111100100′;
    precTmpCode128.Encoding := ‘10011110100′;

    Best regards

  7. lyot Says:

    Thanx for reporting the bug!
    I’ll try to bring out an update this weekend.

  8. lyot Says:

    v1.2 :
    - Wrong Code128 for value 84
    - Performance fix for scaling barcode

  9. Lex Says:

    Hallo,

    Thank you for your source. The EAN13 code we use have 13 characters so I cut the last one to use it in your function EncodeEAN13 (I believe the last one is the checksum). That works for numbers like 4260061920112. I get a barcode that I can scan en it return the same number. When I use a number like 8710871087995 it prints a barcode witch I cant scan any more. Can you tell me what do I wrong.

    Lex

  10. lyot Says:

    Hi Lex,

    You’re doing nothing wrong, this probably an encoding bug.
    I’ll try to fix this this weekend.
    This new version will also enable to print bar codes vertical.

  11. Lex Says:

    Thank you for your reply.
    I wait for the new version for testing.

    Lex

  12. lyot Says:

    Version v1.3 :
    - Printing of vertical barcodes
    - Bugfix EAN13 EAN character set encoding table

  13. Lex Says:

    Thanks, works now….

    Lex

  14. igor Says:

    Hello,

    When I am trying to encode Item No. with space character in it, for example: “09P SCREW M12″ I get error message: “Code 128/39 CharA ” does not exist”. Items without spaces are working fine.

    Any Idea, how to solve this problem?

    Thank you,
    Igor

  15. lyot Says:

    Hi Igor,

    I’m gonna try to look at this, this weekend.

  16. lyot Says:

    Version v1.4 :
    - Bugfix for encoding ‘.’ & ‘ ‘ in Code128

  17. Got C/AL? Says:

    What is in your Dynamics NAV toolbelt? Barcode Creator by Stijn Bossuyt…

    Stijn Bossuyt has made a great function/codeunit for generating barcodes within Dynamics NAV without…

  18. lyot Says:

    Version v1.5:
    - Bugfix for encoding multiple consecutive whitespaces in Code128

  19. deV.ch Says:

    Since i had performance drops (not a lot but noticable) i did some changes to your code.

    In the Function CreateBarcodeDetail (Horizontal):

    // For Better Performance, write a String for one line!
    BinaryStringLen := bxtBarcodeBinary.LENGTH;
    IF BinaryStringLen <= MAXSTRLEN(BinaryString) THEN BEGIN
    FOR lintBarLoop := 1 TO bxtBarcodeBinary.LENGTH DO BEGIN
    bxtBarcodeBinary.GETSUBTEXT(ltxtByte,lintBarLoop,1);

    IF ltxtByte = ‘1′ THEN
    lchar := 0
    ELSE
    lchar := 255;

    FOR lintSize := 1 TO pintSize DO BEGIN
    //Putting Pixel: Black or White
    BinaryString += FORMAT(lchar);
    BinaryString += FORMAT(lchar);
    BinaryString += FORMAT(lchar);
    END
    END;

    FOR lintChainFiller := 1 TO ((lintBarLoop* pintSize) MOD 4) DO BEGIN
    //Adding 0 bytes if needed - line end
    lchar := 0;
    BinaryString += FORMAT(lchar);
    END;

    FOR lintLineLoop := 1 TO pintLines * pintSize DO BEGIN
    poutBmpHeader.WRITETEXT(BinaryString);
    END;
    END ELSE BEGIN // Slow Variant but allowes more than 1024 Bites per Line

    - I simply write everything in a string of 1024 lenth. My largest EAN128 Barcode only used 299 of it so it should cover most the barcodes i guess. Otherwise we could make the stirng an array that should get enough room for one barcode line.
    - WRITETEXT is used because in RTC i could not use WRITE and a lenth with a TEXT variable, maybe a bug, but with writetext it works.
    - performance: with original code, report had 2.5 sec to execute in classic mode, after changes made it was around 0.2 sec.
    - The performance should be definitly increased if you use a higher scale then 1

    btw: very nice work! without that solution i wouldn’t be able to save a RDLC report as PDF (with SAVEASPDF) and have working barcode (SAVEASPDF mess things up when using barcode fonts)!

  20. Manolo_SPA Says:

    Hi,
    great job about your source.
    I’m trying to print a vertical barcode and it’s still too big. I had review code and minimum resolution is one.
    How can I do barcode smaller?
    it’s code128 with 12 digits and it prints 4.1 cm barcode. I will need 3.1 or less

    Regards

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

*
To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
Click to hear an audio file of the anti-spam word