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.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"
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user