Dor
Dor

Reputation: 1

run google mlkit text recognition synchronously

I'm using google's mlkit in order to do text recognition, i need this to be sync and not async i tried adding the following, without any successes, any suggestions?

Thread thread = new Thread() {
    @Override
    public void run() {
        try {
            Tasks.await(result);
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
};
    InputImage image = InputImage.fromBitmap(processedImageOfROI, 0);
    TextRecognizer recognizer = TextRecognition.getClient();
    Task<Text> result =
            recognizer.process(image)
                    .addOnSuccessListener(new OnSuccessListener<Text>() {
                        @Override
                        public void onSuccess(Text visionText) {
                            handleTextRecognizerResult(visionText);
                            Log.e("Text recognition", "Successfully to detected text");
                        }
                    })
                    .addOnFailureListener(
                            new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    Log.e("Text recognition", "Failed to detect text");
                                }
                            });

Upvotes: 0

Views: 1236

Answers (1)

Shiyu
Shiyu

Reputation: 935

You can call Tasks.await(result) right after the recognizer.process() call.

Upvotes: 2

Related Questions