Reading a barcode?

Started by
4 comments, last by Jonny_S 13 years, 9 months ago
Hi all,

The conundrum of reading a barcode, coupled with my boredom has caused me attempt to write a simple barcode reader in C#. I'm pretty new to image processing as a whole but I thought starting with simple barcode. I could build on some fundamentals and keep going until I get bored.

My first attempt was to read pixels from left to right until I reached a black pixel. The first bar of a barcode should always be the width of one bit (or so I thought) so if I could get the width of that one bar, I could then loop through getting the values for all bars based on colour (i.e 1 for black, 0 for white). However, it seems from the examples I've been using, bar widths are fairly inconsistent and so I'm wondering what another approach could be?

I was wondering if anyone is pretty savvy in the image processing world or knows how actual photographic scanners work could give me any advice?

Thanks,

Jon

[edit]
I've finished uni, I'm jobless I am actually this bored...
Advertisement
Do you get a perfect bar code (i.e. a bitmap with the bars perfectly pixel aligned) or is it a photograph from a real world bar code?

If the latter, it might be worth looking into Fourier Transformations: http://homepages.inf.ed.ac.uk/rbf/HIPR2/fourier.htm. I haven't worked with it in software yet, but it's a good technique to either retrieve the bar code or to sharpen / rotate the image so you can read it more accurately.
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
I was starting with a perfect barcode, just an example I grabbed off wikipedia. However, I was hoping to move on to actual photos once I'd perfected the algorithm, so I'll look into the fourier transform.

My current issue comes from working out how many bars are in a block, which is sort of shown here.

I thought if I could get the width of 1 bar, such as the start bar (which is always 1 bar) I could then split up blocks of bars easily. However it turns out bar widths aren't always exact, so I was wondering if I should continue down this line of thought or if there is a better way to do it.

Hope that makes sense,
Which kind of barcode are you trying to read? There are dozens of different types? I'd recommend trying to read an "Interleaved 2 of 5".
For that, you could scan across the image to find out:

the widest black bar
the narrowest black bar
the widest white bar
the narrowest white bar

Next, you pick a cut-off value such as half way between the widest and narrowest and scan across the image again. anything that is wider than the cutoff counts as a wide bar, and the rest are not.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
FFT's and image scanning sounds like overkill to me. If you have the right barcode reader hardware, it will convert a barcode into ascii text. Back when I worked on political campaigns (about 10 years ago), we would use a barcode font to print voter ids right onto the address lists we would give to volunteers. At the end of the day, after they'd finished canvassing, we would take the lists and use a barcode scanner to immediately look up the voters who had been contacted to flag their record in our database depending on if they wanted a yard sign or what not (and to avoid canvassing them a second time as well). I suppose today this would all be managed through smart phones and the like.

At any rate, if your goal is to investigate image processing using barcodes, you ought to be able to find free barcode fonts on the internet that you can use with Photoshop or what not to build the images that you'll later try to take apart.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Thanks for the responses, I was trying to read a UPC barcode and I think I'm on the path to success.

I've scanned through and found the minimum bar width and the minimum space width. I've then scanned through each bar, got the bar size and divided it by the minimum size to see how many "bits" are in the bar. If the division has a remainder I round up or down which takes care of any left over pixel due to inconsistent bar sizes.

Seems to be working at the moment, I'm collecting a lot of barcodes to do a thorough test.

This topic is closed to new replies.

Advertisement