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. 😎

About Fitzcarraldo
A Linux user with an interest in all things technical.

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

  1. anthonyvenable110 says:

    Reblogged this on anthonyvenable110 and commented:

  2. Rahul Panwar says:

    Its really a nice tool, i will try this on my laptop 🙂

    Please take a look on my blog


  3. This worked great for me. Thank you. I’m also using Gentoo.

  4. Pingback: How to create QR Codes easily in Gentoo Linux | Fitzcarraldo's Blog

  5. Pingback: More tools for creating QR Codes in Linux | Fitzcarraldo's Blog

  6. Jo says:

    its only a toy, has a good scan-quality(!) and is nothwithstanding nearly unuseable for professional use, because it doesn’t stop. So you can only write the barcode (a whole bunch of them) but you can’t get timestamps in the same step – and get a line-identity-problem that way.

    The problem is:

    zbarcam is going asleep between his single (very short) scan-activities – so you can’t kill it from script, because its “S” (=> ps ax) – holy cra* )

    the solution: take the sourcecode of tbarcam, erase the loop-function and compile new: working “1 step scan – then quit”, so you can it use in a script: “scan 1 step, add the timestamp, quit, again …”

    a second workaround is: go kill it after every single scan by the KDE-Konsole-ringtone – use a kill-script instead the ringtone …

    or: tailoring some with kdedialog

    Is this still linux?

    No way!

    Thats b* sh*!


  7. Fco Manuel Fdez Trujillo says:

    Reading barcodes with a USB webcam on Linux?

    OK. Follow these steps…

    First, installed Zbar and a little program called “crikey”. The easy way. Open a terminal and type in:

    sudo apt-get install zbar-tools
    sudo apt-get install libx11-dev x11proto-xext-dev libxt-dev libxtst-dev
    tar -xzf crikey-0.8.3.tar.gz
    cd crikey-0.8.3
    sudo cp crikey /usr/local/bin

    Now hook up your USB webcam.

    Is your USB webcam supported by V4L1 (Video4Linux1)? Then, open again a terminal and type in:

    LD_PRELOAD=/usr/lib/libv4l/ zbarcam –raw –prescale=320×240 /dev/video1 | crikey -i

    Now open Google or a Gedit instance, clic inside the search box, and put a barcode in front of your USB webcam. Focus in the barcode, as necessary. You will get the barcode number where you have clicked on before…

    Is your USB webcam supported by V4L2 (Video4Linux2)? Then, open a terminal and type in:

    zbarcam –raw –prescale=320×240 /dev/video0 | crikey -i

    If the above commands don’t work, try “video1”, instead of “video0”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: