Android Task<FirebaseVisionText> not returning FirebaseVisionText type

112 views Asked by At

I'm trying to pull the text and blocks from my image processed through a FirebaseVisionTextRecognizer, and even though the processing is successful, it doesn't seem to return as the correct type. Android studio reports that result is of type Task<FirebaseVisionText> instead of type FirebaseVisionText, as the documentation suggests it should.

Here is the problem area: '''FirebaseVisionImage fImg = FirebaseVisionImage.fromBitmap(curBitmap); FirebaseVisionTextRecognizer tscan = FirebaseVision.getInstance().getOnDeviceTextRecognizer(); System.out.println("before task");

    Task<FirebaseVisionText> result =
            tscan.processImage(fImg)
                    .addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
                        @Override
                        public void onSuccess(FirebaseVisionText firebaseVisionText) {
                            System.out.println("task success");
                            // Task completed successfully
                            // ...
                        }
                    })
                    .addOnFailureListener(
                            new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    System.out.println("task fail");
                                    // Task failed with an exception
                                    // ...
                                }
                            });
    String str = result.getText();'''

The line: String str = result.getText(); reports the error: Cannot resolve method 'getText' in 'Task'

I tried casting result to FirebaseVisionText as well as setting a new FirebaseVisionText equal to result, but neither solved the problem.

1

There are 1 answers

0
Seth Frolichman On

I fixed the issue by replacing String str = result.getText(); with Tasks.await(result); return result.getResult(); and calling the containing function on a new thread: new Thread(new Runnable() { public void run() { try{ containingFunction();} catch {}}}).start();