def showQR(addr, errcorr):
# generate QR code and display on LED grid
code = pyqrcode.create(addr, error=errcorr, version=3)
t = code.text(1)
row = 31
col = 0
bufferInit()
for i in t:
if i != '\n':
bufferPixel(row, col, 255-int(i)*255, 255-int(i)*255, 255-int(i)*255)
col += 1
else:
row -= 1
col = 0
bufferDraw()
#time.sleep(0.001)
# give us a chance to scan it
time.sleep(QRTIME)
# draw blocks since last difficulty adjustment
python类create()的实例源码
def create_qrcode(data='QR Code Symbol'):
"""qrcode create"""
qr = QRCode(error_correction=ERROR_CORRECT_M)
qr.add_data(data, optimize=False)
qr.make()
def create_pyqrcode(data='QR Code Symbol'):
"""PyQRCode create"""
pyqrcode.create(data, error='m')
def svg_pyqrcode(data='QR Code Symbol'):
"""PyQRCode SVG"""
pyqrcode.create(data, error='m').svg('out/pyqrcode_%s.svg' % data, scale=10)
def png_pyqrcode(data='QR Code Symbol'):
"""PyQRCode PNG"""
pyqrcode.create(data, error='m').png('out/pyqrcode_%s.png' % data, scale=10)
def create_qrcodegen(data='QR Code Symbol'):
"""qrcodegen create"""
QrCode.encode_text(data, QrCode.Ecc.MEDIUM)
def twofactor_qrcode():
"""Return a QRcode of the TOTP URI for the current user"""
user = User.query.filter_by(id=current_user.id).first_or_404()
url = pyqrcode.create(user.get_totp_uri())
stream = BytesIO()
url.svg(stream, scale=5)
return stream.getvalue(), 200, {
'Content-Type': 'image/svg+xml',
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'
}
def gen_qr_code(self, qr_file_path):
string = 'https://login.weixin.qq.com/l/' + self.uuid
qr = pyqrcode.create(string)
if self.conf['qr'] == 'png':
qr.png(qr_file_path, scale=8)
show_image(qr_file_path)
# img = Image.open(qr_file_path)
# img.show()
elif self.conf['qr'] == 'tty':
print(qr.terminal(quiet_zone=1))
def gen_qr_code(self, qr_file_path):
string = 'https://login.weixin.qq.com/l/' + self.uuid
qr = pyqrcode.create(string)
if self.conf['qr'] == 'png':
qr.png(qr_file_path, scale=8)
show_image(qr_file_path)
# img = Image.open(qr_file_path)
# img.show()
elif self.conf['qr'] == 'tty':
qr.png(qr_file_path, scale=8)
print(qr.terminal(quiet_zone=1))
def gen_qr_code(self, qr_file_path):
string = 'https://login.weixin.qq.com/l/' + self.uuid
qr = pyqrcode.create(string)
if self.conf['qr'] == 'png':
qr.png(qr_file_path, scale=8)
show_image(qr_file_path)
# img = Image.open(qr_file_path)
# img.show()
elif self.conf['qr'] == 'tty':
print(qr.terminal(quiet_zone=1))
def qr_by_account(account):
account = 'xrb:{0}'.format(account)
path = '{1}{0}.png'.format(account, qr_folder_path)
if (not os.path.isfile(path)):
qr = pyqrcode.create(account, error='L', version=4, mode=None, encoding='iso-8859-1')
qr.png('{1}{0}.png'.format(account, qr_folder_path), scale=8)
def gen_qr_code(self, qr_file_path):
string = 'https://login.weixin.qq.com/l/' + self.uuid
qr = pyqrcode.create(string)
if self.conf['qr'] == 'png':
qr.png(qr_file_path, scale=5)
self.show_image(qr_file_path)
# img = Image.open(qr_file_path)
# img.show()
elif self.conf['qr'] == 'tty':
print(qr.terminal(quiet_zone=1))
def gen_qr_code(self, qr_file_path):
string = 'https://login.weixin.qq.com/l/' + self.uuid
qr = pyqrcode.create(string)
if self.conf['qr'] == 'png':
qr.png(qr_file_path, scale=8)
show_image(qr_file_path)
# img = Image.open(qr_file_path)
# img.show()
elif self.conf['qr'] == 'tty':
print(qr.terminal(quiet_zone=1))
def create_qr_code(data):
buffer = io.BytesIO()
qr_code = pyqrcode.create(data)
qr_code.svg(buffer, scale=10)
logging.info('QR code created')
return buffer
def run_backup(self, args):
"""Run the backup operation."""
chunksize = args.chunksize
encodefunc = base64.b85encode #FIXME: add arg
infile = open(args.infile, "rb")
infile_size = os.path.getsize(infile.name)
outfile = args.outfile
inputhash = hashlib.sha256()
framedata = self.frame_data_func(args)
qr_count = infile_size / chunksize + 1
self.logger.info('Original file size: %dKiB', infile_size / 1024)
self.logger.info('Total number of QR codes: %d', qr_count)
exporter = self.setup_exporter(args, qr_count)
qr_number = 0
sizesofar = 0
while True:
bindata = infile.read(chunksize)
if not bindata: break
frame = framedata(bindata, qr_count, sizesofar)
inputhash.update(bindata)
sizesofar += len(bindata)
qr_number += 1
self.logger.info('Exporting QR %d of %d', qr_number, qr_count)
encdata = encodefunc(frame).decode()
qr = pyqrcode.create(encdata)
exporter.add_qr(qr)
exporter.finish(inputhash)
self.logger.info('Finished exporting')
if args.sha256:
print('SHA-256 of input: %s' % inputhash.hexdigest())
def shorten_url(long_url):
url_data = parse.urlencode(dict(url=long_url))
byte_data = str.encode(url_data)
ret = request.urlopen("http://tinyurl.com/api-create.php", data=byte_data).read()
result = str(ret)[2:-1]
return result
def make_qrcode_image(url,path='',event=None):
try:
url = shorten_url(url)
if event:
ID = event
qr = pyqrcode.create(url,version=15)
else:
ID = hashlib.md5(url.encode('utf8')).hexdigest()
qr = pyqrcode.create(url,version=15)
file_name = os.path.join(path,'%s.png' % ID)
qr.png(file_name,scale=4)
return ID
except Exception as e:
print(str(e))
return -1
def gen_qr_code(self, qr_file_path):
string = 'https://login.weixin.qq.com/l/' + self.uuid
qr = pyqrcode.create(string)
if self.conf['qr'] == 'png':
qr.png(qr_file_path, scale=8)
show_image(qr_file_path)
# img = Image.open(qr_file_path)
# img.show()
elif self.conf['qr'] == 'tty':
print(qr.terminal(quiet_zone=1))
def gen_qr_code(self, qr_file_path):
string = 'https://login.weixin.qq.com/l/' + self.uuid
qr = pyqrcode.create(string)
if self.conf['qr'] == 'png':
qr.png(qr_file_path, scale=8)
show_image(qr_file_path)
# img = Image.open(qr_file_path)
# img.show()
elif self.conf['qr'] == 'tty':
print(qr.terminal(quiet_zone=4))
def _get_qr_code(**get_params):
data = get_params['url']
qr = pyqrcode.create('www.google.comwww.google.comwww.google.comUnladden swallow')
return dict(binary_array=qr.code)
def gen_qr_code(self, qr_file_path):
string = 'https://login.weixin.qq.com/l/' + self.uuid
qr = pyqrcode.create(string)
if self.conf['qr'] == 'png':
qr.png(qr_file_path, scale=8)
show_image(qr_file_path)
# img = Image.open(qr_file_path)
# img.show()
elif self.conf['qr'] == 'tty':
print(qr.terminal(quiet_zone=1))
def create_QR_image(text, scale):
full_QR_path = os.getcwd() + '/img/QRcode.png'
url = pyqrcode.create(text)
url.png(full_QR_path, scale)
print('Create QR Code succes in here: ' + full_QR_path)
print(url.text())
def returnQRCodeText(self, validURL):
objectQRCode = pyqrcode.create(validURL)
return objectQRCode.text()
def saveQRCodeSVG(self, validURL, filename, imageDirectory=None):
fullfilename = imageDirectory + filename if imageDirectory is not None else filename
objectQRCode = pyqrcode.create(validURL)
objectQRCode.svg(fullfilename + ".svg", scale=4)
# in-memory stream is also supported
buffer = io.BytesIO()
objectQRCode.svg(buffer)
# do whatever you want with buffer.getvalue()
print(list(buffer.getvalue()))
def saveQRCodePNG(self, validURL, filename, imageDirectory=None):
fullfilename = imageDirectory + filename if imageDirectory is not None else filename
objectQRCode = pyqrcode.create(validURL)
with open(fullfilename + ".png", 'wb') as fstream:
objectQRCode.png(fstream, scale=5)
# same as above
objectQRCode.png(fullfilename + ".png", scale=5)
# in-memory stream is also supported
buffer = io.BytesIO()
objectQRCode.png(buffer)
# do whatever you want with buffer.getvalue()
print(list(buffer.getvalue()))
def showQR():
global rpc_connection
print "Loading 2nd bitcoin address..."
# connect to node and get new wallet address
try:
addr = rpc_connection.getnewaddress()
except (socket.error, httplib.CannotSendRequest):
print "showQR Timeout"
initRPC()
return False
# bypass rpc for testing
#addr = '1CepbXDXPeJTsk9PUUKkXwfqcyDgmo1qoE'
# generate QR code and display on LED grid
code = pyqrcode.create(addr, error='M', version=3)
t = code.text(1)
print addr
# print the actual QR code to terminal with 1's and 0's
print t
row = 31
col = 0
matrix.Clear()
for i in t:
if i != '\n':
matrix.SetPixel(row, col, 255-int(i)*255, 255-int(i)*255, 255-int(i)*255)
col += 1
else:
row -= 1
col = 0
time.sleep(0.001)
# give us a chance to scan it
time.sleep(5)
return True
def getOtp( self ):
totp = session._tmp_otp
totpImg = None
if totp is not None:
totpImg = 'data:image/png;base64,%s' % pyqrcode.create( totp ).png_as_base64_str( scale = 4 )
return ( totp, totpImg )
def doPOST( self ):
params = web.input( oid = None, iid = None, tags = '', desc = '' )
try:
oid = uuid.UUID( params.oid )
except:
oid = None
try:
if params.iid is not None:
iid = uuid.UUID( params.iid )
else:
# If no IID is present it indicates to create a new one.
iid = None
except:
iid = None
if oid is None:
raise web.HTTPError( '400 Bad Request: oid required' )
if not isOrgAllowed( oid ):
raise web.HTTPError( '401 Unauthorized' )
tags = [ x.strip() for x in params.tags.split( ',' ) ]
resp = deployment.request( 'set_installer_info', { 'oid' : oid,
'iid' : iid,
'tags' : tags,
'desc' : params.desc } )
if resp.isSuccess:
redirectTo( 'manage' )
else:
raise web.HTTPError( '503 Service Unavailable: %s' % str( resp ) )
def _qrcode_from_signify_ed25519_pubkey(pubkey_file, mode='text'):
"""
Usage:
1. Get the OpenBSD 5.7 release public key from here
http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/etc/signify/Attic/openbsd-57-base.pub?rev=1.1
2. Generate QR Code and print to terminal
print(cryptosign._qrcode_from_signify_ed25519_pubkey('openbsd-57-base.pub'))
3. Compare to (scroll down) QR code here
https://www.openbsd.org/papers/bsdcan-signify.html
"""
assert(mode in ['text', 'svg'])
import pyqrcode
with open(pubkey_file) as f:
pubkey = f.read().splitlines()[1]
qr = pyqrcode.create(pubkey, error='L', mode='binary')
if mode == 'text':
return qr.terminal()
elif mode == 'svg':
import io
data_buffer = io.BytesIO()
qr.svg(data_buffer, omithw=True)
return data_buffer.getvalue()
else:
raise Exception('logic error')
def qrcode_from_totp(secret, label, issuer):
if type(secret) != six.text_type:
raise Exception('secret must be of type unicode, not {}'.format(type(secret)))
if type(label) != six.text_type:
raise Exception('label must be of type unicode, not {}'.format(type(label)))
try:
import pyqrcode
except ImportError:
raise Exception('pyqrcode not installed')
import io
buffer = io.BytesIO()
data = pyqrcode.create(u'otpauth://totp/{}?secret={}&issuer={}'.format(label, secret, issuer))
data.svg(buffer, omithw=True)
return buffer.getvalue()
#
# The following code is adapted from the pbkdf2_bin() function
# in here https://github.com/mitsuhiko/python-pbkdf2
# Copyright 2011 by Armin Ronacher. Licensed under BSD license.
# https://github.com/mitsuhiko/python-pbkdf2/blob/master/LICENSE
#