扫码改用 ZXing 并按 ABI 拆分安装包(30MB 减至 5MB)
This commit is contained in:
@@ -69,16 +69,6 @@ private val licenseGroups = listOf(
|
||||
url = "https://github.com/google/gson"
|
||||
)
|
||||
)
|
||||
),
|
||||
LicenseGroup(
|
||||
license = "ML Kit Terms of Service",
|
||||
licenseUrl = "https://developers.google.com/ml-kit/terms",
|
||||
libraries = listOf(
|
||||
LibraryLicense(
|
||||
name = "ML Kit Barcode Scanning",
|
||||
url = "https://developers.google.com/ml-kit"
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
package cn.airnan.a2fair.ui.tokens
|
||||
|
||||
import android.Manifest
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageManager
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.graphics.ImageFormat
|
||||
import android.net.Uri
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.PickVisualMediaRequest
|
||||
@@ -36,10 +40,17 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import cn.airnan.a2fair.R
|
||||
import cn.airnan.a2fair.data.entity.HashAlgorithm
|
||||
import cn.airnan.a2fair.data.entity.TokenType
|
||||
import com.google.mlkit.vision.barcode.BarcodeScanning
|
||||
import com.google.mlkit.vision.barcode.common.Barcode
|
||||
import com.google.mlkit.vision.common.InputImage
|
||||
import com.google.zxing.BarcodeFormat
|
||||
import com.google.zxing.BinaryBitmap
|
||||
import com.google.zxing.DecodeHintType
|
||||
import com.google.zxing.LuminanceSource
|
||||
import com.google.zxing.MultiFormatReader
|
||||
import com.google.zxing.NotFoundException
|
||||
import com.google.zxing.PlanarYUVLuminanceSource
|
||||
import com.google.zxing.RGBLuminanceSource
|
||||
import com.google.zxing.common.HybridBinarizer
|
||||
import kotlinx.coroutines.launch
|
||||
import java.nio.ByteBuffer
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@@ -59,25 +70,11 @@ fun AddTokenBottomSheet(
|
||||
contract = ActivityResultContracts.PickVisualMedia()
|
||||
) { uri: Uri? ->
|
||||
if (uri != null) {
|
||||
try {
|
||||
val image = InputImage.fromFilePath(context, uri)
|
||||
val scanner = BarcodeScanning.getClient()
|
||||
scanner.process(image)
|
||||
.addOnSuccessListener { barcodes ->
|
||||
val barcode = barcodes.firstOrNull { it.valueType == Barcode.TYPE_WIFI || it.rawValue?.startsWith("otpauth://") == true }
|
||||
?: barcodes.firstOrNull()
|
||||
val rawValue = barcode?.rawValue
|
||||
if (rawValue != null && rawValue.startsWith("otpauth://")) {
|
||||
viewModel.addTokenFromUri(rawValue)
|
||||
} else {
|
||||
// Handle error: no valid QR code found
|
||||
}
|
||||
}
|
||||
.addOnFailureListener {
|
||||
// Handle error
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
val rawValue = decodeQrFromUri(context, uri)
|
||||
if (rawValue != null && rawValue.startsWith("otpauth://")) {
|
||||
viewModel.addTokenFromUri(rawValue)
|
||||
} else {
|
||||
// Handle error: no valid QR code found
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -255,37 +252,124 @@ fun AddTokenBottomSheet(
|
||||
}
|
||||
|
||||
class ZxingAnalyzer(private val onScan: (String) -> Unit) : ImageAnalysis.Analyzer {
|
||||
private val scanner = BarcodeScanning.getClient()
|
||||
private val reader = MultiFormatReader().apply {
|
||||
setHints(
|
||||
mapOf(
|
||||
DecodeHintType.POSSIBLE_FORMATS to listOf(BarcodeFormat.QR_CODE, BarcodeFormat.AZTEC),
|
||||
DecodeHintType.TRY_HARDER to true
|
||||
)
|
||||
)
|
||||
}
|
||||
@Volatile
|
||||
private var scanned = false
|
||||
|
||||
@androidx.annotation.OptIn(androidx.camera.core.ExperimentalGetImage::class)
|
||||
override fun analyze(imageProxy: ImageProxy) {
|
||||
if (scanned) {
|
||||
imageProxy.close()
|
||||
return
|
||||
}
|
||||
val mediaImage = imageProxy.image
|
||||
if (mediaImage != null) {
|
||||
val image = InputImage.fromMediaImage(mediaImage, imageProxy.imageInfo.rotationDegrees)
|
||||
scanner.process(image)
|
||||
.addOnSuccessListener { barcodes ->
|
||||
val barcode = barcodes.firstOrNull { it.valueType == Barcode.TYPE_WIFI || it.rawValue?.startsWith("otpauth://") == true }
|
||||
?: barcodes.firstOrNull()
|
||||
val rawValue = barcode?.rawValue
|
||||
if (rawValue != null && rawValue.startsWith("otpauth://")) {
|
||||
scanned = true
|
||||
onScan(rawValue)
|
||||
}
|
||||
val text = try {
|
||||
val source = luminanceSource(imageProxy)
|
||||
if (source != null) {
|
||||
try {
|
||||
reader.decodeWithState(BinaryBitmap(HybridBinarizer(source))).text
|
||||
} catch (e: NotFoundException) {
|
||||
null
|
||||
} finally {
|
||||
reader.reset()
|
||||
}
|
||||
.addOnFailureListener {
|
||||
// Ignored
|
||||
}
|
||||
.addOnCompleteListener {
|
||||
imageProxy.close()
|
||||
}
|
||||
} else {
|
||||
} else {
|
||||
null
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
null
|
||||
} finally {
|
||||
imageProxy.close()
|
||||
}
|
||||
if (text != null && text.startsWith("otpauth://")) {
|
||||
scanned = true
|
||||
onScan(text)
|
||||
}
|
||||
}
|
||||
|
||||
private fun luminanceSource(proxy: ImageProxy): LuminanceSource? {
|
||||
if (proxy.format != ImageFormat.YUV_420_888) return null
|
||||
val plane = proxy.planes[0]
|
||||
val yPlane = extractYPlane(
|
||||
plane.buffer, proxy.width, proxy.height, plane.rowStride, plane.pixelStride,
|
||||
proxy.imageInfo.rotationDegrees
|
||||
)
|
||||
return PlanarYUVLuminanceSource(yPlane.data, yPlane.width, yPlane.height, 0, 0, yPlane.width, yPlane.height, false)
|
||||
}
|
||||
}
|
||||
|
||||
internal class YPlane(val data: ByteArray, val width: Int, val height: Int)
|
||||
|
||||
/**
|
||||
* Compacts the camera Y plane (dropping row/pixel padding) and rotates it by
|
||||
* [rotationDegrees] into display orientation. PlanarYUVLuminanceSource itself
|
||||
* cannot rotate (it throws UnsupportedOperationException), so the raw bytes
|
||||
* are re-indexed here. Output dimensions are swapped for 90/270.
|
||||
*/
|
||||
internal fun extractYPlane(
|
||||
buffer: ByteBuffer,
|
||||
width: Int,
|
||||
height: Int,
|
||||
rowStride: Int,
|
||||
pixelStride: Int,
|
||||
rotationDegrees: Int
|
||||
): YPlane {
|
||||
val rotation = ((rotationDegrees % 360) + 360) % 360
|
||||
val rotated = rotation == 90 || rotation == 270
|
||||
val outWidth = if (rotated) height else width
|
||||
val outHeight = if (rotated) width else height
|
||||
val out = ByteArray(outWidth * outHeight)
|
||||
for (y in 0 until height) {
|
||||
val rowStart = y * rowStride
|
||||
for (x in 0 until width) {
|
||||
val v = buffer.get(rowStart + x * pixelStride)
|
||||
when (rotation) {
|
||||
90 -> out[(width - 1 - x) * height + y] = v
|
||||
180 -> out[(height - 1 - y) * width + (width - 1 - x)] = v
|
||||
270 -> out[x * height + (height - 1 - y)] = v
|
||||
else -> out[y * width + x] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
return YPlane(out, outWidth, outHeight)
|
||||
}
|
||||
|
||||
private fun decodeQrFromUri(context: Context, uri: Uri): String? {
|
||||
return try {
|
||||
val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
|
||||
context.contentResolver.openInputStream(uri)?.use {
|
||||
BitmapFactory.decodeStream(it, null, bounds)
|
||||
}
|
||||
var sampleSize = 1
|
||||
while (maxOf(bounds.outWidth, bounds.outHeight) / sampleSize > 1536) sampleSize *= 2
|
||||
val options = BitmapFactory.Options().apply { inSampleSize = sampleSize }
|
||||
val bitmap = context.contentResolver.openInputStream(uri)?.use {
|
||||
BitmapFactory.decodeStream(it, null, options)
|
||||
} ?: return null
|
||||
val pixels = IntArray(bitmap.width * bitmap.height)
|
||||
bitmap.getPixels(pixels, 0, bitmap.width, 0, 0, bitmap.width, bitmap.height)
|
||||
bitmap.recycle()
|
||||
val reader = MultiFormatReader().apply {
|
||||
setHints(
|
||||
mapOf(
|
||||
DecodeHintType.POSSIBLE_FORMATS to listOf(BarcodeFormat.QR_CODE, BarcodeFormat.AZTEC),
|
||||
DecodeHintType.TRY_HARDER to true
|
||||
)
|
||||
)
|
||||
}
|
||||
try {
|
||||
reader.decodeWithState(BinaryBitmap(HybridBinarizer(RGBLuminanceSource(bitmap.width, bitmap.height, pixels)))).text
|
||||
} catch (e: NotFoundException) {
|
||||
null
|
||||
} finally {
|
||||
reader.reset()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user