1. Scene
- Dynamically generate the URL into a QR code front-end display (WeChat Payment, etc.,) -
1. Static file path access
Return URL_name, (a tag, src static route access)
2. Streaming, front-end rendering
The binary stream is returned to the front-end, which displays it according to the binary stream encoding type
3. Front-end js generation
The backend gets the code_url of the wechat payment,the frontend js generates a QR code from the code_url and renders it
- Actual code
Using the python_web framework --> tornado
import os import asyncio import import import import from import define, options, parse_command_line from apps import UrlHandler, Url2Handler, Url3Handler define("port", default=8000, type=int) def create_app(): settings = { "template_path": ((__file__), "templates"), "static_path": ((__file__), "static"), } application = ( handlers=[ (r"/url", UrlHandler), (r"/url2", Url2Handler), (r"/url3", Url3Handler), ], debug=True, **settings, ) return application if __name__ == '__main__': parse_command_line() app = create_app() server = (app) () asyncio.get_event_loop().run_forever()
import from manager_handler import gen_qrcode, gen_qrcode_obj,gen_qrcode_buf class BaseHandler(): pass class UrlHandler(BaseHandler): def get(self): # Get the link ('', title='url', data='URL-Submit', img_stream='') async def post(self): # Generate QR code url = self.get_argument('url_str') # URL to QR code conversion img_stream = gen_qrcode(url) await ('', title='qrcode', data='Sweeping code payment', img_stream=img_stream) class Url2Handler(BaseHandler): def get(self): # Get the link ('', title='url', data='URL-Submit', img_stream='') async def post(self): # Generate QR code url = self.get_argument('url_str') # URL to QR code conversion img_stream = gen_qrcode_obj(url=url) # await ('', title='qrcode', data='Sweep Payment', img_stream=img_stream) self.set_header('Content_Type', 'image/jpg') self.set_header('Content_length', len(img_stream)) (img_stream) class Url3Handler(BaseHandelr): def get(self): ('', title='url', data='URL-Submit', img_stream='') def post(self): url = self.get_argument('url') img_stream = gen_qrcode_buf(url) self.set_header('Content-Type', 'image/png') (img_stream)
manager_handler.py
import qrcode import io import base64 import time def gen_qrcode(url): """ Way 1: URL to convert QR code :param url: the URL to convert the QR code to :return: base64-encoded binary stream of QR code data """ qr = (url) buf = () (buf) img_buf = () img_stream = base64.b64encode(img_buf) return img_stream def gen_qrcode_obj(version=1, box_size=10, border=4, url=None): """ Way 2: URL to QR code conversion (image streaming, template needs to specify data:base64 encoding) :param version: :param box_size. :param box_size. :param border. :return: :param box_size: :param border. """ qr = ( version=version, error_correction=.ERROR_CORRECT_L, box_size=box_size, border=border, ) url = "https://" if url is None else url save_name = "./" + "qrcode" + str(()) + ".png" qr.add_data(url) () img = qr.make_image() (save_name.encode()) with open(save_name, 'rb') as img_f: img_stream = img_f.read() img_stream = base64.b64encode(img_stream) print(img_stream) return img_stream def gen_qrcode_buf(words): qr = (words) buf = () (buf, 'png') qr_buf = () # img_stream = base64.b64encode(qr_buf) return qr_buf
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{% block title %}{% end %}</title> {% block head %}{% end %} </head> <body> <h1 style="text-align: center"> {% block h1 %}{{ data }}{% end %} </h1> {% block content %}{% end %} </body> </html>
{% extends "" %} {% block title %} {{ title }} {% end %} {% block h1 %} {{ data }} {% end %} {% block content %} <form method="post" action="" > <p> Input the value to be convertedURL:<input name="url_str"/> <br> {# {{ img_stream }}#} {% if img_stream %} <img style="width:180px" src="data:;base64,{{ img_stream }}" alt=""> {% end %} </p> <br> <input type="submit" value="Generate QR code"> </form> {% end %}
The above is python-image streaming transfer ideas and examples (url conversion QR code) of the details, more information about python image streaming transfer please pay attention to my other related articles!