Example of the one-dimensional code (barcode) function generated by JS, js example

Source: Internet
Author: User

Example of the one-dimensional code (barcode) function generated by JS, js example

This article describes how to generate a one-dimensional code (barcode) function in JavaScript. We will share this with you for your reference. The details are as follows:

1. js code:

(Function () {if (! Exports) var exports = window; var BARS = [empty, 222221,121223, 121322,131222, 122213,122312, empty, 223211,221132, empty, 322112,322211, large, 232121,111323, 135693,1321, 112313,132113, large, 231113,231311, 111083,112331, 135691,113123, 113321,133121, large, 231131-12 13113, 2017311, 2017131,311123, expires, 142.162,141221, expires, 241211,221114, 413111,241112, expires, 412121,111143, 111341,131141, 114113,114311, middle, 113141,114131, 311141,411131, 211412,21_14, 211232,23311120], STAR T_BASE = 38, STOP = 106; function code128 (code, barcodeType) {if (arguments. length <2) barcodeType = code128Detect (code); if (barcodeType = 'C' & code. length % 2 = 1) code = '0' + code; var a = parseBarcode (code, barcodeType); return bar2html (. join ('') + '<label>' + code + '</label>';} function bar2html (s) {for (var pos = 0, sb = []; pos <s. length; pos + = 2) {sb. push ('<div class = "bar' + s. charAt (pos) + 'Space' + s. charAt (pos + 1) + '"> </div>');} return sb. join ('');} function code128Detect (code) {if (/^ [0-9] + $ /. test (code) return 'C'; if (/[a-z]/. test (code) return 'B'; return 'a';} function parseBarcode (barcode, barcodeType) {var bars = []; bars. add = function (nr) {var nrCode = BARS [nr]; this. check = this. length = 0? Nr: this. check + nr * this. length; this. push (nrCode | ("UNDEFINED:" + nr + "->" + nrCode) ;}; bars. add (START_BASE + barcodeType. charCodeAt (0); for (var I = 0; I <barcode. length; I ++) {var code = barcodeType = 'C '? + Barcode. substr (I ++, 2): barcode. charCodeAt (I); converted = fromType [barcodeType] (code); if (isNaN (converted) | converted <0 | converted> 106) throw new Error ("Unrecognized character (" + code + ") at position" + I + "in code" + barcode + "'. "); bars. add (converted);} bars. push (BARS [bars. check % 103], BARS [STOP]); return bars;} var fromType = {A: function (charCode) {if (charCode> = 0 & charCode <32) return charCode + 64; if (charCode >=32 & charCode <96) return charCode-32; return charCode ;}, B: function (charCode) {if (charCode >=32 & charCode <128) return charCode-32; return charCode ;}, C: function (charCode) {return charCode ;}}; // -- | Export exports. code128 = code128;}) ();/* showDiv: represents the divID to be displayed, textVlaue: represents the value to be generated, barcodeType: represents the generation type (A, B, C) three types */function createBarcode (showDiv, textValue, barcodeType) {var divElement = document. getElementById (showDiv); divElement. innerHTML = code128 (textValue, barcodeType );}

2.css code:

.barcode { float:left; clear:both; padding: 0 10px; /*quiet zone*/ overflow:auto; height:0.5in; /*size*/}.right { float:right; }.barcode + * { clear:both; }.barcode div { float:left; height: 0.35in; /*size*/}.barcode .bar1 { border-left:1px solid black; }.barcode .bar2 { border-left:2px solid black; }.barcode .bar3 { border-left:3px solid black; }.barcode .bar4 { border-left:4px solid black; }.barcode .space0 { margin-right:0 }.barcode .space1 { margin-right:1px }.barcode .space2 { margin-right:2px }.barcode .space3 { margin-right:3px }.barcode .space4 { margin-right:4px }.barcode label { clear:both; display:block; text-align:center; font: 0.125in/100% helvetica; /*size*/}/*** bigger ******************************************/.barcode2 { float:left; clear:both; padding: 0 10px; /*quiet zone*/ overflow:auto; height:1in; /*size*/}.barcode2 + * { clear:both; }.barcode2 div { float:left; height: 0.7in; /*size*/}.barcode2 .bar1 { border-left:2px solid black; }.barcode2 .bar2 { border-left:4px solid black; }.barcode2 .bar3 { border-left:6px solid black; }.barcode2 .bar4 { border-left:8px solid black; }.barcode2 .space0 { margin-right:0 }.barcode2 .space1 { margin-right:2px }.barcode2 .space2 { margin-right:4px }.barcode2 .space3 { margin-right:6px }.barcode2 .space4 { margin-right:8px }.barcode2 label { clear:both; display:block; text-align:center; font: 0.250in/100% helvetica; /*size*/}

3.html code:

<Html> 

Run the following command:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.