π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Try original resolution for Auth QR image imports
Neeraj Guptad787062ab3a1
diff --git a/mobile/apps/auth/lib/utils/gallery_import_util.dart b/mobile/apps/auth/lib/utils/gallery_import_util.dartindex 3a69266e69..1d71d5d4ab 100644--- a/mobile/apps/auth/lib/utils/gallery_import_util.dart+++ b/mobile/apps/auth/lib/utils/gallery_import_util.dart@@ -136,7 +136,10 @@ Future<GalleryImportResult?> pickCodeFromGallery(}try {- final qrResult = await EnteQr().scanQrFromImage(imagePath);+ final qrResult = await EnteQr().scanQrFromImage(+ imagePath,+ tryOriginalResolution: true,+ );if (qrResult.success && qrResult.content != null) {try {diff --git a/mobile/packages/ente_qr/android/src/main/kotlin/io/ente/ente_qr/EnteQrPlugin.kt b/mobile/packages/ente_qr/android/src/main/kotlin/io/ente/ente_qr/EnteQrPlugin.ktindex 2b9ff5fd7e..2f3eddb31a 100644--- a/mobile/packages/ente_qr/android/src/main/kotlin/io/ente/ente_qr/EnteQrPlugin.kt+++ b/mobile/packages/ente_qr/android/src/main/kotlin/io/ente/ente_qr/EnteQrPlugin.kt@@ -42,8 +42,11 @@ class EnteQrPlugin: FlutterPlugin, MethodCallHandler {}executor.execute {+ val tryOriginalResolution =+ call.argument<Boolean>("tryOriginalResolution") ?: false+val qrResult = try {- scanQrCode(imagePath)+ scanQrCode(imagePath, tryOriginalResolution)} catch (e: Exception) {mapOf("success" to false, "error" to "Error scanning QR code: ${e.message}")}@@ -83,7 +86,7 @@ class EnteQrPlugin: FlutterPlugin, MethodCallHandler {* Load a bitmap with efficient downsampling and EXIF orientation correction.* Uses two-pass decoding: first get dimensions, then load at target size.*/- private fun loadBitmap(imagePath: String, maxDimension: Int = 1024): Bitmap? {+ private fun loadBitmap(imagePath: String, maxDimension: Int? = 1024): Bitmap? {// Pass 1: get dimensions without loading pixelsval opts = BitmapFactory.Options().apply { inJustDecodeBounds = true }BitmapFactory.decodeFile(imagePath, opts)@@ -93,12 +96,18 @@ class EnteQrPlugin: FlutterPlugin, MethodCallHandler {// Pass 2: load with downsamplingvar sampleSize = 1- while (rawW / (sampleSize * 2) >= maxDimension ||- rawH / (sampleSize * 2) >= maxDimension) {- sampleSize *= 2+ if (maxDimension != null) {+ while (rawW / (sampleSize * 2) >= maxDimension ||+ rawH / (sampleSize * 2) >= maxDimension) {+ sampleSize *= 2+ }}val decodeOpts = BitmapFactory.Options().apply { inSampleSize = sampleSize }- val bitmap = BitmapFactory.decodeFile(imagePath, decodeOpts) ?: return null+ val bitmap = try {+ BitmapFactory.decodeFile(imagePath, decodeOpts)+ } catch (_: OutOfMemoryError) {+ null+ } ?: return null// Apply EXIF orientationreturn try {@@ -123,6 +132,34 @@ class EnteQrPlugin: FlutterPlugin, MethodCallHandler {}}+ private fun readQrContent(bitmap: Bitmap): String? {+ val options = createReaderOptions()+ val results = ZxingCpp.readBitmap(+ bitmap, 0, 0, bitmap.width, bitmap.height, 0, options+ )++ if (results != null && results.isNotEmpty()) {+ val text = results[0].text+ if (text.isNotEmpty()) {+ return text+ }+ }++ // Fallback: try GlobalHistogram binarizer+ options.binarizer = Binarizer.GLOBAL_HISTOGRAM+ val fallbackResults = ZxingCpp.readBitmap(+ bitmap, 0, 0, bitmap.width, bitmap.height, 0, options+ )+ if (fallbackResults != null && fallbackResults.isNotEmpty()) {+ val text = fallbackResults[0].text+ if (text.isNotEmpty()) {+ return text+ }+ }++ return null+ }+private fun createReaderOptions(): ReaderOptions {return ReaderOptions().apply {formats = setOf(ZxingCpp.BarcodeFormat.QRCode)@@ -136,38 +173,36 @@ class EnteQrPlugin: FlutterPlugin, MethodCallHandler {// ββ Main scan methods βββββββββββββββββββββββββββββββββββββββββββββ- private fun scanQrCode(imagePath: String): Map<String, Any> {+ private fun scanQrCode(+ imagePath: String,+ tryOriginalResolution: Boolean = false,+ ): Map<String, Any> {val file = File(imagePath)if (!file.exists()) {return mapOf("success" to false, "error" to "Image file not found: $imagePath")}+ if (tryOriginalResolution) {+ val originalBitmap = loadBitmap(imagePath, maxDimension = null)+ if (originalBitmap != null) {+ try {+ val text = readQrContent(originalBitmap)+ if (text != null) {+ return mapOf("success" to true, "content" to text)+ }+ } finally {+ originalBitmap.recycle()+ }+ }+ }+val bitmap = loadBitmap(imagePath)?: return mapOf("success" to false, "error" to "Unable to decode image file")try {- val options = createReaderOptions()- val results = ZxingCpp.readBitmap(- bitmap, 0, 0, bitmap.width, bitmap.height, 0, options- )-- if (results != null && results.isNotEmpty()) {- val text = results[0].text- if (text.isNotEmpty()) {- return mapOf("success" to true, "content" to text)- }- }-- // Fallback: try GlobalHistogram binarizer- options.binarizer = Binarizer.GLOBAL_HISTOGRAM- val fallbackResults = ZxingCpp.readBitmap(- bitmap, 0, 0, bitmap.width, bitmap.height, 0, options- )- if (fallbackResults != null && fallbackResults.isNotEmpty()) {- val text = fallbackResults[0].text- if (text.isNotEmpty()) {- return mapOf("success" to true, "content" to text)- }+ val text = readQrContent(bitmap)+ if (text != null) {+ return mapOf("success" to true, "content" to text)}return mapOf("success" to false, "error" to "No QR code found in image")diff --git a/mobile/packages/ente_qr/lib/ente_qr.dart b/mobile/packages/ente_qr/lib/ente_qr.dartindex fd0e56d63d..3ae1beecf2 100644--- a/mobile/packages/ente_qr/lib/ente_qr.dart+++ b/mobile/packages/ente_qr/lib/ente_qr.dart@@ -24,8 +24,14 @@ class EnteQr {////// Returns a [QrScanResult] containing either the QR code content on success/// or an error message on failure.- Future<QrScanResult> scanQrFromImage(String imagePath) {- return _scanPlatform.scanQrFromImage(imagePath);+ Future<QrScanResult> scanQrFromImage(+ String imagePath, {+ bool tryOriginalResolution = false,+ }) {+ return _scanPlatform.scanQrFromImage(+ imagePath,+ tryOriginalResolution: tryOriginalResolution,+ );}/// Scans all QR codes from an image file at the given path.diff --git a/mobile/packages/ente_qr/lib/ente_qr_method_channel.dart b/mobile/packages/ente_qr/lib/ente_qr_method_channel.dartindex ae439df7d8..cb81787d97 100644--- a/mobile/packages/ente_qr/lib/ente_qr_method_channel.dart+++ b/mobile/packages/ente_qr/lib/ente_qr_method_channel.dart@@ -17,11 +17,17 @@ class MethodChannelEnteQr extends EnteQrPlatform {}@override- Future<QrScanResult> scanQrFromImage(String imagePath) async {+ Future<QrScanResult> scanQrFromImage(+ String imagePath, {+ bool tryOriginalResolution = false,+ }) async {try {final dynamic result = await methodChannel.invokeMethod('scanQrFromImage',- {'imagePath': imagePath},+ {+ 'imagePath': imagePath,+ 'tryOriginalResolution': tryOriginalResolution,+ },);if (result == null) {diff --git a/mobile/packages/ente_qr/lib/ente_qr_platform_interface.dart b/mobile/packages/ente_qr/lib/ente_qr_platform_interface.dartindex 37086db52b..1c1f14f354 100644--- a/mobile/packages/ente_qr/lib/ente_qr_platform_interface.dart+++ b/mobile/packages/ente_qr/lib/ente_qr_platform_interface.dart@@ -102,7 +102,10 @@ abstract class EnteQrPlatform extends PlatformInterface {/// Scans a QR code from an image file at the given path./// Returns the QR code content as a string if successful, null otherwise.- Future<QrScanResult> scanQrFromImage(String imagePath) {+ Future<QrScanResult> scanQrFromImage(+ String imagePath, {+ bool tryOriginalResolution = false,+ }) {throw UnimplementedError('scanQrFromImage() has not been implemented.');}diff --git a/mobile/packages/ente_qr/lib/src/dart_zxing_qr_platform.dart b/mobile/packages/ente_qr/lib/src/dart_zxing_qr_platform.dartindex 4125f999b6..ab4277bd0d 100644--- a/mobile/packages/ente_qr/lib/src/dart_zxing_qr_platform.dart+++ b/mobile/packages/ente_qr/lib/src/dart_zxing_qr_platform.dart@@ -11,7 +11,10 @@ final class DartZxingQrPlatform extends EnteQrPlatform {}@override- Future<QrScanResult> scanQrFromImage(String imagePath) async {+ Future<QrScanResult> scanQrFromImage(+ String imagePath, {+ bool tryOriginalResolution = false,+ }) async {try {final content = await _decodeQrFromImagePath(imagePath);return QrScanResult.success(content);diff --git a/mobile/packages/ente_qr/test/ente_qr_test.dart b/mobile/packages/ente_qr/test/ente_qr_test.dartindex 2d9bf5f019..5168aaf401 100644--- a/mobile/packages/ente_qr/test/ente_qr_test.dart+++ b/mobile/packages/ente_qr/test/ente_qr_test.dart@@ -11,8 +11,10 @@ class MockEnteQrPlatformFuture<String?> getPlatformVersion() => Future.value('42');@override- Future<QrScanResult> scanQrFromImage(String imagePath) =>- Future.value(QrScanResult.error('Mock implementation'));+ Future<QrScanResult> scanQrFromImage(+ String imagePath, {+ bool tryOriginalResolution = false,+ }) => Future.value(QrScanResult.error('Mock implementation'));@overrideFuture<QrScanResults> scanAllQrFromImage(String imagePath) =>