我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用qrcode.QRCode()。
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
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())
def test_render_svg_path(self): qr = qrcode.QRCode() qr.add_data(UNICODE_TEXT) img = qr.make_image(image_factory=qrcode.image.svg.SvgPathImage) img.save(six.BytesIO())
def test_render_svg_fragment(self): qr = qrcode.QRCode() qr.add_data(UNICODE_TEXT) img = qr.make_image(image_factory=qrcode.image.svg.SvgFragmentImage) img.save(six.BytesIO())
def test_render_pymaging_png(self): qr = qrcode.QRCode() qr.add_data(UNICODE_TEXT) img = qr.make_image(image_factory=qrcode.image.pure.PymagingImage) with warnings.catch_warnings(): if six.PY3: warnings.simplefilter('ignore', DeprecationWarning) img.save(six.BytesIO())
def test_render_pymaging_png_bad_kind(self): qr = qrcode.QRCode() qr.add_data(UNICODE_TEXT) img = qr.make_image(image_factory=qrcode.image.pure.PymagingImage) self.assertRaises(ValueError, img.save, six.BytesIO(), kind='FISH')
def test_optimize(self): qr = qrcode.QRCode() text = 'A1abc12345def1HELLOa' qr.add_data(text, optimize=4) qr.make() self.assertEqual(len(qr.data_list), 5) self.assertEqual(qr.data_list[0].mode, MODE_8BIT_BYTE) self.assertEqual(qr.data_list[1].mode, MODE_NUMBER) self.assertEqual(qr.data_list[2].mode, MODE_8BIT_BYTE) self.assertEqual(qr.data_list[3].mode, MODE_ALPHA_NUM) self.assertEqual(qr.data_list[4].mode, MODE_8BIT_BYTE) self.assertEqual(qr.version, 2)
def test_optimize_size(self): text = 'A1abc12345123451234512345def1HELLOHELLOHELLOHELLOa' * 5 qr = qrcode.QRCode() qr.add_data(text) qr.make() self.assertEqual(qr.version, 10) qr = qrcode.QRCode() qr.add_data(text, optimize=0) qr.make() self.assertEqual(qr.version, 11)
def test_print_ascii_stdout(self): qr = qrcode.QRCode() stdout_encoding = sys.stdout.encoding with mock.patch('sys.stdout') as fake_stdout: # Python 2.6 needs sys.stdout.encoding to be a real string. sys.stdout.encoding = stdout_encoding fake_stdout.isatty.return_value = None self.assertRaises(OSError, qr.print_ascii, tty=True) self.assertTrue(fake_stdout.isatty.called)
def test_print_tty_stdout(self): qr = qrcode.QRCode() with mock.patch('sys.stdout') as fake_stdout: fake_stdout.isatty.return_value = None self.assertRaises(OSError, qr.print_tty) self.assertTrue(fake_stdout.isatty.called)
def test_print_tty(self): qr = qrcode.QRCode() f = six.StringIO() f.isatty = lambda: True qr.print_tty(out=f) printed = f.getvalue() f.close() BOLD_WHITE_BG = '\x1b[1;47m' BLACK_BG = '\x1b[40m' WHITE_BLOCK = BOLD_WHITE_BG + ' ' + BLACK_BG EOL = '\x1b[0m\n' expected = ( BOLD_WHITE_BG + ' '*23 + EOL + WHITE_BLOCK + ' '*7 + WHITE_BLOCK) self.assertEqual(printed[:len(expected)], expected)
def test_get_matrix(self): qr = qrcode.QRCode(border=0) qr.add_data('1') self.assertEqual(qr.get_matrix(), qr.modules)
def test_get_matrix_border(self): qr = qrcode.QRCode(border=1) qr.add_data('1') matrix = [row[1:-1] for row in qr.get_matrix()[1:-1]] self.assertEqual(matrix, qr.modules)
def test_negative_size_at_construction(self): self.assertRaises(ValueError, qrcode.QRCode, box_size=-1)
def process_message(self, msg): command, text = self.parse_message(msg, full_text=True) if not text: await msg.answer('??????? ????? ??? ?????? ????? ????????????? QR ???') qr = qrcode.QRCode( error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4, ) qr.add_data(text) try: qr.make(fit=True) except DataOverflowError: return await msg.answer('??????? ??????? ?????????!') img = qr.make_image() buffer = io.BytesIO() img.save(buffer, format='png') buffer.seek(0) result = await upload_photo(self.api, buffer) return await msg.answer(f'??? QR ???, ? ???????: \n "{msg.text}"', attachment=str(result))
def get_qr_code(self): qr = qrcode.QRCode( version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=5, border=4, ) qr.add_data(self.secret) qr.make() f = TemporaryFile() img = qr.make_image() img.save(f) return f
def _str2qr(self, str): qr = qrcode.QRCode() qr.border = 1 qr.add_data(str) mat = qr.get_matrix() self._printQR(mat) # qr.print_tty() or qr.print_ascii()
def __init__(self, values = None, base_url = ""): self.values = values self.base_url = base_url self.images = [] self.qr = qrcode.QRCode( version = None, error_correction = qrcode.constants.ERROR_CORRECT_L, box_size = 5, border = 4) self.fnt = ImageFont.truetype(FONT_PATH, 16) self.qr.make(fit=True)
def _str2QRMat(self, str): qr = qrcode.QRCode() qr.border = 1 qr.add_data(str) mat = qr.get_matrix() return mat
def qr_terminal_str(str,version=1): qr = qrcode.QRCode(version) qr.add_data(str) qr.make() output = white_block*(qr.modules_count+2) + new_line for mn in qr.modules: output += white_block for m in mn: if m: output += black_block else: output += white_block output += white_block + new_line output += white_block*(qr.modules_count+2) + new_line return output
def gen_qr_code(text, border=4, pixel=15, raw_image=False, img_html=''): try: import qrcode except ImportError: if img_html: return 'Install <code>pillow/qrcode</code> packages for QR code' raise Exception('Please install pillow and qrcode packages, e.g., conda install pillow; pip install qrcode') qr = qrcode.QRCode( version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=pixel, border=border, ) qr.add_data(text) qr.make(fit=True) img = qr.make_image() img_io = io.BytesIO() img.save(img_io, "png") img_io.seek(0) img_data = img_io.getvalue() if raw_image: return img_data else: data_uri = "data:image/gif;base64,"+img_data.encode("base64") if img_html: return img_html % data_uri else: return data_uri