autojs之生成二维码

简介: 使用情景生成二维码

使用情景


生成二维码


效果展示

微信图片_20220624130601.jpg


原理


  1. python负责生成二维码
  2. autojs负责展示二维码


知识点


  1. python自定义模块的导入
  2. django 解析POST method传过来的数据
  3. python判断字符串中是否包含中文


代码讲解


python


  1. 添加路由
urlpatterns = [
    path('', csrf_exempt(views.mqrcode), name='qrcode'),
]


  1. 编写生成二维码的函数
def mqrcode(request):


  1. 解析POST method传过来的数据
# 获取content字段的值
content = request.POST.get("content")
# 获取背景图片
background_image = request.FILES.get('background_image')


  1. 如果有中文, 就提示禁止中文
def is_chinese(string):
    """
    检查整个字符串是否包含中文
    :param string: 需要检查的字符串
    :return: bool
    """
    for ch in string:
        if u'\u4e00' <= ch <= u'\u9fff':
            return True
    return False
HttpResponse("禁止中文")


  1. 保存图片和文字
# 保存图片
in_filepath = handle_uploaded_file(background_image)
# 保存文字
with open(father_path + '/res/' + 'content.txt', 'w') as f:
    f.write(content)


  1. 编写生成二维码的模块
def create(content, in_filepath):


  1. 导入二维码模块
BASE_DIR = os.path.abspath(os.path.dirname(
    os.path.dirname(os.path.abspath(__file__))) + '/qrcode')
sys.path.append(BASE_DIR)
import createqrcode


  1. 生成二维码
myqr.run(
    words=content,  # 二维码网址内容
    version=3,  # 设置容错率为最高
    level='H',  # 控制纠错水平,范围是L、M、Q、H,从左到右依次升高
    picture=in_filepath,  # 导入图片
    colorized=True,  # 生成彩图
    contrast=1.0,  # 用以调节图片的对比度,1.0 表示原始图片,更小的值表示更低对比度,更大反之。默认为1.0
    brightness=1.0,  # 用来调节图片的亮度,其余用法和取值同上
    save_name="demo.png",  # 保存文件的名字,格式可以是jpg,png,bmp,gif
    save_dir=father_path + "/res"  # 默认存储位置是当前目录
)


  1. 二维码返回给前端
def download_api(filepath):
    file = open(filepath, 'rb')
    response = HttpResponse(file)
    # 设置头信息,告诉浏览器这是个文件
    response['Content-Type'] = 'application/octet-stream'
    response['Content-Disposition'] = 'attachment;filename="cat.jpg"'
    return response


autojs


1.ui

"ui";
ui.layout(
  <vertical margin="20" gravity="center">
    <horizontal>
      <text>图片路径</text>
      <input id="imgPath" w="*">
        cat.jpg
      </input>
    </horizontal>
    <horizontal>
      <text>文字内容(禁止中文)</text>
      <input id="content" w="*">
        miao
      </input>
    </horizontal>
    <button id="btn" textSize="20sp">
      生成二维码
    </button>
    <text textSize="30sp" w="*" gravity="center">
      作者: 牙叔
    </text>
    <img id="img" w="300dp" h="300dp"></img>
  </vertical>
);


  1. 设置点击事件
ui.btn.click(function () {
  threads.start(function () {
    let url = "http://192.168.101.4:8000/qrcode/";
    var res = http.postMultipart(url, getData());
    let tempFilepath = "/sdcard/1.jpg";
    files.writeBytes(tempFilepath, res.body.bytes());
    ui.run(function () {
      ui.img.attr("src", "file://" + tempFilepath);
    });
    // app.viewFile(tempFilepath);
  });
});






相关文章
autojs普通版控制台美化
autojs普通版控制台美化
826 0
|
3月前
|
Python
生成二维码
使用Python生成二维码可借助`qrcode`库。安装库:`pip install qrcode[pil]`。创建二维码的步骤如下: ```python import qrcode
71 0
|
小程序
微信小程序生成二维码。把文字,链接,网址等生成二维码
微信小程序生成二维码。把文字,链接,网址等生成二维码
331 0
|
JSON 小程序 数据格式
微信小程序实现生成二维码功能并下载到本地
微信小程序实现生成二维码功能并下载到本地
417 0
微信小程序实现生成二维码功能并下载到本地
|
Android开发
autojs控制台美化
牙叔教程 简单易学 使用场景 自定义控制台
707 0
|
Android开发
autojs获取网页源代码
牙叔教程 简单易懂
1089 1
|
前端开发 数据安全/隐私保护 Android开发
autojs图片加水印
牙叔教程 简单易懂
218 0
|
人工智能 算法 数据挖掘
autojs生成色块风格头像
牙叔教程 简单易懂
245 0
|
XML JavaScript 数据格式
autojs之启动页
启动页概念 启动页包括开屏页和广告页, 桌面点击app, 你看见的第一个页面就是开屏页; 开屏页后面可以显示广告页, 也可以不显示 本节教程针对开屏页
621 0
autojs之启动页
|
Android开发
autojs一键换肤
牙叔教程 简单易懂
141 0