mirror of
https://github.com/Yubico/yubioath-flutter.git
synced 2024-12-22 17:51:29 +03:00
use only sampleSize parameter
This commit is contained in:
parent
6a32cdc835
commit
2faf75b40e
@ -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"
|
||||
}
|
Loading…
Reference in New Issue
Block a user