`

Creating a Code 39 Barcode using HTML, CSS and Javascript

 
阅读更多

Introduction

 

This article explores the use of Javascript, HyperText Markup Language (HTML) and Cascading Style Sheets (CSS) for the creation of a Code 39 barcode. This solution described does not employ the use of graphical image and overcomes the problems associated with the limited support of the Data URI feature in some browsers. Server-side scripts are also not required as the steps for creating the barcode e.g. data validation, check character generation and generation of the HTML barcode are performed in Javascript.

 

Background

 

One of the objectives of this article is to have a solution to generate a barcode using pure browser (client-side) technology. The first thing that comes into mind is to make use of graphical images, especially considering that a barcode comprises of a sequence of black and white bars. Many modern browsers also support the data Uniform Resource Locators (URI) capability. This capability allows a web page to include in-line data such as images as if they are external resources.

 

However, one of the most commonly-used browsers, the Internet Explorer (as of current v8.0) does not fully support data URI. In IE8, data URIs must be smaller than 32KB. This poses a problem when we require a barcode image bigger than 32KB.

 

Another solution is to generate barcode images on the server side. However there are many server technologies in use today including but not limited to Java, .Net, PHP, Perl, Python and Ruby. We hope to have a solution that works on all server platforms. This means that our solution has to be implemented on the browser (client) side without using graphics images.

 

The Solution

 

The simplest and easiest way to render a barcode on a web page is to make use of background colors in HTML tables. Each of the black and white bars of the barcode can be represented by the different columns of the tables using different colors. However, upon further investigation, this implementation is proned to problems. Modern browsers e.g. Internet Explorer, implement features to enable and disable the printing of background colors and images. By default, this option is set to "disabled", i.e. when a barcode (generated using HTML tables) is printed, the barcode may not appear as the browser chooses not to print the background. To print such barcodes, users need to follow instructions to enable the "Print Background Colors and Images" option.

 

After much thought and analysis, we employ a solution to make use of the HTML span element. An example as follows :

 

			
<SPAN style="BORDER-LEFT: black 0.02in solid; DISPLAY: inline-block; HEIGHT: 1in"></SPAN>
<SPAN style="BORDER-LEFT: white 0.06in solid; DISPLAY: inline-block; HEIGHT: 1in"></SPAN>
<SPAN style="BORDER-LEFT: black 0.02in solid; DISPLAY: inline-block; HEIGHT: 1in"></SPAN>
<SPAN style="BORDER-LEFT: white 0.02in solid; DISPLAY: inline-block; HEIGHT: 1in"></SPAN>

 

The barcode is represented by a series of span elements each with a different width representing a bar of the barcodes. The span elements also have alternating black and white colors without any space between them. The Javascript code, included in the source zip file, basically converts an input data to a series of alternating black and white bars as shown below. The Human Readable Text portion of the barcode is also rendered using HTML text.

 

htmlbarcode1.png

 

Using the Code

 

Create a HTML file (or you can use the Code39Barcode.html file provided in the source zip file). In the file, include the Code 39 barcode creation script.

 

			
<script type="text/javascript" src="code39.js"></script>

 

The script enables the generation of a Code 39 barcode. The first parameter is the input data and the second parameter is to indicate whether to generate the check chracter.

 

			
DrawCode39Barcode("123456",1);

 

To make use of the above function, include the following section into the body of the HTML file.

 

			
<div id="externalbox" style="width:4in">
<div id="inputdata">123456</div>
</div>
 
<br />
 
<script type="text/javascript">
/* <![CDATA[ */
  function get_object(id) {
   var object = null;
   if (document.layers) {
    object = document.layers[id];
   } else if (document.all) {
    object = document.all[id];
   } else if (document.getElementById) {
    object = document.getElementById(id);
   }
   return object;
  }
get_object("inputdata").innerHTML=DrawCode39Barcode(get_object("inputdata").innerHTML,1);
/* ]]> */
</script>

 

The main action is in the line:

 

			
get_object("inputdata").innerHTML=DrawCode39Barcode(get_object("inputdata").innerHTML,1);

 

This line of code gets the div element of id="inputdata" and replaces it with the output of the Javascript function DrawCode39Barcode. The output of the function is the series of span elements as described above.

 

The output of the HTML file is as shown below:

 

htmlbarcode2.png

 

Understanding the Code

 

The Javascript code translates the input data characters to a series of barcode patterns and then convert the patterns to a series of HTML span elements. For example, the character "1" is translated to the pattern "wttwttttwt". 't' is known as the thin bar of the barcode while 'w' is known the thick bar of the barcode. Under the Code 39 specifications, the thick bar can be from 2-3x the width of the thin bar. So the first step to create a Code 39 barcode means translating the input data to a series of thick and thin bar patterns. For example, the input "123" can be translated to the following patterns:

 

"wttwttttwtttwwttttwtwtwwtttttt"

 

The "EncodeCode39" Javascript function performs the task of what is described above.

 

  function EncodeCode39(data,checkDigit)
            {
                var fontOutput = ConnectCode_Encode_Code39(data,checkDigit);
                var output = "";
                var pattern = "";
                for (x = 0; x < fontOutput.length; x++)
                {
                    switch (fontOutput.substr(x,1))
                    {
                        case '1':
                            pattern = "wttwttttwt";
                            break;
                        case '2':
                            pattern = "ttwwttttwt";
                            break;
                        case '3':
                            pattern = "wtwwtttttt";
				.
				.
				.
				.
				.
                        case 'Z':
                            pattern = "twwtwttttt";
                            break;
				default : break;
                    }
                    output=output+pattern;
                }
                return output;
            }

 

The next step involves translating the barcode patterns to a series of HTML span elements.

 

"wttwttttwtttwwttttwtwtwwtttttt"

 

The series of patterns above gets translated to the following :

 

			
<SPAN style="BORDER-LEFT: black 0.02in solid; DISPLAY: inline-block; HEIGHT: 1in"></SPAN>
<SPAN style="BORDER-LEFT: white 0.06in solid; DISPLAY: inline-block; HEIGHT: 1in"></SPAN>
<SPAN style="BORDER-LEFT: black 0.02in solid; DISPLAY: inline-block; HEIGHT: 1in"></SPAN>
<SPAN style="BORDER-LEFT: white 0.02in solid; DISPLAY: inline-block; HEIGHT: 1in"></SPAN>
<SPAN style="BORDER-LEFT: black 0.06in solid; DISPLAY: inline-block; HEIGHT: 1in"></SPAN>
<SPAN style="BORDER-LEFT: white 0.02in solid; DISPLAY: inline-block; HEIGHT: 1in"></SPAN>
<SPAN style="BORDER-LEFT: black 0.06in solid; DISPLAY: inline-block; HEIGHT: 1in"></SPAN>
<SPAN style="BORDER-LEFT: white 0.02in solid; DISPLAY: inline-block; HEIGHT: 1in"></SPAN>
.
.
.

 

The code for performing this task is found in the function "DrawBarcode_Code39". Other functions worth mentioning is the code for generating the Check Character. This can be found in the "generateCheckDigit" function.

 

Points of Interest

 

The solution has been tested to work on the following browsers:

 

  • IE 6, 7 and 8
  • Firefox 3.5+
  • Chrome 5,6
  • Opera 10+
  • Safari 5

 

When the barcodes are printed, they can be easily scanned by various brands of barcode scanners. On top of that, most newer browsers also tend to respect the width settings of the HTML span element very accurately. This allows very precise barcodes to be created. Moreover, it is important to remember that this solution is interesting as it does not make use of server-side programming or graphical image technology.

 

License

 

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

 

About the Author

 

 

 

ConnectCode
 
Singapore Singapore
Member
 

 

No Biography provided

 

分享到:
评论

相关推荐

    生成Code39(extend)条形码源码 GenerateCode39Barcode.rar

    生成Code39(extend)条形码源码 源码描述: 一、源码特点 C#GDI+绘制Code39条形码 。网上开源的众多,但却都是根据内容和长宽来确定模块宽度。本次由于某需求需要根据模块宽度以及数据多少来确定条形码的长宽。识别...

    QR CodeBarcode Scanner and Generator--Cross PlatformPro

    Unity3D 二维码插件 最新版, QR CodeBarcode Scanner and Generator--Cross PlatformPro.unitypackage

    unity二维码识别最新插件QR CodeBarcode4.9.rar

    QR CodeBarcode Scanner Generator是一个高效的代码扫描器和生成器工具,它运行跨平台,支持扫描QRCode,Code_128,Code_93,Code_39,EAN_13,EAN_8,Aztec代码,UPC-A,UPC-E,数据矩阵,PDF-417, RSS_14等,支持...

    js-barcode:条码的纯JavaScript实现

    js条形码条码的纯JavaScript实现当前,仅支持代码39。 在您的html中使用条形码code39.js并使用GenerateCode39函数获取条形码。... useCanvas : true = Draw the code 39 barcode in a 2d canvas context. false = Retur

    QR CodeBarcode Scanner and Generator(Cross PlatformPro 4.9).zip

    QR CodeBarcode Scanner Generator是一个高效的代码扫描器和生成器工具,它运行跨平台,支持扫描QRCode,Code_128,Code_93,Code_39,EAN_13,EAN_8,Aztec代码,UPC-A,UPC-E,数据矩阵,PDF-417, RSS_14等,支持...

    Javascript-Barcode-Reader:简单快速的条码解码器,支持 javascript 中的 Code128、Code93、Code39、StandardIndustrial 2 of 5、Interleaved 2 of 5、Codabar、EAN-13、EAN-8 条码

    适用于浏览器和 Node.js 的简单快速条码解码器,能够读取 Code128 (UCC/EAN-128)、Code93、Code39、标准/工业 2 of 5、Interleaved 2 of 5、Codabar 和 EAN-13 条码。 现在试试 可用的解码器 EAN-13 EAN-8 代码-...

    Code39和Code128条形码辅助类BarCode Helper

    `BarCode Helper`的使用非常简单,它提供的API允许开发者指定条形码类型(Code39或Code128)、输入数据、图片尺寸等参数,然后直接生成条形码图片。这大大减少了在软件开发中处理条形码生成的复杂性。同时,由于生成...

    CODE128,CODE39生成打印代码

    在提供的文件列表中,我们可以看到一些源代码文件(如barcode.cpp、Code128.cpp、Code39.cpp)和项目文件(如barcode.dsp、barcode.dsw),这表明作者可能正在开发一个条形码生成器应用程序。`barcode.exe`可能是这...

    条形码字体barcode39

    条形码字体`barcode39`,也称为Code 39或Alpha39,是一种广泛应用于工业、商业和医疗领域的条形码标准。这个条形码格式最初设计时支持数字0-9、大写字母A-Z以及一些特殊字符,如空格、美元符号、加号、减号、百分比...

    barcode 39,128,93.zip

    本压缩包“barcode 39,128,93.zip”包含与三种特定条形码格式——Code 39、Code 128以及Code 93相关的资源,适用于生成和打印条形码,甚至进行自定义设计和激光打标。 1. **Code 39**:也称为USD-3或Alpha39,是最...

    barcode39 识别

    压缩包中的"Barcode39.dll"可能是一个用于识别39条形码的动态链接库,而"test_ocr_barcode.exe"可能是配套的测试应用程序,它们共同构成了一个完整的条码识别系统,能够读取和解析39条形码。 综上所述,39条形码...

    QR CodeBarcode Scanner and Generator--Cross PlatformPro 4.9.zip

    本文将深入探讨Unity中的二维码生成与识别技术,以及如何通过"QR Code Barcode Scanner and Generator--Cross Platform Pro 4.9.unitypackage"这个插件来实现这一功能。 首先,我们来看二维码生成。在Unity中,我们...

    CODE39码介绍

    - **标准型CODE39**:这种类型的CODE39码可以表示大写字母(A-Z)、数字(0-9)以及其他特定的符号(如“-”、“.”、“$”等),总共包含44个字符。 - **完全型CODE39 (Full ASCII)**:除了包含标准型CODE39的所有...

    vb.net条码生成器BarCode39

    BarCode39是一种广泛使用的条形码标准,也称为Code 39或Alpha39,它能够编码数字、字母和某些特殊字符。在VB.NET中实现BarCode39生成器,我们可以利用各种库或自定义代码来完成。 首先,让我们了解一下Code 39条形...

    javascript网页输出Code128条码

    javascript实现页面输出Code128条码;div绘图模式;无需字体库; 使用方法: Barcode.create({options}).encode("string", type).tostring(); options: barWidth: 条码宽度,必须为1px的整数倍; barHeight:条码...

    Code128 code39等一维条码生成类库事例

    Barcode code39 = new Barcode(); code39.Type = BarcodeType.CODE39; code39.Code = "ABC123"; code39.IncludeQuietZone = true; code39.BarHeight = 50; // 生成位图并保存 Bitmap code39Bitmap = code39.Render...

    QR CodeBarcode Scanner and Generator 4.6.rar

    "QR Code Barcode Scanner and Generator 4.6" 是一款针对二维码和条形码进行扫描与生成的软件,特别适用于Unity开发环境。本文将深入探讨这款软件的功能、使用方法及其在网络应用中的价值。 首先,我们来看一下...

    Code39条码编程

    Code39条码的编码机制基于一个简单的规则集,它能够表示包括数字(0-9)、大写字母(A-Z)、以及一组特殊字符在内的44种字符。每个字符由5条和4空组成,其中包含3个宽单元,通过不同宽度的线条和空隙来区分。具体而...

    QR CodeBarcode Scanner and Generator-Cross Platform Pro v5.1.rar

    https://assetstore.unity.com/packages/tools/integration/qr-code-barcode-scanner-and-generator-cross-platform-pro-56083

    barcode39 识别 调用DEMO

    在提供的文件列表中,我们看到有多个`.bmp`图像文件,如`src.bmp`、`barcode39_0.bmp`等,这些很可能是包含Code39条形码的图像样本。条形码识别软件或库通常会解析这些图像以提取其中包含的信息。例如,`Barcode39....

Global site tag (gtag.js) - Google Analytics