SoftwarePractice.org: Home | Courseware | Wiki | Archive

Barcode Character Recognition

From SoftwarePractice.org

The image processor outputs a single one dimensional binary array, which is passed to the character recognition functions. There are a number of tests and operations that the character recognition phase involves to determine the barcode characters from this array.


Contents

Code 39 Testing

There were several tests which needed to be performed on the inputted barcode to determine if it was a Code 39 barcode. These tests were implemented as separate functions in our Matlab code, and are explained below.

White Space Test

The Code 39 standard specifies that each character in a barcode should be separated by a narrow space, referred to as an inter-character space. Therefore, every 13th element should be a zero. As the star character is removed in the pre-processing phase of the Matlab code, the code below implements this testing from the first element of the input, representing the first inter-character space.

       count = 0;
       for k=1:13:(length(x)-12)
           if x(k) == 0;
               count = count + 1;     
           end
       end
   
       if count == n
           y = 0;
       else
           y = 22;     %ERROR: White space test failed
       end


Test 3 of 9

Each Code 39 barcode character is composed of 6 narrow and 3 wide elements, and the first and last elements are always ones. The following code shows how we implementing the testing of this condition:

       count=0;
       for z=1:h
            if chArray(z,1) == 1 && chArray(z,12) == 1
               for v=1:(w-1)
                   if chArray(z,v) ~= chArray(z,(v+1))
                       count = count + 1;
                   end
               end
            end
       end
       if count == 8*h
           y = 0;
       else
           y = 24;     %ERROR: Character/s not 3 Wide, 6 narrow
       end


Star test

Every Code 39 barcode starts and ends with a star character. Once the process of character recognition has been completed in our program, the final character is check to see if it is a star. If so, it is removed. Otherwise, an error message is displayed on the GUI. The start star character at this point has already been detected and removed by the pre-processor.

            if asciiChars(length(asciiChars)) == '*' 
               asciiChars(:,length(asciiChars))=[];
               decimalChars(:,length(decimalChars))=[];
               resultCode = 0;
           else
               resultCode = 26;    %ERROR: No trailing star found
           end



Barcode Character Recognition

In order to determine the characters in a barcode image, we needed an efficient way of creating a lookup table which stored the characteristics or values of each unique character. With the use of a lookup table, the program could compare the values of the inputted barcode with the table and determine which characters it represented.

For example, the letter 'A' in a Code 39 barcode is represented by:

110101001011 (where a 1 represents a bar, 0 a space)

Rather than use this representation, we decided to implement a more efficient method by converting each binary character to a decimal number. For the example shown above, 'A' would then be represented by 3403.

The lookup table was implemented in Matlab as a 44x2 array. The first column contains each character's decimal value (ascii char converted to decimal format), and the second column contains its value as calculated above.

The following is a sample of this code, taken from the function lookupTable.m


TABLE =

[48, 2669;

49, 3371;

50, 2859;

51, 3477;

52, 2667;

53, 3381;

54, 2869;

55, 2651;

56, 3373;

57, 2861;

65, 3403;

66, 2891;

67, 3493;

68, 2763;

.........

.........]



Checksum Calculation

The barcode GUI allows the user to check a tickbox if the barcode contains a checksum digit. This optional checksum is utilised in barcodes to provide additional testing to detect errors in the read. The checksum is ultimately an extra character which is appended to the barcode before the last star character.

Each character has its own unique check value, ranging from 0-42. The checksum is calculated based on all characters except the start and end star characters (and of course the checksum itself), using a Modulo 43 method.


The checksum digit is calculated as follows:

1) Take the check value (0-42) of each character in the barcode

2) Sum all the values from step 1

3) Divide the result from step 2 by 43

4) The remainder from step 3 is the check value of the character that will be appended to the barcode before the stop character


Checksum Example


Image:CODE.gif

To calculate the checksum digit for the above barcode:

1) Calculate the check values: C=12, O=24, D=13, E=14

2) Sum all the values from step 1: 12 + 24 + 13 + 14 = 63

3) Divide the result from step 2 by 43 = 1 remainder 20

4) The remainder from the division in step 3 is 20, so the checksum character is K


The resulting barcode with the appended checksum is:

Image:CODEK.gif


Below is a sample of the code used in our function to calculate the checksum:


   for j=1:(length(x)-1)
       for k=1:length(TABLE)
           if x(j) == TABLE(k,2)
               z(j) = k-1;
           end
       end
   end
   checksumTotal = sum(z);
   remainder = rem(checksumTotal,43);
   y = char(TABLE((remainder+1),1));



Extension to Full Ascii

At a late stage in our program development, we extended our Matlab code to recognise barcodes that encoded all 128 ASCII characters. Initially, we had only accounted for 44 characters (0-9, A-Z, -, ., SPACE, %, /, +, %, *).

The extension to Full Ascii Mode is achieved by using the +, /, %, and $ symbols as "shift" characters. These shift characters are combined with a single (following) character to represent a Full ASCII character.

As an example, in an Extended Ascii barcode, lowercase letters are represented as a “+” character preceeding the uppercase version of the letter.


Extended Ascii Example

Taking the example in the above section:


“CODE” is represented as: *CODE*

Image:CODE.gif


“code” is represented using Full Ascii Mode as: *+C+O+D+E*


Image:Code lowercase.gif


Note: An Extended Ascii Code 39 Table can be found at barcodeisland.com. See References


Below is a sample of our code to calculate the Extended Ascii characters:

   adjustValues = 0;
   new_Chars = 0;
   counter = 1;
   k = 1;
   y=;
       while k <= length(decimalChars)
           if asciiChars(k) == '+'
               new_Chars(counter) = decimalChars(k+1);
               adjustValues(counter) = 32;
               counter = counter + 1;
               k = k + 2;
           
           else if asciiChars(k) == '/'
               new_Chars(counter) = decimalChars(k+1);
               adjustValues(counter) = -32;
               counter = counter + 1;
               k = k + 2;
           
           else if asciiChars(k) == '%'
                       
               new_Chars(counter) = decimalChars(k+1);
                   if (convertToCharacter(new_Chars(counter)) >= 70 && convertToCharacter(new_Chars(counter)) <= 74) 
                        adjustValues(counter) = -11;
                   end
           
                   if (convertToCharacter(new_Chars(counter)) >= 75 && convertToCharacter(new_Chars(counter)) <= 79)     
                       adjustValues(counter) = 16;
                   end
           
                   if (convertToCharacter(new_Chars(counter)) >= 80 && convertToCharacter(new_Chars(counter)) <= 83)    
                       adjustValues(counter) = 43;
                   end
           
               counter = counter + 1;
               k = k + 2;
           
           else
               new_Chars(counter) = decimalChars(k);
               adjustValues(counter) = 0;
               counter = counter + 1;
               k = k + 1;
           end
           end
           end               
       end
   new_Chars = convertToCharacter(new_Chars);
   new_Chars = new_Chars + adjustValues;
   y = char(new_Chars);



To return to main page of Team B click here Bar code reading from image

Personal tools