Firebase Vision onSuccess()方法无法正确运行命令

时间:2019-08-14 20:20:39

标签: java android firebase opencv firebase-mlkit

我正在编写一个使用Firebase的FirebaseVisionTextRecognizer对象的程序。 OCR可以很好地工作,但是不能将任何检测到的字符串保存到我之前定义的全局ArrayList中。我在下面附加了我的代码;任何帮助将不胜感激!

        FirebaseVisionImage image;
        Bitmap bitmap;
        FirebaseVisionTextRecognizer detector = FirebaseVision.getInstance().getOnDeviceTextRecognizer();

        for(Mat mat : croppedTags) {
            bitmap = Bitmap.createBitmap(mat.width(), mat.height(), Bitmap.Config.ARGB_8888);
            Utils.matToBitmap(mat, bitmap);
            image = FirebaseVisionImage.fromBitmap(bitmap);

            Task<FirebaseVisionText> result = detector.processImage(image);

            result = detector.processImage(image)
                    .addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
                        @Override
                        public void onSuccess(FirebaseVisionText firebaseVisionText) {
                            String text = firebaseVisionText.getText();
                            text = text.toLowerCase();
                            text = text.replaceAll("\\s","");
                            text = text.replace('.', ' ');
                            text = text.replace(',', ' ');

                            if(text.length() > 8 && text.length() < 18) {
                                for(int i = 0; i < text.length(); i++) {
                                    char c = text.charAt(i);
                                    if(c != ' '&& c != 'a' && c != 'b' && c != 'c' && c != 'd' && c != 'e' && c != 'f' && c != 'g' && c != 'h' && c != 'i'
                                            && c != 'j' && c != 'k' && c != 'l' && c != 'm' && c != 'n' && c != 'o' && c != 'p' && c != 'q' && c != 'r' && c != 's'
                                            && c != 't' && c != 'u' && c != 'v' && c != 'w' && c != 'x' && c != 'y' && c != 'z' && c != '0' && c != '1' && c != '2'
                                            && c != '3' && c != '4' && c != '5' && c != '6' && c != '7' && c != '8' && c != '9' && c != '.' && c != ',' && c != '\n') {
                                        text = text.replace(c, ' ');
                                    }
                                    text = text.replaceAll(" ", "");
                                }
                                ocrRecognized.add(text);
                                Log.i("SUCCESS", text);
                            }
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Log.i("ERROR", "Something went wrong :(.");
                    });
        }

值得注意的是,它总是记录正确的值,但从未将文本添加到全局ArrayList“ ocrRecognized”。

0 个答案:

没有答案