This article mainly introduces the zbar decoding QR code and bar code examples. For more information, see
The code is as follows:
#! /Usr/bin/env python
# Coding: u8
Import OS
Import zbar
Import Image
Import urllib
Import uuid
Def qrRead (url ):
Uuid1 = uuid. uuid1 ()
Filename = str (uuid1) + ". jpg"
Print uuid1
Urllib. urlretrieve (url, filename)
# Create a reader
Resize = zbar. imageresize ()
# Configure the reader
Parser. parse_config ('enable ')
# Obtain image data
Pil = Image. open (filename). convert ('L ')
Width, height = pil. size
# Pil. show ()
Raw = pil. tostring ()
# Wrap image data
Image = zbar. Image (width, height, 'y800', raw)
# Scan the image for barcodes
Scan. scan (image)
Tmpdata =''
# Extract results
For symbol in image:
# Do something useful with results
Print symbol. type, 'image content: \ n % s' % symbol. data
Tmpdata = tmpdata + symbol. data
# Clean up
Del (image)
OS. remove (filename)
Return tmpdata
If _ name _ = '_ main __':
Url = 'http: // www.jb51.net'
QrRead (url)
Install python-zbar
Check that the universe repository is enabled.
Check/etc/apt/sources. list and sudo to ensure that you have the right permissions to use your favorite editor.
The code is as follows:
Sudo gedit/etc/apt/sources. list
Ensure that universe is included.
After any changes, you should run this command to update your system.
The code is as follows:
Sudo apt-get update
You can install such a package now.
Install python-zbar
The code is as follows:
Sudo apt-get install python-zbar
This will install python-zbar and any other packages it depends on.