android.gms.tasks.addOnSuccessListener无法解析

时间:2019-03-26 13:44:57

标签: android firebase firebase-mlkit

我正在使用firebase ml套件捕获图像并从该图像中识别文本。为此,我正在使用FirebaseVision,FirebaseVisionTextDectector和FirebaseVisionImage。 但是我之所以处于堆栈状态,是因为无法解析aaddOnSuccessListener和addOnFailureListener

我的build.gradle(app)文件:

apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
    applicationId "app.shakil.com.datafactor"
    minSdkVersion 19
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.+'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:27.+'
implementation 'com.google.firebase:firebase-ml-vision:15.0.0'
implementation 'com.google.firebase:firebase-core:15.0.2'

testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
apply plugin: 'com.google.gms.google-services'

我的文本识别器方法:

private void processTxt() {
    //This part will load the captured original image from the directory
    imageBitmap= BitmapFactory.decodeFile(photoFile.getAbsolutePath());
    imageViewForSavedImage.setImageBitmap(imageBitmap);
    firebaseVisionImage=FirebaseVisionImage.fromBitmap(imageBitmap);
    detector=FirebaseVision.getInstance().getVisionTextDetector();
    pixelAmount=new int[]{imageBitmap.getHeight()*imageBitmap.getWidth()};
    for(int i=0;i<pixelAmount.length;i++){

        int pixel = pixelAmount[i];
        if(pixel!=0){
            totalPixel=totalPixel+pixelAmount[i];
        }

    }
    Log.d("BITMAP","PIXEL:"+totalPixel);
    //Setting the image resolution
    imageResolution.setText("Width : "+imageBitmap.getWidth()+"\nHeight :"+imageBitmap.getHeight());
    numberOfPixels.setText("Number of pixels : "+totalPixel);
    detector.detectInImage(firebaseVisionImage).addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
        @Override
        public void onSuccess(FirebaseVisionText firebaseVisionText) {
            Toast.makeText(getApplicationContext(),"Task successful",Toast.LENGTH_LONG).show();
            processTxt(firebaseVisionText);
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(getApplicationContext(),"Task failed",Toast.LENGTH_LONG).show();
        }
    });
List<FirebaseVisionText.Block> blocks = text.getBlocks();
    if (blocks.size() == 0) {
        Toast.makeText(MainActivity.this, "No Text :(", Toast.LENGTH_LONG).show();
        return;
    }
    for (FirebaseVisionText.Block block : text.getBlocks()) {
        String txt = block.getText();
        recognizedTXT.setTextSize(24);
        recognizedTXT.setText(txt);
    }

}

0 个答案:

没有答案