【问题标题】:Generate barcode from text and convert it to base64从文本生成条形码并将其转换为 base64
【发布时间】:2016-06-08 19:37:08
【问题描述】:

是否有人知道从字符串生成条形码图像(最好是代码 39)并将其转换为 base64 字符串的工具,使用如下:

var text = "11220"; // text to convert
var base64Str = textToBase64Barcode(text); // function to convert its input 
        // to an image formatted in a base64 string like : "data:image/jpeg;base64..."

?

【问题讨论】:

标签: javascript base64 barcode


【解决方案1】:

使用JsBarcode这个函数会做你想做的事。

function textToBase64Barcode(text){
  var canvas = document.createElement("canvas");
  JsBarcode(canvas, text, {format: "CODE39"});
  return canvas.toDataURL("image/png");
}

【讨论】:

  • 你刚刚拯救了我的一天! :) 非常感谢!
【解决方案2】:

如果你在node.js端需要这个功能,你可以试试下面

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)
            }
        })
    })
}

有关 bwip-js 的更多信息,请参阅bwip-js

【讨论】:

    【解决方案3】:

    从我的角度来看(前端),替代选项是bwipjs

    const canvas = document.createElement('canvas');
    
        const dataURL = bwipjs
          .toCanvas(canvas, {
            bcid: 'upca', // Barcode type
            text: barcode,
            scale: 3, // 3x scaling factor
            height: 20, // Bar height, in millimeters,
            border: 5,
            includetext: true, // Show human-readable text
          })
          .toDataURL('image/png');
    

    【讨论】:

      猜你喜欢
      • 2016-01-31
      • 2019-05-06
      • 1970-01-01
      • 1970-01-01
      • 2017-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多