Search code examples
androidandroid-vision

Search for specific pattern in text using Android Mobile Vision API


I have made a working implementation of an app that uses the android mobile vision API to read text. But is there a way in which i can search for a specific pattern of text for example searching for where there are 10 digits in a row or something like that. Is it possible to implement this.

All help will be appreciated.


Solution

  • In the android mobile vision api there is a method called receiveDetections inside the OcrDetectorProcessor class.

    This method recieves all the characters which have been detected through the camera and it's default behaviour is to display each and every character detected onto the screen.

    @Override
        public void receiveDetections(Detector.Detections<TextBlock> detections) {
            mGraphicOverlay.clear();
            SparseArray<TextBlock> items = detections.getDetectedItems();
            for (int i = 0; i < items.size(); ++i) {
                TextBlock item = items.valueAt(i);
                OcrGraphic graphic = new OcrGraphic(mGraphicOverlay, item);
                mGraphicOverlay.add(graphic);
            }
        }
    

    You can edit this method to filter the detected characters and only display what you want to display to the user. So if say, according to your question, you wanted to display any string with 10 characters you can do that by editing the method to the following,

    @Override
        public void receiveDetections(Detector.Detections<TextBlock> detections) {
            if(stopScan){
                SparseArray<TextBlock> items = detections.getDetectedItems();
                for (int i = 0; i < items.size(); ++i) {
                    TextBlock item = items.valueAt(i);
    
            //verify string here
                    if (item.getValue().length() == 10) {
                        OcrGraphic graphic = new OcrGraphic(mGraphicOverlay, item);
                        mGraphicOverlay.add(graphic);
                    }
                }
            }
        }