【发布时间】:2018-05-15 11:05:20
【问题描述】:
我想使用我的相机从条形码中获取一些信息。 当我使用从网站下载的 png 图像时它可以工作,但是当我尝试让它与我拍摄的照片一起工作时,它会输出空数组。似乎我需要对图像进行一些准备才能使其正常工作。 这是我的代码:
fun getTheBarCode(bitmap: Bitmap) {
val options = FirebaseVisionBarcodeDetectorOptions.Builder()
.setBarcodeFormats(
FirebaseVisionBarcode.FORMAT_AZTEC)
.build()
val detector = FirebaseVision.getInstance().getVisionBarcodeDetector(options)
val bm = BitmapFactory.decodeResource(getResources(), R.drawable.barcode) //this is the place where I can load my downloaded barcode to make everything work!
val newBitmap = Bitmap.createScaledBitmap(bitmap, 300, 500, false)
val image = FirebaseVisionImage.fromBitmap(newBitmap)
photoImage.setImageBitmap(newBitmap)
detector.detectInImage(image)
.addOnSuccessListener {
Log.d("Success", "Success")
//empty array here, when I take picture.
}
.addOnFailureListener {
Log.d("Failed", it.message)
}
}
这就是我从相机获取图像的方式
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
val photo = data.extras.get("data") as Bitmap
getTheBarCode(photo)
}
}
编辑:
我用我的手机拍了一张照片,把它缩小到 1500x1000px 并把它放在我的应用程序目录中,然后将它作为位图加载。 还是不行。
【问题讨论】:
-
1.就像下面的另一个答案一样,我怀疑您会得到一张低质量的缩略图照片。 2. 根据您尝试解析的条形码类型,需要不同的分辨率。但是1280*960大部分时间都可以。请参阅此处的公共文档:firebase.google.com/docs/reference/android/com/google/firebase/…。此外,条形码检测是在设备上进行的,因此您无需在将图像提供给 API 之前调整其大小。
-
@IsabellaChen 请查看下面的评论。大图像也不起作用。
-
但是你还在把它缩放到 300 * 500 吗?你能删除调整大小的逻辑吗?另外,试试 FirebaseVisionImage.fromFilePath。如果还是不行,把图分享给我,我看看?
-
您好问题解决了吗?我也面临同样的问题,我正在使用遵循developer.android.com/training/camera/photobasics 指南的全质量照片......没有让条形码工作......
标签: android kotlin firebase-mlkit