0

我试图从通过 FirebaseVisionTextRecognizer 处理的图像中提取文本和块,即使处理成功,它似乎也不会作为正确的类型返回。Android Studio 报告结果是 Task<FirebaseVisionText> 类型,而不是 FirebaseVisionText 类型,正如文档所建议的那样。

这是问题区域:'''FirebaseVisionImage fImg = FirebaseVisionImage.fromBitmap(curBitmap); FirebaseVisionTextRecognizer tscan = FirebaseVision.getInstance().getOnDeviceTextRecognizer(); System.out.println("任务前");

    Task<FirebaseVisionText> result =
            tscan.processImage(fImg)
                    .addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
                        @Override
                        public void onSuccess(FirebaseVisionText firebaseVisionText) {
                            System.out.println("task success");
                            // Task completed successfully
                            // ...
                        }
                    })
                    .addOnFailureListener(
                            new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    System.out.println("task fail");
                                    // Task failed with an exception
                                    // ...
                                }
                            });
    String str = result.getText();'''

行: String str = result.getText(); 报错:Cannot resolve method 'getText' in 'Task'

我尝试将结果转换为 FirebaseVisionText 并将新的 FirebaseVisionText 设置为等于结果,但都没有解决问题。

4

1 回答 1

0

我通过在新线程上替换并调用包含函数来解决此问题String str = result.getText();Tasks.await(result); return result.getResult();new Thread(new Runnable() { public void run() { try{ containingFunction();} catch {}}}).start();

于 2020-12-24T21:49:05.770 回答