Image resolution and barcode reading
We often receive emails asking us to explain why our SDK is unable to decode a barcode from an attached image, and the answer is often that the resolution of the image is too low; but why does image resolution play such an important role in the decoding of a barcode?
What is resolution?
For flat bed scanners – Image resolution is normally defined as the number of dots (or pixels) per inch – and is referred to as “dpi”. If you scan using a low dpi setting then the image will be need less hard disk storage but will not be as clear. If you use a higher dpi setting then the image will need more hard disk storage but will be clearer.
For digital cameras – image resolution is normally defined in terms of mega-pixels or the number of dots in the image. This can often sound like a lot, but the dots-per-inch value obviously depends on the actual dimensions of the object being photographed.
What’s the right resolution for a barcode?
Resolution alone doesn’t matter – what does matter is how wide the barcode is in the image, measured in pixels.
The number of pixels required depends on the number of characters encoded and the type of barcode. A barcode can only be decoded if each bar and space in the original barcode is preserved in the scanned image – if the bars and spaces blur into each other then it makes it harder, if not impossible, to decode the barcode.
Take the following image below…
It looks like a very clear image of a barcode – but if you zoom in on the image then you will see that some of the bars and spaces are actually represented using similar shades of grey….
…and this is a very strong indication that the resolution is too low.
What is a barcode module?
A module is the smallest width of bar or space in a barcode and all other bars and spaces are multiples of a module.
A perfect scan of an image would only need to use 1 pixel in the image for each module in a barcode – but if you think of the scanning process as like placing one grid on top of another, then unless the two grids line up exactly, the resultant image will be mostly shades of grey.
In practise you should allow at least 2 pixels for each module.
How to Calculate the Size
It is possible to work out the correct pixel width for a given barcode, but you need to know the following information:
- Type of barcode being used
- Number of characters encoded in the barcode (N)
P = 24 * (N + 2)
For a Code 25 barcode…
P = (14 * N) + 16
For a Code 128 barcode encoding digits using symbol set C…
P = (11 * N) + 66
For other Code 128 barcodes…
P = (22 * N) + 66
Note that Code 128 barcodes can contain a mixture of symbol sets and so the calculation can be more complicated. Hint: Count the number of black bars, subtract 10 and divide by 3 – if this matches the number of characters then you are not using symbol set C; if this comes to half the number of digits then you are using symbol set C; somewhere in the middle means you are using a mixture.
If W = width of barcode in inches then the resolution (R) can then be calculated as:
R = P / W
Note that the scanner resolutions are normally values such as 100, 200 etc, so if R worked out as 134 then it would be best to use the resolution of 200dpi.