使用ML套件从位图读取条形码信息

时间:2018-05-15 11:05:21

标签: android kotlin firebase-mlkit

我想用相机从条形码中获取一些信息。 当我使用从网站下载的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并将其放入我的app目录,然后将其作为位图加载。 仍然没有工作。

1 个答案:

答案 0 :(得分:0)

您使用的方法只会返回照片的缩略图(根据https://developer.android.com/training/camera/photobasics)...这可能不足以满足您的目的。该链接还包含有关如何访问全尺寸照片的信息。