记录一次ocr识别

// log(clickCharEx_paddle("图片", true, 0, true));
function requestScreenshot() {
    let result = false;
    //等待截屏权限申请并同意
    let thread = threads.start(function () {
        if (device.brand == 'vivo') {
            text('取消').id('com.android.systemui:id/vbutton_title').waitFor();
            sleep(1000)
            let s = text('取消').findOnce()
            console.log(s.bounds().centerX(), s.bounds().centerY() - 180);
            click(s.bounds().centerX(), s.bounds().centerY() - 180)
        } else {
            packageName('com.android.systemui').text('立即开始').waitFor();
            text('立即开始').click();
        }
    });
    //申请截屏权限
    if (!requestScreenCapture()) {
        toast("请求截图失败");
    } else {
        result = true;
        thread.interrupt();
    }
    return result;
};
/**
OCR识图并点击(paddle)

*/
function clickCharEx_paddle(content, isDimFind, index, isClick, offsetX, offsetY, milliSecond) {

    if (!milliSecond) { milliSecond = 1000 };
    if (!index) { index = 0 };
    if (!offsetX) { offsetX = 0 };
    if (!offsetY) { offsetY = 0 };
    let result = false;
    requestScreenshot()
    let img = captureScreen();
    // console.log(img);
    if (img) {
        // console.log('开始识别');
        // let start = new Date();
        let res = paddle.ocr(img);
        // console.log(res);
        // log('OCR识别耗时:' + (new Date() - start) + 'ms');
        if (res && res.length > 0) {
            for (let i = 0; i < res.length; i++) {
                let ocrResult = res[i];
                log(ocrResult);

                if (isDimFind && ocrResult.words.indexOf(content) != -1) {
                    // log("模糊找到");
                    result = true;
                } else if (content === ocrResult.words) {
                    // log("精确找到");
                    result = true;
                } else {

                }

                if (result) {
                    if (isClick) {
                        click(ocrResult.bounds.left + parseInt((ocrResult.bounds.right - ocrResult.bounds.left) / 2) + offsetX, ocrResult.bounds.top + parseInt((ocrResult.bounds.bottom - ocrResult.bounds.top) / 2) + offsetY);
                    }

                    break;
                }
            }
        }

        img.recycle();  // 回收图片
    } else {
        log("截图失败");
    }

    if (result) {
        sleep(milliSecond);
    };
    return result;
};


你可能感兴趣的:(Autox.js,ocr)