使用ML Kit Firebase无法从文本识别中获取文本

时间:2019-11-29 20:55:11

标签: android bitmap firebase-mlkit

我在android应用程序中使用ML Kit文本识别。因此,用户使用“画图”在屏幕上写了一些内容,然后我保存了该图像并使用Firebase ML Kit文本识别对其进行了处理。但是,当用户使用PaintView类中的Paint.clear()来清除屏幕时,它会在屏幕上进行绘制或写入,因此ML Kit仍会识别所编写的预览文本。我需要在这里重置什么。我正在用文字说话来说书面语。

paintView = (PaintView) findViewById(R.id.paintView);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
paintView.init(metrics);

        speakTextButton = findViewById(R.id.button_speak);
        clearTextButton = findViewById(R.id.button_clear);

        Bitmap b = paintView.getDrawingCache();

        textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                textToSpeech.setLanguage(Locale.US);
            }
        });

        clearTextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                paintView.clear();
            }
        });

        speakTextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                paintView.setDrawingCacheEnabled(true); //saving image
                Bitmap bitmap = paintView.getDrawingCache();
                FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap);

                //create detector
                FirebaseVisionTextRecognizer detector = FirebaseVision.getInstance()
                        .getOnDeviceTextRecognizer();

                Task<FirebaseVisionText> result =
                        detector.processImage(image)
                                .addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
                                    @Override
                                    public void onSuccess(FirebaseVisionText firebaseVisionText) {
                                        extractText(firebaseVisionText);
                                    }
                                })
                                .addOnFailureListener(new OnFailureListener() {
                                    @Override
                                    public void onFailure(@NonNull Exception e) {
                                    }
                                });
            }
        });
    }

    public void extractText(FirebaseVisionText result) {
        resultText = result.getText();
        for (FirebaseVisionText.TextBlock block: result.getTextBlocks()) {
            for (FirebaseVisionText.Line line: block.getLines()) {
                if(line != null) {
                    Log.v("LINETEXT", line.getText());
                    Float lineConfidence = line.getConfidence();
                }
            }
        }
        startTextToSeech(resultText);

    }


    public void startTextToSeech(String resultText) {
        Log.v("QUOTE", resultText);
        textToSpeech.speak(resultText, TextToSpeech.QUEUE_FLUSH,null, null);
    }

1 个答案:

答案 0 :(得分:0)

语句“ StartTextToSpeech(resultText);”接近代码底部的输入错误。代替“ StartTextToSpeech”,而是“ StartTextToSeech”。

这可能会产生一些影响,并且由于它是一种启动方法,因此可能是整个问题。