本文转载自http://www.uedsc.com/barcode-js.htmljavascript
Barcode.js是一个基于jQuery库的插件,用于绘制条形码或者二维码,可以生成基于DIV+CSS或者Canvas等的条码,该插件支持PHP,jQuery和JavaScript,解压后对应3个目录,每一个目录下都有对应的例子能够查看。css
注意:须要绘制的条形码/二维码长度和字符串包含字母之类的,注意要选择不一样的条形码/二维码类型,要不没法绘制(没研究过条形码,经测试视乎是这样的)。建议直接选择code128。html
一、同其余jQuery插件同样,只须要将jQuery框架和jquery.barcode.js
文件引入页面。java
<script type="text/javascript" src="jquery.min.js"></script> <script type="text/javascript" src="jquery.barcode.js"></script>
二、定义一个DOM对象做为生成条形码/二维码的容器jquery
<div id="bcTarget"></div>
三、使用jQuery选择器调用barcode()
方法绘制条形码/二维码git
$("#bcTarget").barcode("1234567890128", "ean13");
根据输入字符的长度来生成对应的条形码github
function genCode(){ var encode = "ean8"; if(8 === $("#exam_no").text().length) encode = "ean8"; else if(11 === $("#exam_no").text().length) encode = "code11"; else if(13 === $("#exam_no").text().length) encode = "ean13"; $("#code_b").barcode($("#exam_no").text(), encode, {barWidth: 2, barHeight: 50}); $("#code_s").barcode($("#exam_no").text(), encode, {barWidth: 1, barHeight: 50}); $("#code_b div").css('background-color', '#eee'); }
jquery对象扩展方法barcode参数说明:barcode: function(datas, type, settings)
canvas
datas参数支持2种类型框架
type : ean8, ean13, code11, code39, code128, codabar | |
---|---|
member | Type |
code | string |
type : std25, int25, code93 | |
---|---|
member | Type |
code | string |
crc | boolean |
type : msi | ||
---|---|---|
member | Type | |
code | string | |
crc | boolean | |
object | crc1 : string(“mod10”, “mod11”) | |
crc2 : string(“mod10”, “mod11”) |
type : datamatrix | |
---|---|
member | Type |
code | string |
rect | boolean (default : false) |
type (string):条形码类型svg
注意要根据字符串长度来选择条形码的编码方式,生成的条形码默认是DIV+CSS形式的,后面的barWidth
和barHeight
是生成参数,默认是70X70的正方形,后面的参数能够调整条形码的比例,但不能调整大小
settings (object):条形码样式的配置
配置名称 | 类型 | 默认值 | 描述 | 限制 |
---|---|---|---|---|
barWidth | int | 1 | 条形码宽度 | 1D |
barHeight | int | 50 | 容器高度 | 1D |
moduleSize | int | 5 | largeur / hauteur d’un module | 2D |
showHRI | bool | true | 是否显示条形码内容(方便识别) | |
bgColor | text | #FFFFFF | 背景色 | |
color | text | #000000 | 条形码颜色 | |
fontSize | int | 10 | 显示的条形码内容字体大小 | |
output | text | css | 如何绘制条形码: css, svg, bmp, canvas,注意svg,bmp,canvas不支持IE,最好不用 |
renderer : canvas | |||
---|---|---|---|
Parameter | Type | Default value | Detail |
posX | int | 0 | X origine |
posY | int | 0 | Y origine |