Created
September 10, 2013 03:16
-
-
Save booxood/6504557 to your computer and use it in GitHub Desktop.
二维码的编码和解码。采用了python的qrcode,zbar。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
#for encode: pip install qrcode | |
import qrcode | |
#for decode: pip install zbar | |
#如果报找不到zbar.h,则先安装libzbar-dev | |
import zbar | |
import Image | |
def encodeQR(data, qrPath='qr.png'): | |
# 简洁用法 | |
# img = qrcode.make(data) | |
# img.save(qrPath) | |
qr = qrcode.QRCode( | |
version=5, | |
error_correction=qrcode.constants.ERROR_CORRECT_L, | |
box_size=10, | |
border=4, | |
) | |
qr.add_data(data) | |
qr.make() | |
img = qr.make_image(fit=True) | |
img.save(qrPath) | |
def decodeQR(qrPath='qr.png'): | |
scanner = zbar.ImageScanner() | |
scanner.parse_config('enable') | |
pil = Image.open(qrPath).convert('L') | |
width, height = pil.size | |
raw = pil.tostring() | |
image = zbar.Image(width, height, 'Y800', raw) | |
scanner.scan(image) | |
data = {} | |
for symbol in image: | |
#print type(symbol.type) #<type 'zbar.EnumItem'> | |
data[str(symbol.type)] = symbol.data | |
del(image) | |
return data | |
encodeQR('hello') | |
print decodeQR() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment