2

我想创建一个类似 CamCard 的应用程序,它将作为 ocr 工作并将文本填充到特定字段中,我已经使用 Firebase MLKit 从图像中识别文本,但问题是它现在给我文本作为列表如何获取哪些文本与哪个相关我的基本实现如下

 FirebaseApp.initializeApp(this);
        FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bm);



FirebaseVisionTextRecognizer textRecognizer = FirebaseVision.getInstance()
                .getOnDeviceTextRecognizer();
        textRecognizer.processImage(image)
                .addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
                    @Override
                    public void onSuccess(FirebaseVisionText result) {
                        // Task completed successfully
                        // ...
                        String resultText = result.getText();
                        for (FirebaseVisionText.TextBlock block : result.getTextBlocks()) {
                            String blockText = block.getText();
                            Float blockConfidence = block.getConfidence();
                            List<RecognizedLanguage> blockLanguages = block.getRecognizedLanguages();
                            Point[] blockCornerPoints = block.getCornerPoints();
                            Rect blockFrame = block.getBoundingBox();
                            for (FirebaseVisionText.Line line : block.getLines()) {
                                String lineText = line.getText();
                                Float lineConfidence = line.getConfidence();
                                List<RecognizedLanguage> lineLanguages = line.getRecognizedLanguages();
                                Point[] lineCornerPoints = line.getCornerPoints();
                                Rect lineFrame = line.getBoundingBox();
                                for (FirebaseVisionText.Element element : line.getElements()) {
                                    String elementText = element.getText();
                                    Float elementConfidence = element.getConfidence();
                                    List<RecognizedLanguage> elementLanguages = element.getRecognizedLanguages();
                                    Point[] elementCornerPoints = element.getCornerPoints();
                                    Rect elementFrame = element.getBoundingBox();
                                }
                            }
                        }
                    }
                })
                .addOnFailureListener(
                        new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                // Task failed with an exception
                                // ...
                            }
                        });

从上面的侦听器中,我将获得所有文本的列表,请演示如何将文本设置为相应的字段

4

1 回答 1

1
private void runTextRecognition()
{

    // mSelectedImage <- Bitmap ( picture to find text )

    // convert bitmap in FirebaseVisionImage
    FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(mSelectedImage);
    FirebaseVisionTextRecognizer textRecognizer = FirebaseVision.getInstance()
            .getOnDeviceTextRecognizer();

    // disable btn search text in activity.
    button_search_text.setEnabled(false);

    textRecognizer.processImage(image)
            .addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
                @Override
                public void onSuccess(FirebaseVisionText result)
                {
                    // send FirebaseVisionText ( txt ) 
                    processTextRecognitionResult(result);
                }
            })
            .addOnFailureListener(
                    new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e)
                        {
                            button_search_text.setEnabled(true);
                            e.printStackTrace();
                        }
                    });
}

// processTextRecognitionResult -> parse the results 
private void processTextRecognitionResult(FirebaseVisionText texts)
{
    List<FirebaseVisionText.TextBlock> blocks = texts.getTextBlocks();

    // ERROR -> No text found
    if (blocks.size() == 0) {
        txt_descripcion.setText( getResources().getString( R.string.alert_no_text ) );
        return;
    }
    mGraphicOverlay.clear();
    for (int i = 0; i < blocks.size(); i++) {
        List<FirebaseVisionText.Line> lines = blocks.get(i).getLines();
        for (int j = 0; j < lines.size(); j++) {
            List<FirebaseVisionText.Element> elements = lines.get(j).getElements();
            for (int k = 0; k < elements.size(); k++) 
            {
                //display result in picture
                Graphic textGraphic = new TextGraphic(mGraphicOverlay, elements.get(k));
                mGraphicOverlay.add(textGraphic);

                // (string) To get text you can use -> elements.get(k).getText() 
                Log.d(TAG, elements.get(k).getText() );

            }
        }
    }

}

示例 [完整解释] [步骤 6]
https://codelabs.developers.google.com/codelabs/mlkit-android/#5

示例 [完整代码]
https://github.com/googlecodelabs/mlkit-android/blob/6da8e85b92ca85c60e1c15f74dd6a8fefb22836f/final/app/src/main/java/com/google/firebase/codelab/mlkit/MainActivity.java#L205

文档
https://firebase.google.com/docs/ml-kit/android/recognize-text#2-extract-text-from-blocks-of-recognized-text

于 2018-11-04T22:39:44.237 回答