How to extract text from image Android app

前端 未结 4 1861
眼角桃花
眼角桃花 2021-02-01 22:52

I am working on a feature for my Android app. I would like to read text from a picture then save that text in a database. Is using OCR the best way? Is there another way? Google

4条回答
  •  旧巷少年郎
    2021-02-01 23:41

    Text from an image can be extracted using Firebase machine learning (ML) kit. There are two versions of the text recognition API, on-device API (free) and on-cloud API.

    To use the API, first create BitMap of the image, which should be upright. Then create FirebaseVisionImage object passing the bitmap object.

    FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap);
    

    Then create FirebaseVisionTextRecognizer object.

    FirebaseVisionTextRecognizer textRecognizer = FirebaseVision.getInstance()
            .getCloudTextRecognizer();
    

    Then pass the FirebaseVisionImage object to processImage() method, add listeners to the resulting task and capture the extracted text in success callback method.

    textRecognizer.processImage(image)
                    .addOnSuccessListener(new OnSuccessListener() {
                        @Override
                        public void onSuccess(FirebaseVisionText firebaseVisionText) {
                           //process success
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                         @Override
                         public void onFailure(@NonNull Exception e) {
                           //process failure
                         }
                     });
    

    For complete example which shows how to use Firebase ML text recognizer, see https://www.zoftino.com/extracting-text-from-images-android

提交回复
热议问题