def get_qr_image(session: CashdeskSession) -> TemporaryFile:
# TODO: check qr code
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
tz = timezone.get_current_timezone()
data = '{end}\tEinnahme\t{total}\tKassensession\t#{pk}\t{supervisor}\t{user}'.format(
end=session.end.astimezone(tz).strftime('%d.%m.%Y\t%H:%M:%S'),
total='{0:,.2f}'.format(session.get_cash_transaction_total()).translate(str.maketrans(',.', '.,')),
pk=session.pk,
supervisor=session.backoffice_user_after.get_full_name(),
user=session.user.get_full_name(),
)
qr.add_data(data)
qr.make()
f = TemporaryFile()
img = qr.make_image()
img.save(f)
return f
python类QRCode()的实例源码
def _getqrtag(self, text, rect):
qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L)
qr.add_data(text)
qr.make(fit=True)
img = qr.make_image()
bio = io.BytesIO()
img.save(bio)
pngqr = bio.getvalue()
base64qr = base64.b64encode(pngqr)
#<image x="110" y="20" width="280px" height="160px" xlink:href="data:image/png;base64,……"/>
imagetag = ElementTree.Element("image")
imagetag.set("xlink:href", "data:image/png;base64,"+base64qr.decode("ascii"))
imagetag.set("x", str(rect.x))
imagetag.set("y", str(rect.y))
imagetag.set("width", str(rect.w))
imagetag.set("height", str(rect.h))
return imagetag
def get_qrcode_by_text(text):
"""
?????????????????
:param text:
:return:
"""
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_Q,
box_size=STEP,
border=0,
)
qr.add_data(text)
qr.make(fit=True)
img = qr.make_image()
return _get_ascii(img)
def gen_qrcode():
url = raw_input(''+T+'' + color.UNDERLINE + 'Website or text>' + color.END)
print ""+C+"Enter the name of the output file without the extension"
name = raw_input(''+T+'' + color.UNDERLINE + 'Output>' + color.END)
qr = qrcode.QRCode(5, error_correction=qrcode.constants.ERROR_CORRECT_L)
qr.add_data(url)
qr.make()
im = qr.make_image()
time.sleep(1)
qr_img_path = os.path.join(name + ".png")
if os.path.isfile(qr_img_path):
os.remove(qr_img_path)
# save the image out
im.save(qr_img_path, format='png')
# print that its been successful
print(""+G+"[!] " + color.UNDERLINE + "QRCode has been generated!" + color.END)
def _showQRCodeImg(self, str):
if self.commandLineQRCode:
qrCode = QRCode('https://login.weixin.qq.com/l/' + self.uuid)
self._showCommandLineQRCode(qrCode.text(1))
else:
url = 'https://login.weixin.qq.com/qrcode/' + self.uuid
params = {
't': 'webwx',
'_': int(time.time())
}
data = self._post(url, params, False)
if data == '':
return
QRCODE_PATH = self._saveFile('qrcode.jpg', data, '_showQRCodeImg')
if str == 'win':
os.startfile(QRCODE_PATH)
elif str == 'macos':
subprocess.call(["open", QRCODE_PATH])
else:
return
def insertQR(image, data):
'''
@params:
image is the image that we'll be messing with
data is what will be encoded in the QR code
inserts a QR code into the image at the specified bounds
the new qr code should fit the bounds and seem natural (like it was the original imge)
'''
qr_gen = qrcode.QRCode(
version = None,
box_size = 4,
border = 0
);
qr_gen.add_data(data)
qr_gen.make(fit=True)
qrCode = qr_gen.make_image()
pgram = scanImage2(image)
#pgram = expandParallelogram(pgram, 15)
return warpImage(image, qrCode, pgram)
def qr_code(request, path):
data = (request.build_absolute_uri('/'+path) +
('?'+request.META['QUERY_STRING'] if request.META['QUERY_STRING'] else ''))
if len(data) > 256:
return HttpResponseBadRequest()
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=2,
)
qr.add_data(data)
qr.make(fit=True)
response = HttpResponse(content_type='image/png')
qr.make_image().save(response, 'PNG')
return response
def make_qr_code(message, error_correction=qrcode.constants.ERROR_CORRECT_M, box_size=10, border=4):
"""Return an in-memory SVG QR code containing the given message."""
# https://pypi.python.org/pypi/qrcode/5.3
# qrcode.constants.ERROR_CORRECT_M means about 15% or less errors can be corrected.
code = qrcode.QRCode(
version=1,
error_correction=error_correction,
box_size=box_size,
border=border,
)
code.add_data(message)
code.make(fit=True)
img = code.make_image(image_factory=SvgPathImage)
raw = BytesIO()
img.save(raw)
raw.flush()
return raw
def prn_qr(self, text, *args, **kwargs):
""" Print QR Code for the provided string """
qr_args = dict(
version=4,
box_size=5,
border=1,
error_correction=qrcode.ERROR_CORRECT_M
)
qr_args.update(kwargs)
qr_code = qrcode.QRCode(**qr_args)
qr_code.add_data(text)
qr_code.make(fit=True)
qr_img = qr_code.make_image()
im = qr_img._img.convert("RGB")
self._print_image(im,1,0)
def prn_qr(self, text, *args, **kwargs):
""" Print QR Code for the provided string """
qr_args = dict(
version=4,
box_size=4,
border=1,
error_correction=qrcode.ERROR_CORRECT_M
)
qr_args.update(kwargs)
qr_code = qrcode.QRCode(**qr_args)
qr_code.add_data(text)
qr_code.make(fit=True)
qr_img = qr_code.make_image()
im = qr_img._img.convert("RGB")
return self.print_image(im)
def qr(self, text,size, *args, **kwargs):
""" Print QR Code for the provided string """
qr_args = dict(
version=4,
box_size=size,
border=1,
error_correction=qrcode.ERROR_CORRECT_M
)
qr_args.update(kwargs)
qr_code = qrcode.QRCode(**qr_args)
qr_code.add_data(text)
qr_code.make(fit=True)
qr_img = qr_code.make_image()
im = qr_img._img.convert("RGB")
# Convert the RGB image in printable image
self._convert_image(im)
def profile_qrcode_img(req):
pprof, pp_created = models.PublicProfile.objects.get_or_create(
user=req.user
)
response = HttpResponse(content_type="image/png")
qr = qrcode.QRCode(
error_correction=qrcode.constants.ERROR_CORRECT_M,
border=0,
)
qr.add_data(profile_url(pprof, req))
qr.make(fit=True)
img = qr.make_image()
img.save(response, 'png')
return response
def generate_qr_code(url):
""" Generate a QR Code for the given URL and return an Image file"""
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(url)
qr.make(fit=True)
return qr.make_image()
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 svg_qrcode_path(data='QR Code Symbol'):
"""qrcode SVG path"""
qr = QRCode(error_correction=ERROR_CORRECT_M, box_size=10,
image_factory=SvgPathImage)
qr.add_data(data, optimize=False)
qr.make_image().save('out/qrcode_path_%s.svg' % data)
def svg_qrcode_rects(data='QR Code Symbol'):
"""qrcode SVG rects"""
qr = QRCode(error_correction=ERROR_CORRECT_M, box_size=10,
image_factory=SvgImage)
qr.add_data(data, optimize=False)
qr.make_image().save('out/qrcode_rects_%s.svg' % data)
def png_qrcode(data='QR Code Symbol'):
"""qrcode PNG"""
qr = QRCode(error_correction=ERROR_CORRECT_M, box_size=10)
qr.add_data(data, optimize=False)
qr.make_image().save('out/qrcode_%s.png' % data)
def test_basic(self):
qr = qrcode.QRCode(version=1)
qr.add_data('a')
qr.make(fit=False)
def test_large(self):
qr = qrcode.QRCode(version=27)
qr.add_data('a')
qr.make(fit=False)
def test_invalid_version(self):
qr = qrcode.QRCode(version=41)
self.assertRaises(ValueError, qr.make, fit=False)
def test_overflow(self):
qr = qrcode.QRCode(version=1)
qr.add_data('abcdefghijklmno')
self.assertRaises(DataOverflowError, qr.make, fit=False)
def test_add_qrdata(self):
qr = qrcode.QRCode(version=1)
data = QRData('a')
qr.add_data(data)
qr.make(fit=False)
def test_mode_number(self):
qr = qrcode.QRCode()
qr.add_data('1234567890123456789012345678901234', optimize=0)
qr.make()
self.assertEqual(qr.version, 1)
self.assertEqual(qr.data_list[0].mode, MODE_NUMBER)
def test_mode_alpha(self):
qr = qrcode.QRCode()
qr.add_data('ABCDEFGHIJ1234567890', optimize=0)
qr.make()
self.assertEqual(qr.version, 1)
self.assertEqual(qr.data_list[0].mode, MODE_ALPHA_NUM)
def test_regression_mode_comma(self):
qr = qrcode.QRCode()
qr.add_data(',', optimize=0)
qr.make()
self.assertEqual(qr.data_list[0].mode, MODE_8BIT_BYTE)
def test_mode_8bit(self):
qr = qrcode.QRCode()
qr.add_data(u'abcABC' + UNICODE_TEXT, optimize=0)
qr.make()
self.assertEqual(qr.version, 1)
self.assertEqual(qr.data_list[0].mode, MODE_8BIT_BYTE)
def test_mode_8bit_newline(self):
qr = qrcode.QRCode()
qr.add_data('ABCDEFGHIJ1234567890\n', optimize=0)
qr.make()
self.assertEqual(qr.data_list[0].mode, MODE_8BIT_BYTE)
def test_qrcode_bad_factory(self):
self.assertRaises(
TypeError, qrcode.QRCode, image_factory='not_BaseImage')
self.assertRaises(
AssertionError, qrcode.QRCode, image_factory=dict)
def test_qrcode_factory(self):
class MockFactory(BaseImage):
drawrect = mock.Mock()
qr = qrcode.QRCode(image_factory=MockFactory)
qr.add_data(UNICODE_TEXT)
qr.make_image()
self.assertTrue(MockFactory.drawrect.called)
def test_render_svg(self):
qr = qrcode.QRCode()
qr.add_data(UNICODE_TEXT)
img = qr.make_image(image_factory=qrcode.image.svg.SvgImage)
img.save(six.BytesIO())