The first step in Recognition and Conversion of barcode, can be easily achieved by several edge detection techniques. We have employed the fact that the barcode is an intensity image. It contains vertical strips of black and white lines. So we use the gray thresh value of the image to detect the start and the end of the barcode strips. This can be achieved by the wavelet transform method [3] or by morphological operations [10] but we want a method that is both quick and efficient so we use the scanline based approach. This method uses the fact that a horizontal scanline is at the center of the barcode. If the image of the barcode is not centered or the center region contains distortions then the algorithm will simply shift the scanline above or below the center of the image until we get a non distorted region.

1. Binarization

The next step is the Binarization of the image. We have to distinguish the barcode i.e., black strips from the white background i.e., spaces. First the start and the end of the barcode is detected i.e., first black strip and last black strip. Then scanline scans the pixels from the center. Then the gray thresh values are used to assign 1s to spaces and 0 s to strips. If the gray thresh is other than that of black or white. We have defined two ranges for gray thresh values, one range is allotted 1 for white and another is assigned a binary 0 for black. If the gray thresh value of a pixel lies within the white range it is assigned binary 1 i.e., we say that this pixel is white or space but has been discolored, if it lies in other range then it is assigned the opposite binary number i.e., 0 we say that the pixel was black but has been decolored. In this way the decolored pixels that may arise in the barcode image are accounted for and thus a type of noise is taken care of. This method is derived from the luminous method described in [1]. In this method without searching for the edges the scanline binarizes all the pixels. The pixel selected first is the middle one. To cater for the noises first the scanline pixels are smoothed and then the luminance value Y(x) Ԑ [0, 1] is calculated for each pixel, whose position is given by:


Then the local minima and maxima are computed

Fig. 2 Block Diagram of our Algorithm, Input is an image of barcode. Which is detected and binatized by scanline approach, then error check and decimal conversion are applied.

2. Error Checking

Once the image has been binarized then all we have to do is to use our knowledge about the construction of the barcode, this in our case is achieved by the knowledge of the UPC’s encoding process. In nearly all the Barcode standards we have a total of 13 decimal digits. The last is the parity bit. This digit is used to provide the second step of the double error check.

A.     The UPC standard [12]

An UPC-A barcode consists of four areas:

  1. The number system
  2. The manufacturer code
  3. The product code
  4. The check digit

Number Systemis for the class or type to which the product belongs.

The manufacturer codeis given to each manufacturer by UCC council for companies.

The product codeis a 5-digit number that the manufacturer gives to his products.

The check digit verifies the validity of the product. It tells whether the barcode has been scanned properly of not.

The barcode begins & ends with a ‘101’ or “black-white-black” pattern, these are also termed as guard bars. Between these guard bars, we have two blocks of black and white strips, separated by a central bar which is a ‘01010’or“white-black-white-black-white” pattern. The two blocks on either side of the central bar contain the decimal code, which is different for each product. Each decimal digit is encoded by using 7 bits. Even and Odd alphabets are used for the encoding. First digit is called the meta-number or induced digit[1].

To ensure that the scanline does not go outside the boundary of the image, we set our starting position in the middle of the image, to the value where luminance is 0 or we start with a bar (black). Then we scan left and right from this point by using pre-determined steps or blocks of pixels. The choice of the steps helps us in finding the guard bars, which are used for the first step of the error detection.

The error detection is as follows:


  • The first 3 bits of the binarized image are equal to 101.
  • The bit 46 is 0, 47 is 1, 48 is 0, 49 is 1 and 50 is 0.
  • The last three bits i.e., 93, 94 and 95 are equal to 101.


The binarized image is a valid UPC code. If any one of the above mentioned conditions is not satisfied then the code is not a valid UPC code.

3.  Conversion

For the conversion of the binarized image to the decimal we again use our knowledge of the UPC construction. There is a lookup table [14] with which we compare our bits.

We ignore first and last 3 bits as they are the guard bits. Also we do not consider the central bits. As these bits are same for all the barcodes. Starting from the 4th bit (first three bits are guard bits) and selecting seven bits at a time. These seven bits are compared with the lookup table of Figure 2 and by comparison we allocate decimal values to the block of seven bits.  We can see from the table that if we are looking at bits to the left of the central bits then we use the “Left Bit Pattern”column to compare, while if we are looking at bits on the right of the central bits we use the “Right Bit Pattern”.


Left Bit Pattern Right Bit Pattern
0 0001101 1110010
1 0011001 1100110
2 0010011 1101100
3 0111101 1000010
4 0100011 1011100
5 0110001 1001110
6 0101111 1010000
7 0111011 1000100
8 0110111 1001000
9 0001011 1110100

Once the 12 digits have been calculated the check sum bit is calculated, it is calculated by the following method [14]

  1. The digits that are in the odd numbered positions are added and multiplied by 3.
  2. The digits in the even numbered positions are added.
  3. The sum of even numbered is added to the sum of odd numbered.
  4. The result is modulo 10.
  5. If the result is zero then check bit is zero. Otherwise subtract it from 10 and the answer is the result.
all clear;
input = imread('4.jpg');
[r c]=size(input);
for i=1:r
for j=1:c
if input(i,j)<t
if input(i,j)>t

%---------CROP Scanline--------------
sizeimg = size(input);
for i=1:sizeimg(2)
binimg1(i)=input(round(sizeimg(1)/2), i);
% figure
% subplot(1,2,1)
% imshow(binimg1)
% subplot(1,2,2)
% imshow(inv)
ind1 = find(inv, 1, 'first');
ind2 = find(inv, 1, 'last');
new_binimg=imcrop(binimg1,[ind1 1 ind2-ind1 0]);
for i=0:numel(scanline)
if scanline(i+1)==0
%----------------binary to decimal conversion---------
if (h(1)==1 && h(2)==0 && h(3)==1 && h(93)==1
&& h(94)==0 && h(95)==1 && h(46)==0 && h(47)==1
&& h(48)==0 && h(49)==1 && h(50)==0)



if cflag==1
for i=1:6
if bit1(1+7*(i-1):7+7*(i-1)) == [0 0 0 1 1 0 1];
elseif bit1(1+7*(i-1):7+7*(i-1)) == [0 0 1 1 0 0 1];
elseif bit1(1+7*(i-1):7+7*(i-1)) == [0 0 1 0 0 1 1];
elseif bit1(1+7*(i-1):7+7*(i-1)) == [0 1 1 1 1 0 1];
elseif bit1(1+7*(i-1):7+7*(i-1)) == [0 1 0 0 0 1 1];
elseif bit1(1+7*(i-1):7+7*(i-1)) == [0 1 1 0 0 0 1];
elseif bit1(1+7*(i-1):7+7*(i-1)) == [0 1 0 1 1 1 1];
elseif bit1(1+7*(i-1):7+7*(i-1)) == [0 1 1 1 0 1 1];
elseif bit1(1+7*(i-1):7+7*(i-1)) == [0 1 1 0 1 1 1];
elseif bit1(1+7*(i-1):7+7*(i-1)) == [0 0 0 1 0 1 1];

for i=7:12
if bit1(48+7*(i-7):54+7*(i-7)) == [1 1 1 0 0 1 0];
elseif bit1(48+7*(i-7):54+7*(i-7)) == [1 1 0 0 1 1 0];
elseif bit1(48+7*(i-7):54+7*(i-7)) == [1 1 0 1 1 0 0];
elseif bit1(48+7*(i-7):54+7*(i-7)) == [1 0 0 0 0 1 0];
elseif bit1(48+7*(i-7):54+7*(i-7)) == [1 0 1 1 1 0 0];
elseif bit1(48+7*(i-7):54+7*(i-7)) == [1 0 0 1 1 1 0];
elseif bit1(48+7*(i-7):54+7*(i-7)) == [1 0 1 0 0 0 0];
elseif bit1(48+7*(i-7):54+7*(i-7)) == [1 0 0 0 1 0 0];
elseif bit1(48+7*(i-7):54+7*(i-7)) == [1 0 0 1 0 0 0];
elseif bit1(48+7*(i-7):54+7*(i-7)) == [1 1 1 0 1 0 0];
ODD = 3*(dec(1) + dec(3) + dec(5) + dec(7) + dec(9) + dec(11));
EVEN = dec(2) + dec(4) + dec(6) + dec(8) + dec(10);
check = 0;
i = 0;
while (check == 0)
if((i*10) > SUM)
CS = (i*10) - SUM
if ( CS == dec(12))
check = 1;

if check==1
else barcode=0;
if barcode==0
barcode='Error in Barcode';
barcode='Error in bar';


Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.