Function does not work in MainActivity, but works in TextRecognizerActivity

21 views Asked by At

I have a recogniseText() function which does text recognition using google ML Kit, placed in TextRecognizerActivity. Initially, I received an image from gallery in MainActivity.kt, and passed it with an intent to TextRecognizerActivity. Everything was working fine.

Then I deleted TextRecognizerActivity and placed the recogniseText() function in MainActivity itself. Now it always executes the addOnFailureListener block. The only thing that changed was the context that was passed to it. Before, the context was this@TextRecognizerActivity, and now the context is this@MainActivity.

public fun recognizeText(uri:Uri, context: Context): ArrayList<String>{
        var itemsPurchased = ArrayList<String>()

        val recognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS)
        val image = InputImage.fromFilePath(context,uri);
        val result = recognizer.process(image)
            .addOnSuccessListener { visionText ->
                itemsPurchased = receiptProcessor(BasicReceipt(),visionText)
            }
            .addOnFailureListener { e ->

            }
        return itemsPurchased
    }

I debugged it. the image variable is populated, the uri is also populated correctly. The variable itemsPurchased is returned as an empty ArrayList.

0

There are 0 answers