懒人精灵写法

import('java.io.File')
import('java.lang.*')
import('java.util.Arrays')
import('android.content.Context')
import('android.hardware.Sensor')
import('android.hardware.SensorEvent')
import('android.hardware.SensorEventListener')
import('android.hardware.SensorManager')
import('com.nx.assist.lua.LuaEngine')
local w,h
w,h = getDisplaySize()
print(w,h) 
 
local loader = LuaEngine.loadApk("111222.apk")

local OCR = loader.loadClass("com.tomato.ocr.lr.OCRApi")
local tmo_ocr = OCR.init(LuaEngine.getContext())

local license = "TF70TX9SQEDJXUISSRNOPT3RPZJRDQKZ|K55DSIM3E82ZRKEKMDEGLF";
local flag = tmo_ocr.setLicense(license);
print(flag)
tmo_ocr.setRecType("ch-3.0")
tmo_ocr.setDetBoxType("rect")
tmo_ocr.setDetUnclipRatio(1.9)
tmo_ocr.setRecScoreThreshold(0.3)
tmo_ocr.setReturnType("json")
tmo_ocr.setBinaryThresh(0)

local w,h
w,h = getDisplaySize()
if w~=540 or h~=960 then
    print('分辨率540*960 才可以运行')
    stop()
end
 

function click_node()
    local sel = index(0):text(""):desc("文件夹:系统应用"):visibleToUser(true)
    local node = sel:findOne(10000)
    if node ~= nil then
        print(node:click())
    end

end
function find_text_click(x1, y1, x2, y2, wz,count)
    count = count or 0  -- 如果count未传入,则设置为0
    local type = 3;

    local bitmap = LuaEngine.snapShot(x1, y1, x2, y2)
    local result = tmo_ocr.ocrBitmap(bitmap, type)
    bitmap.recycle()

    local point = tmo_ocr.findTapPoint(wz)
    if point ~= "" then
        local json_point = jsonLib.decode(point)
        local center_x = json_point[1] + x1
        local center_y = json_point[2] + y1
        printEx('找到了' .. wz .. ': 坐标' .. center_x .. '  ' .. center_y .. ' 开始点击');
        tap(center_x, center_y)

    else

        printEx('未找到文字' .. wz)
        count = count + 1;
        sleep(2000)
        print('延迟2秒后继续找' .. count)
        find_text_click(x1, y1, x2, y2, wz,count)
    end

end

function recognize_text(x1, y1, x2, y2)
    local type = 3;

    local bitmap = LuaEngine.snapShot(x1, y1, x2, y2)
    local result = tmo_ocr.ocrBitmap(bitmap, type)
    bitmap.recycle()

    if result ~= "" then
        local json_result = jsonLib.decode(result) -- setReturnType为“json”时,返回的是json字符串,用jsonLib.decode解析
        for i, data in ipairs(json_result) do
            local location = data.location
            local score = data.score
            local words = data.words
            printEx('找到文字' .. words);
            return words;

        end

    else
        printEx('识别不了文字');
        return 0;
    end

end

function stop()
    exitScript()
end
function begintouch()
    print('开始滑动');
    touchDown(1, 297, 327)
    sleep(50)
    touchMoveEx(1, 297, 127, 2000)
    touchUp(1)

end
-- local ret, code = httpPost("https://wxserver.zhihuxuetang.cn/home/moban/testajax", "arg=123")
-- print(ret)

local function start()
    find_text_click(0, 0, 208, 397, '系统应用')

    text = recognize_text(79, 341, 209, 375)
    print(text);
end

return {
    start = start
}

 

posted @ 2024-11-02 20:34  newmiracle宇宙  阅读(5)  评论(0编辑  收藏  举报