Tesseract getBoxText() 在 > 5.10 中已弃用

问题描述 投票:0回答:1

由于

getBoxText()
不起作用,如何使用最新的 Tesseract / Tess4j 获取位于图片内的元素(文本)的矩形?

更新:

getBoxText()
在 v5.0 中已弃用,根据文档无法找到替代方案 文档

        String extractedText = tesseract.doOCR(screenShot);
        System.out.println("current snapshot from screen has the following test : " + extractedText);
        // Check if target string exists
        if (extractedText.contains(textElement)) {
            System.out.println("Targeted element by Text found ! ");

            BufferedImage bufferedImage = ImageIO.read(screenShot);
            Rectangle textRectangle = tesseract.getBoxText(textElement); // => here ****

            // Calculate midpoint coordinates
            int x = textRectangle.x + textRectangle.width / 2;
            int y = textRectangle.y + textRectangle.height / 2;
            System.out.println("clicked on element location (x,y) coordinate in snapshot is : + " + x + "," + y);
            Actions actions = new Actions(driver);
            actions.moveToLocation(x, y).click().perform();
        } else {
            System.out.println("Targeted element by Text Not found !");
        }
java tesseract tess4j
1个回答
0
投票

这是有效的代码,使用

getSegmentedRegions()
方法

    //take a Screenshot by emulator
    File screenShot = this.driver.getScreenshotAs(OutputType.FILE);
    BufferedImage bufferedImage = ImageIO.read(screenShot);
    //get all text from the image snapshot
    String extractedText = tesseract.doOCR(bufferedImage);
    System.out.println("current snapshot from screen has the following test : " + extractedText);

    // Check if target string exists if yes get it's coordinates
    if (extractedText.contains(textElement)) {
        System.out.println("Targeted element by Text found ! ");
        List<java.awt.Rectangle> textRectangles = tesseract.getSegmentedRegions(bufferedImage, 2);
        for (java.awt.Rectangle rectangle : textRectangles) {
            // Extract recognized text for this region
            String regionText = tesseract.doOCR(bufferedImage, rectangle);// Implement extraction logic
            if (regionText.contains(textElement)) {
                // Calculate midpoint coordinates
                int x = rectangle.x + rectangle.width / 2;
                int y = rectangle.y + rectangle.height / 2;
                Actions actions = new Actions(driver);
                actions.moveToLocation(x, y).click().perform();
                System.out.println("clicked on element location (x,y) coordinate in snapshot is : + " + x + "," + y);
                Thread.sleep(2000);
                break;
            }
        }

    } else {
        System.out.println("Targeted element by Text Not found !");
    }
© www.soinside.com 2019 - 2024. All rights reserved.