Search code examples
androidgoogle-mlkit

How can I get the ML Kit TextRecognizer text position on screen?


I am using ML Kit to manipulate text in real time. I want to show converted text in the same area as the input text on the screen (like google translate does).

I use this code, but I do not know how I can get the position on screen of the visionText.

            imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(this), new ImageAnalysis.Analyzer() {
                @Override
                public void analyze(@NonNull ImageProxy imageProxy) {
                    @SuppressLint("UnsafeOptInUsageError") InputImage imageProcess = InputImage.fromMediaImage(Objects.requireNonNull(imageProxy.getImage()), imageProxy.getImageInfo().getRotationDegrees());
                    TextRecognizer recognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS);
                    recognizer.process(imageProcess).addOnSuccessListener(new OnSuccessListener<Text>() {
                        @Override
                        public void onSuccess(@NonNull Text visionText) {
                            // get visionText position on screen
                            
                        }
                    }).addOnCompleteListener(new OnCompleteListener<Text>() {
                        @Override
                        public void onComplete(@NonNull Task<Text> task) {
                            imageProxy.close();
                        }
                    });

                }
            });

Solution

  • Check out the following reference page of the ML Kit platform: https://developers.google.com/android/reference/com/google/mlkit/vision/text/Text.Element

    You get the Point[] coordinates of an element with the getCornerPoints() method.