我有一个Android应用程序,正在使用文档文本识别API。这是基于云的API,为此我启用了Firebase Blaze计划-这是使用基于云的API所必需的。我具有文档中列出的所有先决条件,并且已在Firebase控制台中启用了Cloud API。
但是,我仍然遇到以下错误:
W/System.err: com.google.firebase.ml.common.FirebaseMLException: 400 Bad Request
{
"code": 400,
"errors": [
{
"domain": "global",
W/System.err: "message": "API key not valid. Please pass a valid API key.",
"reason": "badRequest"
}
],
"message": "API key not valid. Please pass a valid API key.",
"status": "INVALID_ARGUMENT"
}
我不明白为什么。我下载了正确的google-services.json
文件。我交叉检查了所有依赖项,并在项目上启用了Blaze计划。为什么我的应用程序中仍然出现此错误?
以下是用于更多参考的代码(Android-Kotlin):
private fun analyzeImage(uri: Uri) {
val image: FirebaseVisionImage
try {
image = FirebaseVisionImage.fromFilePath(this, uri)
image.bitmap.run { Log.i("Image Size", "Width: $width | Height: $height") }
val options = FirebaseVisionCloudDocumentRecognizerOptions.Builder().setLanguageHints(listOf("en")).build()
val dtr = FirebaseVision.getInstance().getCloudDocumentTextRecognizer(options)
dtr.processImage(image)
.addOnSuccessListener { doOCR(it); btnGetPhoto.isEnabled = true }
.addOnFailureListener { it.printStackTrace(); btnGetPhoto.isEnabled = true }
} catch (ioe: IOException) {
Log.e("GetImage:Failed", "Failed to get image from uri", ioe)
}
}
private fun doOCR(fvtResult: FirebaseVisionDocumentText) {
tvResultText.text = buildString {
for (block in fvtResult.blocks) {
for (para in block.paragraphs) {
append("${para.text} -> [")
para.boundingBox?.run { append("L: $left, T: $top, R: $right, B: $bottom ]") }
append("\n\n")
}
}
}
}
如何摆脱这个错误?