python 二维码实现

来源:互联网 发布:腾讯云 centos 7 pptp 编辑:程序博客网 时间:2024/06/02 03:11

参考:http://www.cnblogs.com/linjiqin/p/4140455.html

   https://pypi.python.org/pypi/qrcode


########################################################################


使用python生成二维码图案。上网找了一下,发现已经有了一个模块qrcode,目前最新版本的是5.2.2

安装qrcode模块:

sudo pip install qrcode


如果想要查看源码的话,可以去官网上下载源码

命令行用法:

qr "Some text" > test.png

"Some text":表示想要设置在二维码上的内容,比如网址等

test.png:生成二维码图案的保存文件名,可以修改为其他的名字和格式,比如"te.jpg"




         

使用python代码:

import qrcodeimg = qrcode.make('Some data here')


更高级的用法:

控制生成二维码图案的参数,python代码如下:

import qrcodeqr = qrcode.QRCode(    version=1,    error_correction=qrcode.constants.ERROR_CORRECT_L,    box_size=10,    border=4,)qr.add_data('Some data')qr.make(fit=True)img = qr.make_image()

参数详解:

The version parameter is an integer from 1 to 40 that controls the size of the QR Code (the smallest, version 1, is a 21x21 matrix). Set to None and use thefit parameter when making the code to determine this automatically.

参数"version":是一个整数,取值范围为[1,40],用于控制二维码图案的大小。当version为1时最小,此时图案大小为21x21。不设置此参数或设为None,并且设置fit参数为True表示让程序自动调整二维码大小()


The error_correction parameter controls the error correction used for the QR Code. The following four constants are made available on theqrcode package:


ERROR_CORRECT_L
About 7% or less errors can be corrected.
ERROR_CORRECT_M (default)
About 15% or less errors can be corrected.
ERROR_CORRECT_Q
About 25% or less errors can be corrected.
ERROR_CORRECT_H.
About 30% or less errors can be corrected.

参数"error_correction":控制二维码误差校正的能力。在qrcode模块中设置了四种方式(用常数表示):

1.ERROR_CORRECT_L:小于等于7%的误差可以校正

2.ERROR_CORRECT_M(默认):小于等于15%的误差可以校正

3.ERROR_CORRECT_Q:小于等于25%的误差可以校正

4.ERROR_CORRECT_H:小于等于30%的误差可以校正


The box_size parameter controls how many pixels each “box” of the QR code is.

参数"box_size":控制二维码中每格所含像素数


The border parameter controls how many boxes thick the border should be (the default is 4, which is the minimum according to the specs).

参数"border":控制了白色边界的宽度(默认是4,也是最小值)


note模块qrcode默认保存图片格式为PIL(Python Imaging Library)格式


########################################################################


python实现:

从摄像头读取视频并显示,在显示图片右上角显示一个二维码图案:

#!/usr/bin/env python#-*- coding: utf-8 -*-__author__  = 'zj'import cv2.cv as cvimport cv2import timeimport qrcodeheight = 480width = 640data = "hello world"def make_qrcode(data):"""制作二维码图案input:data - 二维码要包含的信息outputqr_img - 生成的二维码图案"""qr = qrcode.QRCode(         version=1,         error_correction=qrcode.constants.ERROR_CORRECT_L,         box_size=10,         border=4, ) qr.add_data(data) qr.make(fit=True)  img = qr.make_image()img.save("qrcode.png")qr_img = cv2.imread("qrcode.png")return qr_imgdef merge_img(img1, img2):"""结合两幅图片,将img2图加在img1图上inputs:img1 - 大图,图像大小为widthxheightimg2 - 小图,图像大小固定为80x80output:img - 结合后的图,小图结合在大图的右上角"""wid = 80if not img2.shape == (wid,wid):res = cv2.resize(img2, (wid,wid), interpolation=cv2.INTER_CUBIC)else:res = img2img = img1img[:res.shape[0], -res.shape[1]:] = resreturn imgif __name__ == '__main__':qr_img = make_qrcode(data)#设置视频捕获cap = cv2.VideoCapture(0)while True:im = Nonewhile im == None:ret, im = cap.read()if im == None:print "im is None"height, width = im.shape[:2]img = merge_img(im, qr_img)cv2.imshow('video test', img)key = cv2.waitKey(10)if key == 27:breakif key == ord(' '):cv2.imwrite('res.jpg', img) print 'save image : res.jpg'

0 0
原创粉丝点击