When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
Print this page.
Published: Friday, August 25, 2000

Dynamically Generating I25 Bar Codes

By Daniel Goncalves

More Bar Chart Articles!
To learn more about dynamically generating Code 39 bar charts through an ASP page, be sure to read: Generating Dynamic Code 39 Bar Code on a Web Page!

- continued -

This article demonstrates how to generate a barcode I25 (interleaved 2-of-5) using server-side VBScript without using a database. First, I've assumed that you don't know what is I25 and how it's encoded. Interleaved 2-of-5 Specifications
I25 is a barcode that can encode numbers only. We have ten patterns, each one represents a single digit (0 to 9) in our decimal numeric scheme. The following picture shows these patterns.

The I25 specification says that each symbol must be interleaved with another. Thus, a pair of digits can be represented by only one I25 unit. As showed in the figure below, the first digit is encoded in the bars and the second interleaved as spaces. Note that 2-of-5 becames from the fact that in a I25 unit there are five bars, two of which are wide and five spaces, two of which are wide.

The symbol in the figure cannot be read because is incomplete. A complete barcode I25 must have a leading quiet zone (usualy at least .25" of width), a start symbol, the number you wish enconded, the stop symbol and a trailing quiet zone. Also note that since the digits are interleaved, there must be an EVEN number of digits in the bar code. A bar code of "11" is legal I25 code, but a bar code of "111" is not.

How To Encode A Number To I25 Pattern
With this knowledge, you're ready to let me guide you to creating a VBScript function that will generate these barcodes. First we'll create a function called I25Encode(). This function will receive a single argument as a string that is the number we wish to encode and interleave. To do that, we'll create an array with 10 elements. Each element will contain the pattern for a specific digit. (If you are interested in seeing the results, go ahead and give the live demo a whirl!)

Function I25Encode(StringNumber)
   Dim asPattern(), sSTART, sSTOP

   Redim asPattern(10)

   ' start and stop patterns can be found in fig. 3
   sSTART = "NNNN"
   sSTOP  = "WNN"

   ' these patterns can be found in fig. 1
   asPattern(0) = "NNWWN"
   asPattern(1) = "WNNNW"
   asPattern(2) = "NWNNW"
   asPattern(3) = "WWNNN"
   asPattern(4) = "NNWNW"
   asPattern(5) = "WNWNN"
   asPattern(6) = "NWWNN"
   asPattern(7) = "NNNWW"
   asPattern(8) = "WNNWN"
   asPattern(9) = "NWNWN"

Now we'll perform some validations to the received argument. Note that the number of characters in the argument must be even; all characters in the argument must be between 0 and 9 (48 to 57 according to the ASCII table). Then:

   If (Len(StringNumber) Mod 2) <> 0 Then
      ' the number of characters in the
      ' argument must be odd
      I25Encode = ""
      Exit Function
   End If

   If Not IsNumeric(StringNumber) Then
      ' argument must be numeric
      I25Encode = ""
      Exit Function
      If (InStr(StringNumber, ".") <> 0) Or _
         (InStr(StringNumber, ",") <> 0) Then
         ' argument is numeric but contains invalid
         ' characters to us
         I25Encode = ""
         Exit Function
      End If
   End If

Ok! The validation is done. (The routine is more efficient if the validation section will placed between the Dim and the Redim statements. Do that If you want.) Now we must read the characters in the received argument to encode and interleave them with the following code:

   Dim sEncodedSTR, sUnit
   Dim iCharRead,   sDigit1, sDigit2, i

   sEncodedSTR = ""
   For iCharRead = 1 To Len(StringNumber) Step 2
       sDigit1 = asPattern( Asc( _
                 Mid( StringNumber, iCharRead, 1 ) ) - 48 )
       sDigit2 = asPattern( Asc( Mid( _
                 StringNumber, iCharRead + 1, 1 ) ) - 48 )

       sUnit = ""

       For i = 1 To 5
           sUnit = sUnit & Mid( sDigit1, i, 1 ) & _
                   Mid( sDigit2, i, 1 )

       sEncodedSTR = sEncodedSTR & sUnit

Well done! We already got the encoded and interleaved result. Just add the start and stop patterns and return them.

   I25Encode = sSTART & sEncodedSTR & sSTOP
End Function

Now we must read the returned encoded data and generate the barcode. We'll look at accomplishing this in Part 2!

  • Read Part 2

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article