Generate barcode from text and convert it to base64

前端 未结 2 1918
说谎
说谎 2021-02-19 11:55

Does someone knows a tool to generate barcode image (preferably code 39) from a string and converts it to base64 string, something to use like this:

var text =         


        
相关标签:
2条回答
  • 2021-02-19 12:37

    if you need this function in node.js side, you can try below

    const bwipjs = require('bwip-js');
    
    function textToBarCodeBase64 (text) {
        return new Promise((resolve, reject) => {
            bwipjs.toBuffer({
                bcid: 'code128',
                text: text,
                scale: 3,
                height: 10,
                includetext: true,
                textxalign: 'center'
            }, function(error, buffer) {
                if(error) {
                    reject(error)
                } else {
                    let gifBase64 = `data:image/gif;base64,${buffer.toString('base64')}`
                    resolve(gifBase64)
                }
            })
        })
    }
    

    about bwip-js see bwip-js for more details

    0 讨论(0)
  • 2021-02-19 12:43

    Using JsBarcode this function will do what you want.

    function textToBase64Barcode(text){
      var canvas = document.createElement("canvas");
      JsBarcode(canvas, text, {format: "CODE39"});
      return canvas.toDataURL("image/png");
    }
    
    0 讨论(0)
提交回复
热议问题