The Barcode Reader project presented on this page allows to scan barcodes using a webcam.
It uses two Open Source libraries: OpenCV and zxing.
OpenCV is a library for camera input and image processing.
zxing is a library to recognize multi-format 1D and 2D barcodes.
zxing recognizes the following barcode types:
- QR Code
- Data Matrix
- Aztec ('beta' quality)
- UPC-A and UPC-E
- EAN-8 and EAN-13
- Code 39
- Code 93
- Code 128
- RSS-14 (all variants)
- RSS Expanded (most variants)
The original C++ version of zxing does not compile on Windows, therefore we provide patched source code
for download here, which can be compiled with Visual-C++. zxing required a few minor changes of about ten
files to make it compile with Visual-C++.
Since OpenCV and zxing are cross-platform libraries, the project should run on the following platforms:
Windows, Android, Maemo, FreeBSD, OpenBSD, iOS, BlackBerry 10, Linux and OS X.
However, we did only test it on Windows so far.
OpenCV and zxing provide so much functionality that the Barcode Reader application requires only
about 300 lines of code, mainly for initialization.
Some comments on the Barcode Reader application:
- We experienced good barcode scanning results with a webcam that has an auto-focus.
We used a HD 720p webcam from Logitech.
- The scanned barcodes must be illuminated brightly. If the barcode is scanned too dark
or shadowed, it will not be recognized.
- Good illumination and an auto-focus are very important! If the barcode image is not sharp,
barcode recognition will fail. You will notice that the webcam sometimes has difficulties to focus
barcodes. Give it some time and illuminate the barcode brightly. Once the auto-focus works,
scanning will work very good and very fast over a long series of different barcodes.
- We included a PDF file (Barcode Samples.pdf) with example barcodes. The PDF file has been created
using our product Virtual Print Engine (VPE). Simply print the PDF and afterwards you can use it to
test barcode recognition.
- Unfortunately the HD resolution of the webcam – which would yield much better barcode recognition
results – can not be used with OpenCV. At least not on Windows. When used, OpenCV will hang the second
time the Barcode Reader application is executed. Beware: The process can not be terminated in this
case – you need to hard-reboot! Therefore the Barcode Reader application only uses 640 x 480 resolution,
which already gives good barcode recognition results. We tried to use the VideoInputLibrary to retrieve
images from the webcam, but it also hangs when using HD resolution. Later we found out that the
VideoInputLibrary is used internally in the Windows version of OpenCV. If anyone can provide a better
library to retrieve images from a webcam, please send us an e-mail.
- When the Barcode Reader application can successfully decode a barcode, it beeps and displays the
barcode content in the text window.
- The live view of the webcam is shown in a smaller window, which can be scaled by code.
This allows to embed it into a host application.
- You can terminate the Barcode Reader application by pressing the ESCape key while the webcam window
has the focus.