use only sampleSize parameter

This commit is contained in:
Adam Velebil 2023-10-30 16:53:17 +01:00
parent 6a32cdc835
commit 2faf75b40e
No known key found for this signature in database
GPG Key ID: C9B1E4A3CBBD2E10

View File

@ -2,10 +2,6 @@ package com.yubico.authenticator.flutter_plugins.qrscanner_zxing
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.ColorMatrix
import android.graphics.ColorMatrixColorFilter
import android.graphics.Paint
import android.util.Log
import com.google.zxing.BarcodeFormat
import com.google.zxing.BinaryBitmap
@ -34,17 +30,17 @@ object QrCodeScanner {
private fun decodeFromBytes(
byteArray: ByteArray,
sampleSize: Int,
rotation: Int,
boostContrast: Boolean
sampleSize: Int
): String? {
var bitmap: Bitmap? = null
try {
Log.v(
TAG,
"Decoding with sampleSize $sampleSize, rotation $rotation, boostContrast: $boostContrast"
"Decoding with sampleSize $sampleSize"
)
bitmap = getScaledBitmap(byteArray, sampleSize, boostContrast)
val options = BitmapFactory.Options()
options.inSampleSize = sampleSize
bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size, options)
bitmap?.let {
val pixels = IntArray(it.allocationByteCount)
it.getPixels(pixels, 0, it.width, 0, 0, it.width, it.height)
@ -52,14 +48,7 @@ object QrCodeScanner {
val luminanceSource =
RGBLuminanceSource(it.width, it.height, pixels)
var binaryBitmap = BinaryBitmap(HybridBinarizer(luminanceSource))
if (rotation in 1..3 && binaryBitmap.isRotateSupported) {
for (r in 1..rotation) {
binaryBitmap = binaryBitmap.rotateCounterClockwise()
}
}
val binaryBitmap = BinaryBitmap(HybridBinarizer(luminanceSource))
val scanResult = decodeFromBinaryBitmap(binaryBitmap)
Log.v(TAG, "Scan result: $scanResult")
return scanResult
@ -81,67 +70,11 @@ object QrCodeScanner {
}
fun decodeFromBytes(byteArray: ByteArray): String? {
for (boostContrast in sequenceOf(false, true)) {
for (rotation in 0 until 4) {
for (sampleSize in sequenceOf(1, 4, 8, 12)) {
val code = decodeFromBytes(byteArray, sampleSize, rotation, boostContrast)
if (code != null) {
return code
}
}
}
}
return null
return decodeFromBytes(byteArray, 1)
?: decodeFromBytes(byteArray, 4)
?: decodeFromBytes(byteArray, 8)
?: decodeFromBytes(byteArray, 12)
}
private fun getScaledBitmap(
byteArray: ByteArray,
sampleSize: Int,
boostContrast: Boolean
): Bitmap? {
var inputBitmap: Bitmap? = null
try {
val options = BitmapFactory.Options()
options.inSampleSize = sampleSize
inputBitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size, options)
inputBitmap?.let {
val drawingBitmap =
Bitmap.createBitmap(it.width, it.height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(drawingBitmap)
val cm = ColorMatrix()
if (boostContrast) {
val contrast = 0f
val scale = contrast + 1f
val translate = (-.5f * scale + .5f) * 255f
cm.setSaturation(0f)
cm.postConcat(
ColorMatrix(
floatArrayOf(
scale, 0f, 0f, 0f, translate,
0f, scale, 0f, 0f, translate,
0f, 0f, scale, 0f, translate,
0f, 0f, 0f, scale, 0f
)
)
)
}
val paint = Paint()
paint.setColorFilter(ColorMatrixColorFilter(cm))
canvas.drawBitmap(it, 0f, 0f, paint)
return drawingBitmap
}
} finally {
inputBitmap?.recycle()
}
return null
}
private const val TAG = "QRScanner"
}