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.
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.
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.
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.
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.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.