forked from lindell/JsBarcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsBarcode.js
66 lines (48 loc) · 1.51 KB
/
JsBarcode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(function($){
$.fn.JsBarcode = function(content,options) {
//Merge the user options with the default
options = $.extend({}, $.fn.JsBarcode.defaults, options);
//Create the canvas where the barcode will be drawn on
var canvas = document.createElement('canvas');
//Abort if the browser does not support HTML5canvas
if (!canvas.getContext) {
return this;
}
var encoder = new window[options.format](content);
//Abort if the barcode format does not support the content
if(!encoder.valid()){
return this;
}
//Encode the content
var binary = encoder.encoded();
//Get the canvas context
var ctx = canvas.getContext("2d");
//Set the width and height of the barcode
canvas.width = binary.length*options.width+2*options.quite;
canvas.height = options.height;
//Paint the canvas white
ctx.fillStyle = "#fff";
ctx.fillRect(0,0,canvas.width,canvas.height);
//Creates the barcode out of the encoded binary
for(var i=0;i<binary.length;i++){
var x = i*options.width+options.quite;
if(binary[i] == "1"){
ctx.fillStyle = "#000";
}
else{
ctx.fillStyle = "#fff";
}
ctx.fillRect(x,0,options.width,options.height);
}
//Grab the dataUri from the canvas
uri = canvas.toDataURL('image/png');
//Put the data uri into the image
return $(this).attr("src",uri);
};
$.fn.JsBarcode.defaults = {
width: 2,
height: 100,
quite: 10,
format: "CODE128"
};
})(jQuery);