【懒人精灵】小飞侠插件专题——生成和识别二维码内容

可以识别图片中的二维码,或生成二维码图片。

识别前,请检查文件是否存在,不存在的文件会识别失败。

local xfxModule = require('lib/XfxPlugin')
local xfx = xfxModule:new({apkName = 'xfxPlugin-release.apk'})

local QrCodeOps = xfx:getOps('qrCodeOps')

if not QrCodeOps then
    print('无法获取 QrCodeOps 对象')
    return
end

-- ============================================
-- 生成二维码示例
-- ============================================

-- 1. 生成二维码(自定义尺寸和颜色)
local success2 =
    QrCodeOps.encodeToFile(
    'Hello, World!', -- 内容
    '/sdcard/qrcode_custom.png', -- 输出路径
    500, -- 宽度(像素)
    500, -- 高度(像素)
    2, -- 边距(模块数)
    'H', -- 错误纠正级别:L/M/Q/H(H为最高)
    0xFF000000, -- 前景色(黑色,格式:0xRRGGBB)
    0xFFFFFFFF, -- 背景色(白色)
    'PNG', -- 格式:PNG/JPEG/WEBP
    100 -- 质量(仅对JPEG有效,0-100)
)
if success2 then
    print('自定义二维码生成成功:/sdcard/qrcode_custom.png')
end

-- 2. 生成彩色二维码
local success3 =
    QrCodeOps.encodeToFile(
    '彩色二维码示例',
    '/sdcard/qrcode_color.png',
    400,
    400,
    1,
    'M',
    0xFF0066FF, -- 蓝色前景
    0xFFFFE6E6, -- 浅红色背景
    'PNG',
    100
)
if success3 then
    print('彩色二维码生成成功:/sdcard/qrcode_color.png')
end

-- 3. 生成二维码 Bitmap(用于进一步处理)
local bitmap =
    QrCodeOps.encodeToBitmap(
    'Bitmap 二维码示例',
    300,
    300,
    2, -- 边距(模块数)
    'H', -- 错误纠正级别:L/M/Q/H(H为最高)
    0xFF000000, -- 前景色(黑色,格式:0xRRGGBB)
    0xFFFFFFFF -- 背景色(白色)
)
if bitmap then
    print('二维码 Bitmap 生成成功')
    -- 可以进一步使用 ImageOps 处理这个 Bitmap
    local ImageOps = xfx:getOps('imageOps')
    if ImageOps then
        ImageOps.saveAsPng(bitmap, '/sdcard/qrcode_from_bitmap.png', true)
        print('从 Bitmap 保存的二维码:/sdcard/qrcode_from_bitmap.png')
    end
else
    print('二维码 Bitmap 生成失败')
end

-- 5. 生成不同错误纠正级别的二维码
local ecLevels = {'L', 'M', 'Q', 'H'}
for i, level in ipairs(ecLevels) do
    local path = '/sdcard/qrcode_ec_' .. level .. '.png'
    local success =
        QrCodeOps.encodeToFile(
        '错误纠正级别:' .. level,
        path,
        300,
        300,
        1,
        level,
        0xFF000000, -- 前景色(黑色,格式:0xRRGGBB)
        0xFFFFFFFF, -- 背景色(白色)
        'PNG', -- 格式:PNG/JPEG/WEBP
        100 -- 质量(仅对JPEG有效,0-100)
    )
    if success then
        print('错误纠正级别 ' .. level .. ' 的二维码已生成:' .. path)
    end
end

-- ============================================
-- 识别二维码示例
-- ============================================

print(fileExist('/sdcard/qrcode_custom.png'))

-- 从图片文件识别二维码
local result = QrCodeOps.decodeFromFile('/sdcard/qrcode_custom.png')
if result then
    print('识别结果:' .. result)
else
    print('识别失败')
end

-- 获取详细信息(JSON格式)
local info = QrCodeOps.decodeFromFileWithInfo('/sdcard/qrcode_custom.png')
if info then
    print('详细信息:' .. info)
end

接口

识别接口

  • decodeFromFile(imagePath: String?): 从图片文件路径识别二维码,返回识别内容(失败返回 null)
  • decodeFromBitmap(bitmap: Bitmap?): 从 Bitmap 对象识别二维码,返回识别内容
  • decodeFromFileWithInfo(imagePath: String?): 从文件路径识别二维码,返回 JSON 格式的详细信息(包含文本、格式等)
  • decodeFromBitmapWithInfo(bitmap: Bitmap?): 从 Bitmap 识别二维码,返回 JSON 格式的详细信息

生成接口

  • encodeToBitmap(content: String?, width: Int = 300, height: Int = 300, margin: Int = 1, errorCorrectionLevel: String = "M", foregroundColor: Int = Color.BLACK, backgroundColor: Int = Color.WHITE): 生成二维码 Bitmap

  • encodeToFile(content: String?, outputPath: String?, width: Int = 300, height: Int = 300, margin: Int = 1, errorCorrectionLevel: String = "M", foregroundColor: Int = Color.BLACK, backgroundColor: Int = Color.WHITE, format: String = "PNG", quality: Int = 90): 生成二维码并保存到文件

1. 官方交流QQ群,添加多个不批。建议使用安卓手机或电脑申请。
飞云脚本圈: 586333520飞云脚本圈
Auto.js学习交流③群:286635606
Auto.js学习交流②群:712194666(满员)
IOS免越狱自动化测试群:691997586
2. 盗版,破解有损他人权益和违法作为,请各位会员支持正版。
3. 本站部分资源来源于用户上传和网络搜集,如有侵权请提供版权证明并联系站长删除。
4.如未特别申明,本站的技术性文章均为原创,未经授权,禁止转载/搬运等侵权行为。
5.全站所有付费服务均为虚拟商品,购买后自动发货。售出后概不接受任何理由的退、换。注册即为接受此条款。
6.如果站内内容侵犯了您的权益,请联系站长删除。
飞云脚本 » 【懒人精灵】小飞侠插件专题——生成和识别二维码内容

企业级大数据智能营销管理系统

源码转让