python 二维码实现

参考: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 qrcode
img = qrcode.make('Some data here')


更高级的用法:

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

import qrcode
qr = 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 the fit 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 the qrcode 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 cv
import cv2
import time
import qrcode

height = 480
width = 640

data = "hello world"

def make_qrcode(data):
	"""	
	制作二维码图案

	input:
	data - 二维码要包含的信息

	output
	qr_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_img

def merge_img(img1, img2):
	"""
	结合两幅图片,将img2图加在img1图上
	
	inputs:
	img1 - 大图,图像大小为widthxheight
	img2 - 小图,图像大小固定为80x80
	
	output:
	img - 结合后的图,小图结合在大图的右上角
	"""
	wid = 80
	if not img2.shape == (wid,wid):
		res = cv2.resize(img2, (wid,wid), interpolation=cv2.INTER_CUBIC)
	else:
		res = img2

	img = img1
	img[:res.shape[0], -res.shape[1]:] = res	
	return img

if __name__ == '__main__':
	qr_img = make_qrcode(data)
	
	#设置视频捕获
	cap = cv2.VideoCapture(0)
	
	while True:
		im = None
		while 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:
			break
		if key == ord(' '):
			cv2.imwrite('res.jpg', img) 
			print 'save image : res.jpg'

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值