Looking for a small 1d barcode generator

Posted on
  • Sure there are many code generators easily to find in the internet, but none of them is small enough to fit for Espruino.

    Is anyone working with tiny javascript 1D barcode generators and like to share it?

  • I don't know anything offhand, but there seem to be a bunch of different barcode styles. If you stuck with just one type I think it would be pretty easy to do from scratch. For instance EAN-13 doesn't look too painful:

    https://en.wikipedia.org/wiki/Internatio­nal_Article_Number#Binary_encoding_of_da­ta_digits_into_EAN-13_barcode

    But I guess you might want UPC?

  • Thanks, was not searching on Wiki.

    Most companies use Code 128, wiki pointed to this nice barcode-svg repo which looks promising.

  • Looks very promising! If you get it working nicely it might be nice to bring it into Espruino as a module?

  • Ok, got some time to work on this again and got some question:

    Q: How to pass a buffer and use the Graphics inside a module?

    Q: Lookup tables for char to barcode fillRect are very large, what is memory wise the best way to store them.

    Note: Got a EAN13 running and on the way for a module

        var charset = [0, 11, 13, 14, 19, 25, 28, 21, 22, 26],
            map = [13, 39, 114, 25, 51, 102, 19, 27, 108, 61, 33, 66, 35, 29, 92,
                   49, 57,  78, 47,  5,  80, 59, 17,  68, 55, 9, 72, 11, 23, 116],
    
     //used  toString(2).pad(7,"0")  to lookup  the bitcode for chars
    

    compared to the original source

    Lookup table for CODE128 is much bigger.


    1 Attachment

    • Bildschirmfoto 2021-05-02 um 10.34.59.jpg
  • Sun 2021.05.02

    Hi @MaBe several years ago, I had similar questions and several chimed in to solve with snippets and links regarding 'Flat Strings'. Here was Gordon's well described response:

    ref post #2 http://forum.espruino.com/comments/14455­141/
    entire thread Definition of a flat string and example

    Some sample snippets:

    Flat String creation failing


    'Q: How to pass a buffer and use the Graphics inside a module?'

    Is the question on how to process the image creation within the developer designed module, and access that ('pass a buffer') content inside the module, back at the console side of the WebIDE?

    Use the array buffer to access the flat string, similar when using a pointer in 'C' perhaps?
    (See last pp in Gordon's response in the first link - ref post #2)

    Nicely done, if I might add! . . .

  • Q: How to pass a buffer and use the Graphics inside a module?

    something similar to this http://forum.espruino.com/comments/15515­743/

  • Hi! I'm not sure I really understand the question?

    You can just use Graphics normally or pass the variable g in if you want to be able to draw to Graphics?

    Q: Lookup tables for char to barcode fillRect are very large, what is memory wise the best way to store them.

    Like in the code you posted? Uint8Array or String is best. Doing new Uint8Array([13, 39,....]) will use a bunch of memory as it still has to create a non-flat array, so you can always create it 'offline' and then use btoa to create a base64 encoded string and use that in your code (a bit like is done with images)

  • You can just use Graphics normally or pass the variable g in if you want to be able to draw to Graphics?

    So using g as a global var is fine, that's what I do at the moment.

    so you can always create it 'offline' and then use btoa to create a base64 encoded string and use that in your code (a bit like is done with images)

    like this?

    
    //var map = [13, 39, 114, 25, 51, 102, 19, 27, 108, 61, 33, 66, 35, 29, 92, 49, 57, 78, 47, 5, 80, 59, 17, 68, 55, 9, 72, 11, 23, 116]; 
    //mapb = btoa(E.toString(map));
    var mapb = "DSdyGTNmExtsPSFCIx1cMTlOLwVQOxFENwlICxd­0";
    console.log(atob(mapB)[0].charAt(0));
    // 13
    
    

    compare usage

    >process.memory()
    ={ free: 2458, usage: 42, total: 2500, history: 1,
      gc: 0, gctime: 4, blocksize: 16 }
    >var map = [13, 39, 114, 25, 51, 102, 19, 27, 108, 61, 33, 66, 35, 29, 92, 49, 57, 78, 47, 5, 80, 59, 17, 68, 55, 9, 72, 11, 23, 116];
    =[ 13, 39, 114, 25, 51,  ... 9, 72, 11, 23, 116 ]
    >
    >process.memory()
    ={ free: 2426, usage: 74, total: 2500, history: 17,    // 30
      gc: 0, gctime: 3, blocksize: 16 }
    >delete map
    =true
    >process.memory()
    ={ free: 2458, usage: 42, total: 2500, history: 19,
      gc: 0, gctime: 5, blocksize: 16 }
    >var mapb = "DSdyGTNmExtsPSFCIx1cMTlOLwVQOxFENwlICxd­0";
    ="DSdyGTNmExtsPSFCIx1cMTlOLwVQOxFENwlICx­d0"
    >process.memory()
    ={ free: 2453, usage: 47, total: 2500, history: 25,     // 5 !
      gc: 0, gctime: 0, blocksize: 16 }
    > 
    

    So using base64 encoded string is a real memory saver !

  • So using g as a global var is fine, that's what I do at the moment.

    I'd say passing it in as an argument is more flexible...

    require("my_lib").drawBarCode(g, myData);
    

    Then you have the option of drawing offscreen if you need.

    like this?

    I'd suggest: map = atob("DSdyGTNmExtsPSFCIx1cMTlOLwVQOxFENw­lICxd0") then map.charCodeAt(index). You could use E.toUint8Array to avoid the charCodeAt but for smaller arrays like this, I guess charCodeAt is more efficient.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Looking for a small 1d barcode generator

Posted by Avatar for MaBe @MaBe

Actions