Generate barcode from text and convert it to base64

lennin92 picture lennin92 · Feb 25, 2016 · Viewed 15.2k times · Source

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 = "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..."

?

Answer

Lindell picture Lindell · Feb 29, 2016

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