Dynamically building barcodes on the Web
This article discusses how to dynamically create barcodes on the web based on database fields, this is useful in situations where the user enters information into the system and then displays a summary page "This is what you typed", which can be printed and can be tracked later with a barcode.
The key to achieving this is how to translate values such as "ABC" into barcodes. Here we use "Code 39", for more information on it, see the definition. I want to display the resulting barcode on a Web page so that the user can print the page that contains the barcode, and then use it in the processing of the graphics system. Many users have the "Code 39" font installed in the system, so I can change it on the page, but I don't want to rely on them. I want to build on the page and be independent of the browser and the computer.
My solution is to create a GIF graphics file for each character supported in Code39. I use the Microsoft Paint software to create a text box with a Code39 font size, and then build a graphic for each character. Next, use Microsoft Image composer to convert them to GIF format. Finally, use the scanning software to check these characters to see if they can be interpreted correctly. Below, you will see barcodes for a, B, C, and asterisk (*):
Code for A
Code for B
Code for C
Code for Asterisk
Then, in order to depict the barcode of the value in the database in the ASP page, simply traverse each character in the Barcodetext variable string, and then build the < IMG src= "..." > tag to replace the character with the corresponding barcode graphics file. This is very simple and does not require a font file to be installed on the client. The relevant code is as follows:
' Code barcodes require an asterisk as the start and stop characters
Response.Write "< IMG src=" "Http://edu.cnzz.cn/NewsInfo/http://edu.cnzz.cn/NewsInfo/asterisk.gif" "width=" "30" Height= "" > "
For x = 1 to Len (Trim (RST ("Barcodetext"))
Response.Write "< IMG src=" "" & Mid (RST ("Barcodetext"), x,1) & _
". gif" "width=" "," "height=" "," ">"
Next
' Code barcodes require an asterisk as the start and stop characters
Response.Write "< IMG src=" "Http://edu.cnzz.cn/NewsInfo/http://edu.cnzz.cn/NewsInfo/asterisk.gif" "width=" "30" Height= "" > "
If the value that is barcodetext in the recordset's current record is "ABC", then the result of the HTML after executing the above code is:
< IMG src= "http://edu.cnzz.cn/NewsInfo/http://edu.cnzz.cn/NewsInfo/asterisk.gif" width= "height=" >
< IMG src= "http://edu.cnzz.cn/NewsInfo/A.gif" width= "height=" >
< IMG src= "http://edu.cnzz.cn/NewsInfo/B.gif" width= "height=" >
< IMG src= "http://edu.cnzz.cn/NewsInfo/C.gif" width= "height=" >
< IMG src= "http://edu.cnzz.cn/NewsInfo/http://edu.cnzz.cn/NewsInfo/asterisk.gif" width= "height=" >
The following graphic is then displayed:
Now that the page is printed, it can be tracked by the barcode scanner.
There are 2 things to note about barcodes: Not all barcodes can support all character sets. Code39 is a very good and simple one. It has a variable length that supports all uppercase letters, numbers, asterisks, and 7 special characters. For each character, it uses a consistent 5-line (2-width 3-narrow) 4-empty (meaning gap, 1-width 3-narrow) Form. Additional character sets support more, using different decoding schemes. Make sure that you have a GIF graphic file for each character, and that you can modify the width and height of the graphic to display slightly larger or slightly smaller barcodes on the page.
In addition, if you encounter special characters, the traversal loop code above will not work because the GIF file cannot be named +.gif. At this point, write a function that accepts character ASCII values, and then use the case statement to return the string equivalent to the filename. If the value of the database field is integral or contains only characters or numbers, the traversal code above is good!
The method described above is not very complicated, but in some ways it is simple and easy to use.