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.Bitmap
import android.graphics.BitmapFactory 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 android.util.Log
import com.google.zxing.BarcodeFormat import com.google.zxing.BarcodeFormat
import com.google.zxing.BinaryBitmap import com.google.zxing.BinaryBitmap
@ -34,17 +30,17 @@ object QrCodeScanner {
private fun decodeFromBytes( private fun decodeFromBytes(
byteArray: ByteArray, byteArray: ByteArray,
sampleSize: Int, sampleSize: Int
rotation: Int,
boostContrast: Boolean
): String? { ): String? {
var bitmap: Bitmap? = null var bitmap: Bitmap? = null
try { try {
Log.v( Log.v(
TAG, 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 { bitmap?.let {
val pixels = IntArray(it.allocationByteCount) val pixels = IntArray(it.allocationByteCount)
it.getPixels(pixels, 0, it.width, 0, 0, it.width, it.height) it.getPixels(pixels, 0, it.width, 0, 0, it.width, it.height)
@ -52,14 +48,7 @@ object QrCodeScanner {
val luminanceSource = val luminanceSource =
RGBLuminanceSource(it.width, it.height, pixels) RGBLuminanceSource(it.width, it.height, pixels)
var binaryBitmap = BinaryBitmap(HybridBinarizer(luminanceSource)) val binaryBitmap = BinaryBitmap(HybridBinarizer(luminanceSource))
if (rotation in 1..3 && binaryBitmap.isRotateSupported) {
for (r in 1..rotation) {
binaryBitmap = binaryBitmap.rotateCounterClockwise()
}
}
val scanResult = decodeFromBinaryBitmap(binaryBitmap) val scanResult = decodeFromBinaryBitmap(binaryBitmap)
Log.v(TAG, "Scan result: $scanResult") Log.v(TAG, "Scan result: $scanResult")
return scanResult return scanResult
@ -81,67 +70,11 @@ object QrCodeScanner {
} }
fun decodeFromBytes(byteArray: ByteArray): String? { fun decodeFromBytes(byteArray: ByteArray): String? {
for (boostContrast in sequenceOf(false, true)) { return decodeFromBytes(byteArray, 1)
for (rotation in 0 until 4) { ?: decodeFromBytes(byteArray, 4)
for (sampleSize in sequenceOf(1, 4, 8, 12)) { ?: decodeFromBytes(byteArray, 8)
val code = decodeFromBytes(byteArray, sampleSize, rotation, boostContrast) ?: decodeFromBytes(byteArray, 12)
if (code != null) {
return code
}
}
}
}
return null
} }
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" private const val TAG = "QRScanner"
} }