How to read toast message in android

Sorry guys was not on this forum actively.

@Zarashima, @U.G.Chinthaka_Devind, here the sample code that works on basic OCR:

  1. First you make use Katalon API to capture some pictures/snapshots:
    SnapTotal = total snapshots
    ScrSnapDir = path to store snapshots as you would need to clear this prior snapshot, sorta like working directory for OCR.
    for (def index : (0…SnapTotal)) {
    String filename = Mobile.concatenate([
    ScrSnapDir,
    ‘\’,
    ‘file’,
    index,
    ‘.png’] as String[], FailureHandling.STOP_ON_FAILURE)
    Mobile.takeScreenshot(filename, FailureHandling.STOP_ON_FAILURE)
    }

  2. You need Tess4j (tess4j-4.0.1.jar, in my case) for the basic OCR as here I not involving yet OpenCV (see another topic created today). Here the basic codes:
    Tesseract instance = new Tesseract();
    instance.setDatapath(TessDataDir);
    instance.setLanguage(TessDataLang);

    Since Tess4j v4alpha, the whitelist seems broken; thus, left out from here.
    Basically variables, you can assign via Katalon Profiles:
    TessDataLang = ‘eng’
    TessDataDir = ‘D:\Dev\tools\tessdata.ocr’ <- this host the Eng data dictionary (eng.traineddata)
    Get files from: https://github.com/tesseract-ocr/tessdata

  3. Code to perform simple OCR, where imageFile is individual full-path-filename that you snapped earlier that you can perform a loop scan to stop when the TEXT you looking for found.
    try {
    String result = instance.doOCR(imageFile);
    }
    catch (TesseractException e) {
    return ‘Error while reading image’
    }

    Notes:

    1. result basically containing TEXT that from OCR.
    2. If no matched TEXT found after all snapped files processed and you sure one of the image is containing the TEXT that needs recognized; this you would need OpenCV to further enhanced the image for more ‘finer’ before passing to doOCR().
  4. Here the list of Java imports used and you need to place all the depending JAR files imported to Katalon Project or manual copy to Drivers folder (restart Katalon) you are sorted.
    import net.sourceforge.tess4j.Tesseract as Tesseract;
    import net.sourceforge.tess4j.TesseractException as TesseractException;

Let me know any further help needed.

Cheers.

2 Likes