googlesamples / googlesamples/mlkit

[Bug report] Barcode scanning not detect QR when use Bitmap

Open
#696 1 comment 0 reactions 1 assignee Claimed by @zhouyiself View on GitHub
Dominant language
Kotlin
Stars
4.3k
Forks
3.1k
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**
When i use the camera and follow the instructions to scan 1 image containing my QR on s3 clound. I got 1 QR Info. But when I downloaded that image and converted it to bitmap, I used InputImage.fromBitmap(bitmap,0) as instructed, I didn't get result.

**To Reproduce**
This is my code:
This is url qrCode https://purrfect-ai-use.s3.us-east-1.amazonaws.com/purrfect-ai-delivery/26901d34-30f6-4ee6-95f6-3f938c4a6617_b78145693084453ca94829a2e7ce0dd7.png

private fun processFromNetwork(
url: String,
callBack: (results: List?, error: QrCodeReaderError?) -> Unit
) {

val handlerThread = object : Thread() {
override fun run() {
if (!URLUtil.isValidUrl(url)) {
callBack(null, QrCodeReaderError.URLNotFound)
return
}

val request = Request.Builder().url(url).build()

OkHttpClient().newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
callBack(null, QrCodeReaderError.CreateFileFromUrlError)
}

override fun onResponse(call: Call, response: Response) {
val body = response.body()

if (body != null) {
val bytes = body.bytes()

val options = BitmapFactory.Options()

options.inMutable = true

val bitmap =
BitmapFactory.decodeByteArray(bytes, 0, bytes.size, options)

Log.w("QR_CODE_READER","${bitmap.width} - ${bitmap.height}")

try {
val barcodes = Tasks.await(barCodeDetector.process(bitmap, 0))

val size = barcodes.size

if (size == 0) {
callBack(listOf(), null)
} else {
val qrCodeResults = mutableListOf()
for (i in 0 until size) {
val barCode = barcodes[i]
val box = barCode.boundingBox!!

val topLeft = HashMap()

topLeft["x"] = (box.width() - box.left).toDouble()
topLeft["y"] = box.top.toDouble()

val topRight = HashMap()

topRight["x"] = (box.width() - box.right).toDouble()
topRight["y"] = box.top.toDouble()

val bottomLeft = HashMap()

bottomLeft["x"] = (box.width() - box.left).toDouble()
bottomLeft["y"] = box.bottom.toDouble()

val bottomRight = HashMap()

bottomRight["x"] = (box.width() - box.right).toDouble()
bottomRight["y"] = box.bottom.toDouble()

val qrCodeResult = QrCodeReaderResult(
barCode.rawValue!!,
bitmap.width.toDouble(),
bitmap.height.toDouble(),
topLeft,
topRight,
bottomLeft,
bottomRight,
)

qrCodeResults.add(qrCodeResult)
}
callBack(qrCodeResults, null)

}
} catch (e: Exception) {
Log.e("QR_CODE_READER", "Error ${e.message}\n${e.localizedMessage}")
callBack(null, QrCodeReaderError.Other)
}

} else {
callBack(null, QrCodeReaderError.CreateFileFromUrlError)
}
}

})
super.run()
}
}

handlerThread.start()
}

**Expected behavior**
Receive QRCode from image

**SDK Info:**
- SDK Name & Version 'com.google.android.gms:play-services-mlkit-barcode-scanning:18.2.0'

**Smartphone:**
- All device

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.