就像标题所述,我想循环Firebase ML识别文本调用,直到返回正确的文本。但是,我的问题是Firebase ML Recognize Text调用返回的诺言在循环再次开始之前没有得到解决。我已经尝试了所有方法来阻止呼叫,甚至实现延迟功能,但无济于事。
编辑:我通过以下代码使其“正常工作”。
fun test() {
OpenSpotify().waitFor()
firebaseReadText() // THE PROMISE IS IN HERE
Thread.sleep(1)
Timer().schedule(2) {
if(text != "Example Text") {
test()
}
}
}
原始示例:
// Fetching media
val projection = arrayOf(
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA,
MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
MediaStore.Images.ImageColumns.DATE_TAKEN,
MediaStore.Images.ImageColumns.MIME_TYPE
)
// Cursor for selecting photo
val cursor = applicationContext.contentResolver
.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
null, null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC"
)
// Function for opening an app
val OpenSpotify: () -> Process = {
Runtime.getRuntime().exec(
arrayOf(
"su", "-c", "" +
"monkey -p com.spotify.music -c android.intent.category.LAUNCHER 1" +
"screencap -p /sdcard/spotify.png"
)
)
}
// While loop
var text = ""
while (text != "Example Text?") {
OpenSpotify().waitFor()
if (cursor!!.moveToNext()) {
val imageLocation = cursor.getString(1)
val imageFile = File(imageLocation)
if (imageFile.exists()) {
val bm = BitmapFactory.decodeFile(imageLocation)
val image = FirebaseVisionImage.fromBitmap(bm)
val detector = FirebaseVision.getInstance().getOnDeviceTextRecognizer()
detector.processImage(image)
.addOnSuccessListener { firebaseVisionText ->
Log.i("test", "test")
text = "Example Text?"
}
.addOnFailureListener {
Log.i("test", "Error with Firebase!")
}
}
}
}