app.qrcode - 二维码 API

二维码生成与识别。

生成

app.qrcode.generate(text, opts?)

生成二维码图片。

参数:

  • text (string) - 要编码的文本
  • opts (table):
    • path (string, 必填) - 输出图片路径
    • size (number, 可选) - 图片尺寸,默认 256
    • correctionLevel (string, 可选) - 纠错级别: "L", "M"(默认), "Q", "H"
    • color (string, 可选) - 前景色,"#RRGGBB" 格式
    • backgroundColor (string, 可选) - 背景色,"#RRGGBB" 格式

返回值: boolean, error

-- 基础生成
app.qrcode.generate("https://example.com", {
    path = "/tmp/qr.png"
})

-- 自定义样式
app.qrcode.generate("https://example.com", {
    path = "/tmp/qr.png",
    size = 512,
    correctionLevel = "H",
    color = "#1A1A2E",
    backgroundColor = "#E0E0E0"
})

识别

app.qrcode.recognize(imagePath)

识别图片中的二维码。

参数:

  • imagePath (string) - 图片文件路径

返回值: array<table>, error - 每项包含:

  • text (string) - 解码的文本
  • type (string) - 类型("QRCode"
  • bounds (table) - 位置: {x, y, w, h}
local codes, err = app.qrcode.recognize("/path/to/image.png")
if codes then
    for _, code in ipairs(codes) do
        app.log.info("内容: " .. code.text)
    end
end

app.qrcode.recognizeFromScreen(opts?)

截取屏幕并识别二维码。

参数:

  • opts (table, 可选):
    • displayId (number) - 显示器 ID
    • rect (table) - 截取区域: {x, y, w, h}

返回值: array<table>, error - 同 recognize()

local codes = app.qrcode.recognizeFromScreen()
if codes and #codes > 0 then
    app.clipboard.setText(codes[1].text)
    app.notification.show("二维码", "已复制: " .. codes[1].text)
end

示例

为选中文件生成二维码

function MyPlugin:handleGenerateQR(context)
    local file = context.selectedFiles[1]
    local text = app.dialog.input("输入内容", app.path.basename(file))
    if not text then return end

    local qrPath = app.path.removeExtension(file) .. "_qr.png"
    local ok, err = app.qrcode.generate(text, {
        path = qrPath,
        size = 512,
        correctionLevel = "H"
    })

    if ok then
        app.notification.show("完成", "二维码已生成")
        app.finder.reveal(qrPath)
    else
        app.dialog.alert("错误", err or "生成失败")
    end
end

识别图片中的二维码

function MyPlugin:handleScanQR(context)
    local file = context.selectedFiles[1]
    local codes, err = app.qrcode.recognize(file)

    if not codes then
        app.dialog.alert("错误", err or "识别失败")
        return
    end

    if #codes == 0 then
        app.dialog.alert("结果", "未识别到二维码")
        return
    end

    local text = codes[1].text
    app.clipboard.setText(text)
    app.notification.show("二维码", "已复制: " .. text)
end
开发者文档
使用帮助
使用说明 脚本菜单 常见问题
脚本开发
开发指南
插件开发
快速开始 开发指南 示例插件
API 参考
概览 API 查询 插件信息 日志 Finder 上下文 插件设置 国际化
UI 与交互
对话框 进度条 系统通知 选择器 WebView 状态栏 Dock
文件与路径
文件操作 路径工具 Finder 操作 废纸篓 扩展属性 元数据 文件监听
数据格式
JSON Plist CSV XML PDF 图片
文本与编码
字符串 正则表达式 日期时间 颜色 加密编码
系统
Shell 命令 进程管理 应用管理 系统信息 AppleScript 快捷指令
系统信息
网络信息 电源/电池 屏幕/外观 音频控制 蓝牙设备 位置服务
网络
HTTP 请求 WebSocket URL 工具
输入与剪贴板
键盘模拟 鼠标模拟 全局热键 剪贴板 窗口管理
存储
SQLite Keychain UserDefaults
媒体
文字识别 二维码
工具
归档 类型标识 分享 定时器 防休眠 并发/协程