CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2002
    Posts
    1,798

    How to scan a barcode bitmap

    I have built a small program that converts a 16 x 16 element matrix of 1's and 0's into a 'barcode' bitmap. The 1's are black squares and the 0's are white squares. A randomized matrix of 1's and 0's is thus converted to a random barcode bitmap and can be saved to disk.

    Question: How can I scan the barcode on the disk to reconstruct the originating matrix?

    So far I have concluded that the bitmap must first be loaded into memory. Then the question becomes how to read the loaded bytes to get the matrix.

    Example:

    For the matrix:
    Code:
    16, 16
    0 0 1 0 1 1 0 0 1 0 0 1 0 1 0 0 
    0 1 1 0 0 0 0 1 1 0 0 0 0 0 1 0 
    1 1 0 0 1 1 1 1 1 0 1 1 1 0 1 0 
    0 1 0 0 1 1 0 1 1 0 1 1 0 1 0 1 
    1 1 1 0 1 1 0 1 0 1 0 1 0 0 1 1 
    0 0 1 0 0 0 1 1 0 0 1 0 0 1 0 1 
    1 0 0 1 1 1 0 0 0 1 0 1 1 1 1 0 
    0 0 0 0 0 0 1 0 0 1 0 1 1 1 1 1 
    0 1 0 1 0 1 1 1 1 0 1 0 0 1 1 0 
    0 0 1 1 0 0 1 0 0 1 0 0 1 0 0 1 
    1 0 1 0 1 0 1 1 0 1 0 1 0 0 1 0 
    1 0 1 1 0 0 1 1 1 1 0 0 1 1 1 0 
    0 0 1 1 0 0 1 1 0 0 1 1 1 0 1 0 
    1 1 1 1 0 1 0 0 0 0 0 1 1 0 0 1 
    1 1 0 1 0 1 1 1 0 1 1 1 0 0 0 1 
    0 1 1 0 0 1 0 1 1 0 1 1 0 1 1 1
    The barcode below is representative.
    Last edited by Mike Pliam; June 10th, 2012 at 03:58 PM.
    mpliam

  2. #2
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: How to scan a barcode bitmap

    If you have the barcode "scanned" in a perfect bitmap then all you need is determining if each "dot" in the barcode is either black or white. since you know each barcode is 16x16, you can split the bitmap into 16x16 blocks and determine the color used in each block.


    Scanning real life barcodes however is a considerably more complex undertaking, and in it's current form the barcode wouldn't be reliably readable.

    For starters, a barcode will need clearly identified markers indicating where a barcode begins and where it ends so you can properly scale and align the barcode.
    If you look at a regular UPC/EAN barcode, you'll notice a set of double lines at both ends, and another set of those lines about in the middle. Those lines are always there and serve as a reference for the scanner.

    If you look at 2D barcodes, like QR codes, you'll see they too have fixed defined areas where specific patterns of black and white need to go.

    When scanning, you also need to account for the fact that the scan may not have the bitmap perfectly aligned, so you need to adjust for that. As well as account for imperfections such as dirt, slightly warped images (because the barcode is on a not perfectly flat surface) and such more.

    Additionally, you need to make sure that your barcode has a sufficient amount of inversions.
    Suppose you wanted to create a barcode of 16 zero's... you can't really use a barcode that's just a white square. Ho about 16x 0xFFFF ? you can's have a block of black either. TO make sure barcodes are correct, they contain special code that makes sure there's enforced inversions of black to white and white to black.

    Most barcodes also have innate error correction or checkdigits built in to work around imperfections in the scan THis basically inserts extra bits in such a way that you can still read out the barcode even if you explicitely erase or blacken some parts of the image.

  3. #3
    Join Date
    May 2002
    Posts
    1,798

    Re: How to scan a barcode bitmap

    You have supplied some very important information that has helped my understanding of the problem greatly. Thank you. I will need to rethink the problem.
    mpliam

  4. #4
    Join Date
    May 2002
    Posts
    1,798

    Re: How to scan a barcode bitmap

    If you have the barcode "scanned" in a perfect bitmap then all you need is determining if each "dot" in the barcode is either black or
    white. since you know each barcode is 16x16, you can split the bitmap into 16x16 blocks and determine the color used in each block.
    OK. Using CRect to get a subrect, using the center point of that subrect and geting the pixel color works -- BUT -- I cannot seem to properly locate the desired square in the iimage. There is some problem with my graphics coordinates.

    I have attached a small demo to illustrate my problem. Given the 'square' image composed of 256 subrectangles, each subrectangle should have height and width 1/16 of the whole image. But when I attempt to identify, for example, the top left subrectangle by painting it red, it is not in correct position. (see below and attached demo).

    What have I done wrong here ?
    Last edited by Mike Pliam; June 19th, 2012 at 01:54 AM.
    mpliam

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: How to scan a barcode bitmap

    To FillRect you use m_cImage context. Therefore, you need to convert scree coordinates to m_cImage client.
    Code:
    	CRect rect;
    	m_cImage.GetWindowRect(&rect);
    	m_cImage.ScreenToClient(rect);
    You use default mapping mode, which is MM_TEXT, so Y axis grows down. Therefore, you need to add while calculating y2.
    Code:
    	y2 = y1 + rect.Height()/16;
    Attached Images Attached Images  
    Best regards,
    Igor

  6. #6
    Join Date
    May 2002
    Posts
    1,798

    Re: How to scan a barcode bitmap

    Yes. That works. Thanks, Igor.
    mpliam

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: How to scan a barcode bitmap

    Just taking the middle pixle will work for this "perfect" bitmap. but it'll be less than ideal in a real life situation since the scanned image will be imperfect for one of many various real life reasons.

    There's many solutions to that being used in actual barcode scanning, a simple one (not necesarrily the best one) would be to average the pixels in the square and determine if it's "closer" to white than to black. or if it's so close to the average that it's impossible to tell.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured