首页 > 解决方案 > 使用机器学习套件从位图中读取条形码信息

问题描述

我想使用我的相机从条形码中获取一些信息。当我使用从网站下载的 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 并把它放在我的应用程序目录中,然后将它作为位图加载。还是行不通。

标签: androidkotlinfirebase-mlkit

解决方案


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


推荐阅读