Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[mob][photos] Revert unrelated auth plugin changes

Amrithesh Kakkoth Entea5b484d8c9fc
diff --git a/mobile/apps/auth/plugins/qr/android/src/main/kotlin/io/ente/auth/ente_qr/EnteQrPlugin.kt b/mobile/apps/auth/plugins/qr/android/src/main/kotlin/io/ente/auth/ente_qr/EnteQrPlugin.kt
index 5ab121d401..7386f80649 100644
--- a/mobile/apps/auth/plugins/qr/android/src/main/kotlin/io/ente/auth/ente_qr/EnteQrPlugin.kt
+++ b/mobile/apps/auth/plugins/qr/android/src/main/kotlin/io/ente/auth/ente_qr/EnteQrPlugin.kt
@@ -2,8 +2,6 @@ package io.ente.auth.ente_qr
import android.graphics.Bitmap
import android.graphics.BitmapFactory
-import android.graphics.Canvas
-import android.graphics.Color
import android.graphics.Matrix
import androidx.exifinterface.media.ExifInterface
import androidx.annotation.NonNull
@@ -31,10 +29,6 @@ class EnteQrPlugin: FlutterPlugin, MethodCallHandler {
private lateinit var channel : MethodChannel
private val executor = Executors.newSingleThreadExecutor()
- companion object {
- private const val SCAN_MAX_DIM = 1200
- }
-
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "ente_qr")
channel.setMethodCallHandler(this)
@@ -93,32 +87,12 @@ class EnteQrPlugin: FlutterPlugin, MethodCallHandler {
}
}
- // ── Image loading & pre-processing ────────────────────────────────
-
- private fun loadAtMaxDimension(imagePath: String, maxDim: Int): Bitmap? {
- val opts = BitmapFactory.Options()
- opts.inJustDecodeBounds = true
- BitmapFactory.decodeFile(imagePath, opts)
- val w = opts.outWidth
- val h = opts.outHeight
- if (w <= 0 || h <= 0) return null
-
- var inSampleSize = 1
- if (w > maxDim || h > maxDim) {
- val halfW = w / 2
- val halfH = h / 2
- while (halfW / inSampleSize >= maxDim ||
- halfH / inSampleSize >= maxDim) {
- inSampleSize *= 2
- }
- }
-
- val decodeOpts = BitmapFactory.Options()
- decodeOpts.inSampleSize = inSampleSize
- return BitmapFactory.decodeFile(imagePath, decodeOpts)
- }
-
- private fun applyExifOrientation(bitmap: Bitmap, imagePath: String): Bitmap {
+ /**
+ * Load a bitmap and apply EXIF orientation so the image is upright
+ * before QR detection.
+ */
+ private fun loadBitmapWithOrientation(imagePath: String): Bitmap? {
+ val bitmap = BitmapFactory.decodeFile(imagePath) ?: return null
return try {
val exif = ExifInterface(imagePath)
val orientation = exif.getAttributeInt(
@@ -141,89 +115,68 @@ class EnteQrPlugin: FlutterPlugin, MethodCallHandler {
}
}
- private fun fixTransparency(bitmap: Bitmap): Bitmap {
- if (!bitmap.hasAlpha()) return bitmap
- val opaque = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888)
- val canvas = Canvas(opaque)
- canvas.drawColor(Color.WHITE)
- canvas.drawBitmap(bitmap, 0f, 0f, null)
- bitmap.recycle()
- return opaque
- }
-
- private fun prepareBitmap(imagePath: String, maxDim: Int): Bitmap? {
- val raw = loadAtMaxDimension(imagePath, maxDim) ?: return null
- val oriented = applyExifOrientation(raw, imagePath)
- return fixTransparency(oriented)
- }
-
- // ── ZXing helpers ─────────────────────────────────────────────────
-
- private fun fastHints(): HashMap<DecodeHintType, Any> {
+ private fun createDecodeHints(): HashMap<DecodeHintType, Any> {
val hints = HashMap<DecodeHintType, Any>()
hints[DecodeHintType.POSSIBLE_FORMATS] = listOf(BarcodeFormat.QR_CODE)
- hints[DecodeHintType.ALSO_INVERTED] = true
- return hints
- }
-
- private fun thoroughHints(): HashMap<DecodeHintType, Any> {
- val hints = fastHints()
hints[DecodeHintType.TRY_HARDER] = true
+ hints[DecodeHintType.ALSO_INVERTED] = true
return hints
}
private fun getPixels(bitmap: Bitmap): IntArray {
- val pixels = IntArray(bitmap.width * bitmap.height)
- bitmap.getPixels(pixels, 0, bitmap.width, 0, 0, bitmap.width, bitmap.height)
+ val width = bitmap.width
+ val height = bitmap.height
+ val pixels = IntArray(width * height)
+ bitmap.getPixels(pixels, 0, width, 0, 0, width, height)
return pixels
}
- // ── ZXing decode ──────────────────────────────────────────────────
+ // ── ZXing decode strategies ───────────────────────────────────────
+ //
+ // 1. HybridBinarizer at original resolution (fast, handles most cases)
+ // 2. GlobalHistogramBinarizer at original resolution (uneven lighting)
+ // 3. HybridBinarizer at half resolution (moirΓ©, noise)
private fun zxingDecode(pixels: IntArray, width: Int, height: Int, hints: HashMap<DecodeHintType, Any>): ZXingResult? {
+ val source = RGBLuminanceSource(width, height, pixels)
val reader = MultiFormatReader()
reader.setHints(hints)
+ // Strategy 1: HybridBinarizer
try {
- val source = RGBLuminanceSource(width, height, pixels)
return reader.decode(BinaryBitmap(HybridBinarizer(source)))
} catch (_: NotFoundException) {}
+ // Strategy 2: GlobalHistogramBinarizer
try {
- val source = RGBLuminanceSource(width, height, pixels)
- return reader.decode(BinaryBitmap(GlobalHistogramBinarizer(source)))
+ val source2 = RGBLuminanceSource(width, height, pixels)
+ return reader.decode(BinaryBitmap(GlobalHistogramBinarizer(source2)))
} catch (_: NotFoundException) {}
return null
}
private fun zxingDecodeMultiple(pixels: IntArray, width: Int, height: Int, hints: HashMap<DecodeHintType, Any>): Array<ZXingResult>? {
+ val source = RGBLuminanceSource(width, height, pixels)
val reader = MultiFormatReader()
reader.setHints(hints)
val multiReader = GenericMultipleBarcodeReader(reader)
+ // Strategy 1: HybridBinarizer
try {
- val source = RGBLuminanceSource(width, height, pixels)
return multiReader.decodeMultiple(BinaryBitmap(HybridBinarizer(source)), hints)
} catch (_: NotFoundException) {}
+ // Strategy 2: GlobalHistogramBinarizer
try {
- val source = RGBLuminanceSource(width, height, pixels)
- return multiReader.decodeMultiple(BinaryBitmap(GlobalHistogramBinarizer(source)), hints)
+ val source2 = RGBLuminanceSource(width, height, pixels)
+ return multiReader.decodeMultiple(BinaryBitmap(GlobalHistogramBinarizer(source2)), hints)
} catch (_: NotFoundException) {}
return null
}
// ── Main scan methods ─────────────────────────────────────────────
- //
- // Two-phase strategy:
- // Phase 1 (fast): No TRY_HARDER, single resolution, dual binarizer
- // β†’ resolves most photos in <200ms
- // Phase 2 (thorough): Only if phase 1 found something β€” re-scan
- // with TRY_HARDER to catch additional QR codes
- //
- // Images without QR codes (the common case) only pay the phase 1 cost.
private fun scanQrCode(imagePath: String): Map<String, Any> {
try {
@@ -232,24 +185,33 @@ class EnteQrPlugin: FlutterPlugin, MethodCallHandler {
return mapOf("success" to false, "error" to "Image file not found: $imagePath")
}
- val bitmap = prepareBitmap(imagePath, SCAN_MAX_DIM)
+ val bitmap = loadBitmapWithOrientation(imagePath)
?: return mapOf("success" to false, "error" to "Unable to decode image file")
try {
- val pixels = getPixels(bitmap)
- val w = bitmap.width
- val h = bitmap.height
+ val hints = createDecodeHints()
- // Phase 1: fast scan (no TRY_HARDER)
- val fastResult = zxingDecode(pixels, w, h, fastHints())
- if (fastResult != null) {
- return mapOf("success" to true, "content" to fastResult.text)
+ // Try at original resolution
+ val pixels = getPixels(bitmap)
+ val result = zxingDecode(pixels, bitmap.width, bitmap.height, hints)
+ if (result != null) {
+ return mapOf("success" to true, "content" to result.text)
}
- // Phase 2: thorough scan (TRY_HARDER) only as fallback
- val thoroughResult = zxingDecode(pixels, w, h, thoroughHints())
- if (thoroughResult != null) {
- return mapOf("success" to true, "content" to thoroughResult.text)
+ // Fallback: half resolution (helps with moirΓ©/noise)
+ val halfW = bitmap.width / 2
+ val halfH = bitmap.height / 2
+ if (halfW > 0 && halfH > 0) {
+ val halfBitmap = Bitmap.createScaledBitmap(bitmap, halfW, halfH, true)
+ try {
+ val halfPixels = getPixels(halfBitmap)
+ val halfResult = zxingDecode(halfPixels, halfW, halfH, hints)
+ if (halfResult != null) {
+ return mapOf("success" to true, "content" to halfResult.text)
+ }
+ } finally {
+ halfBitmap.recycle()
+ }
}
return mapOf("success" to false, "error" to "No QR code found in image")
@@ -267,31 +229,40 @@ class EnteQrPlugin: FlutterPlugin, MethodCallHandler {
return mapOf("success" to false, "error" to "Image file not found: $imagePath")
}
- val bitmap = prepareBitmap(imagePath, SCAN_MAX_DIM)
+ val bitmap = loadBitmapWithOrientation(imagePath)
?: return mapOf("success" to false, "error" to "Unable to decode image file")
try {
+ val imgWidth = bitmap.width.toDouble()
+ val imgHeight = bitmap.height.toDouble()
+ val hints = createDecodeHints()
+
+ // Try at original resolution
val pixels = getPixels(bitmap)
- val w = bitmap.width
- val h = bitmap.height
- val imgW = w.toDouble()
- val imgH = h.toDouble()
-
- // Phase 1: fast scan (no TRY_HARDER)
- val fastResults = zxingDecodeMultiple(pixels, w, h, fastHints())
- if (fastResults != null && fastResults.isNotEmpty()) {
- val detections = buildDetections(fastResults, imgW, imgH)
+ val results = zxingDecodeMultiple(pixels, bitmap.width, bitmap.height, hints)
+ if (results != null && results.isNotEmpty()) {
+ val detections = buildDetections(results, imgWidth, imgHeight, 1.0)
if (detections.isNotEmpty()) {
return mapOf("success" to true, "detections" to detections)
}
}
- // Phase 2: thorough scan (TRY_HARDER) only as fallback
- val thoroughResults = zxingDecodeMultiple(pixels, w, h, thoroughHints())
- if (thoroughResults != null && thoroughResults.isNotEmpty()) {
- val detections = buildDetections(thoroughResults, imgW, imgH)
- if (detections.isNotEmpty()) {
- return mapOf("success" to true, "detections" to detections)
+ // Fallback: half resolution
+ val halfW = bitmap.width / 2
+ val halfH = bitmap.height / 2
+ if (halfW > 0 && halfH > 0) {
+ val halfBitmap = Bitmap.createScaledBitmap(bitmap, halfW, halfH, true)
+ try {
+ val halfPixels = getPixels(halfBitmap)
+ val halfResults = zxingDecodeMultiple(halfPixels, halfW, halfH, hints)
+ if (halfResults != null && halfResults.isNotEmpty()) {
+ val detections = buildDetections(halfResults, imgWidth, imgHeight, 2.0)
+ if (detections.isNotEmpty()) {
+ return mapOf("success" to true, "detections" to detections)
+ }
+ }
+ } finally {
+ halfBitmap.recycle()
}
}
@@ -307,6 +278,7 @@ class EnteQrPlugin: FlutterPlugin, MethodCallHandler {
results: Array<ZXingResult>,
imgWidth: Double,
imgHeight: Double,
+ scale: Double,
): List<Map<String, Any>> {
val detections = mutableListOf<Map<String, Any>>()
@@ -321,10 +293,12 @@ class EnteQrPlugin: FlutterPlugin, MethodCallHandler {
for (point in points) {
if (point == null) continue
- if (point.x < minX) minX = point.x
- if (point.y < minY) minY = point.y
- if (point.x > maxX) maxX = point.x
- if (point.y > maxY) maxY = point.y
+ val px = (point.x * scale).toFloat()
+ val py = (point.y * scale).toFloat()
+ if (px < minX) minX = px
+ if (py < minY) minY = py
+ if (px > maxX) maxX = px
+ if (py > maxY) maxY = py
}
// Add padding around finder patterns