Installing and using ZBar in Linux to scan bar codes with your Webcam

ZBar is an application that can scan and decode several bar code symbologies (including QR Code) from sources such as a Webcam or an image file. I had been wanting to install it for some time and was finally spurred on to do it by a request for help to get it working in the Sabayon Linux forums. Here are the steps I used.

1. I installed the package media-gfx/zbar-0.10-r1.
Note for Gentoo users: I merged the package with the imagemagick and v4l USE flags set, so that both zbarimg and zbarcam would be installed. If you only want to use ZBar with a Webcam then turn off the imagemagick flag and zbarimg will not be installed.
Note for Sabayon Linux users: The Entropy package was built with both the above-mentioned USE flags set, so zbarimg and zbarcam will be installed.

2. Then I checked the status of the installed package by using the Portage eix command:

$ eix -I zbar
[I] media-gfx/zbar
Available versions: (~)0.10-r1 {{X gtk imagemagick jpeg python qt4 static-libs +threads v4l xv}}
Installed versions: 0.10-r1(03:56:14 05/10/12)(X gtk imagemagick jpeg qt4 threads v4l xv -python -static-libs)
Description: Library and tools for reading barcodes from images or video

Sabayon Linux users could instead use the following command:

$ equo search --verbose zbar

3. Then I checked which video device my laptop’s inbuilt Webcam is:

$ ls /dev/video*

4. As it is /dev/video0, I launched zbarcam as follows:

$ zbarcam --raw /dev/video0
WARNING: no compatible input to output format
...trying again with output disabled
ERROR: zbar processor in zbar_processor_init():
unsupported request: no compatible image format

Clearly zbarcam was not recognising the Webcam.

5. As I had installed the package with the Video4Linux USE flag set, I then launched zbarcam with the following prefix:

$ LD_PRELOAD=/usr/lib/libv4l/ zbarcam --raw /dev/video0

The ZBar GUI window popped up and I could see myself in the window. So far, so good.

6. I held a QR Code 2D bar code in front of the Webcam, then held a 1D Interleaved 2 of 5 bar code in front of the Webcam. The following was displayed:

$ LD_PRELOAD=/usr/lib/libv4l/ zbarcam --raw /dev/video0

Both the QR Code and the I2of5 bar code were read correctly. If the --raw parameter is omitted then zbarcam displays the symbology of the bar code too:

$ LD_PRELOAD=/usr/lib/libv4l/ zbarcam /dev/video0

7. Then I tried using ZBar to read a bar code which I had previously saved as a .jpg file:

$ zbarimg --raw ~/zebra04.jpg
scanned 1 barcode symbols from 1 images in 0.04 seconds

$ zbarimg ~/zebra04.jpg
scanned 1 barcode symbols from 1 images in 0.04 seconds

To find out the other parameters available, use the commands:

$ man zbarcam
$ zbarcam --help
$ man zbarimg
$ zbarimg --help

ZBar is a nice tool. 😎