簡體   English   中英

等待兩個異步Firebase ml視覺方法結果在第三種方法中使用它們的最佳方法

[英]Best way to wait for two async firebase ml vision method results to use them in a third method

使用Firebase ml vision識別圖像中的QR碼和/或文本時。 等待異步文本和條形碼檢測器方法解決的最佳方法是什么,因此我可以在第三種方法中利用它們的結果。

我知道我可以從每個異步方法的“成功”偵聽器中調用替代方法來設置“ hasReturned”變量,並且僅在兩個方法都返回后才繼續進行,但是我正在尋找實現此目的的正確方法。

private void firebaseRecognitionFromImage(FirebaseVisionImage image) {

    //detect qr code
    FirebaseVisionBarcodeDetectorOptions options = new FirebaseVisionBarcodeDetectorOptions.Builder().setBarcodeFormats(FirebaseVisionBarcode.FORMAT_ALL_FORMATS).build();
    FirebaseVisionBarcodeDetector qrDetector = FirebaseVision.getInstance().getVisionBarcodeDetector(options);
    qrDetector.detectInImage(image).addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionBarcode>>() {
        @Override
        public void onSuccess(List<FirebaseVisionBarcode> barcodes) { /* RESULT 1 */ }
    });

    //detect text
    FirebaseVisionTextRecognizer textDetector = FirebaseVision.getInstance().getOnDeviceTextRecognizer();
    textDetector.processImage(image).addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
        @Override
        public void onSuccess(FirebaseVisionText texts) { /* RESULT 2 */ }
    });

    //process qr code and text information or lack thereof
    thirdMedthod("RESULT 1", "RESULT 2");
}

您應該使用Tasks.whenAll()的變體之一。 它將創建一個新的Task對象,該對象將在其他任務完成時完成。

Task<List<FirebaseVisionBarcode>> t1 = qrDetector.detectInImage(image);
Task<FirebaseVisionText> t2 = textDetector.processImage(image);
Tasks.whenAll(t1, t2).addOnSuccessListener(new OnSuccessListener<Void>() {
    // check the results of t1 and t2
});

此博客系列中了解有關Play服務任務API的更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM