π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Move iOS QR detection off main thread
Amrithesh Kakkoth Ente22ebf68836c2
diff --git a/mobile/apps/auth/plugins/qr/ios/Classes/EnteQrPlugin.swift b/mobile/apps/auth/plugins/qr/ios/Classes/EnteQrPlugin.swiftindex 7a94d2fbe5..6903a921e6 100644--- a/mobile/apps/auth/plugins/qr/ios/Classes/EnteQrPlugin.swift+++ b/mobile/apps/auth/plugins/qr/ios/Classes/EnteQrPlugin.swift@@ -2,6 +2,8 @@ import Flutterimport UIKitpublic class EnteQrPlugin: NSObject, FlutterPlugin {+ private static let detectionQueue = DispatchQueue(label: "io.ente.qr.detection", qos: .userInitiated)+public static func register(with registrar: FlutterPluginRegistrar) {let channel = FlutterMethodChannel(name: "ente_qr", binaryMessenger: registrar.messenger())let instance = EnteQrPlugin()@@ -38,137 +40,163 @@ public class EnteQrPlugin: NSObject, FlutterPlugin {result(FlutterMethodNotImplemented)}}-+private func scanQrCode(from imagePath: String, result: @escaping FlutterResult) {- guard let image = UIImage(contentsOfFile: imagePath) else {- result([- "success": false,- "error": "Unable to load image from path: \(imagePath)"- ])- return- }-- guard let cgImage = image.cgImage else {- result([- "success": false,- "error": "Unable to get CGImage from UIImage"- ])- return- }-- let detector = CIDetector(ofType: CIDetectorTypeQRCode,- context: nil,- options: [CIDetectorAccuracy: CIDetectorAccuracyHigh])-- guard let qrDetector = detector else {- result([- "success": false,- "error": "Unable to create QR code detector"- ])- return- }-- let ciImage = CIImage(cgImage: cgImage)- let features = qrDetector.features(in: ciImage)-- if let qrFeature = features.first as? CIQRCodeFeature,- let messageString = qrFeature.messageString {- result([- "success": true,- "content": messageString- ])- } else {- result([- "success": false,- "error": "No QR code found in image"- ])- }- }+ EnteQrPlugin.detectionQueue.async {+ guard let image = UIImage(contentsOfFile: imagePath) else {+ DispatchQueue.main.async {+ result([+ "success": false,+ "error": "Unable to load image from path: \(imagePath)"+ ])+ }+ return+ }- private func scanAllQrCodes(from imagePath: String, result: @escaping FlutterResult) {- guard let image = UIImage(contentsOfFile: imagePath) else {- result([- "success": false,- "error": "Unable to load image from path: \(imagePath)"- ])- return- }+ guard let cgImage = image.cgImage else {+ DispatchQueue.main.async {+ result([+ "success": false,+ "error": "Unable to get CGImage from UIImage"+ ])+ }+ return+ }- guard let cgImage = image.cgImage else {- result([- "success": false,- "error": "Unable to get CGImage from UIImage"- ])- return- }+ let detector = CIDetector(ofType: CIDetectorTypeQRCode,+ context: nil,+ options: [CIDetectorAccuracy: CIDetectorAccuracyHigh])++ guard let qrDetector = detector else {+ DispatchQueue.main.async {+ result([+ "success": false,+ "error": "Unable to create QR code detector"+ ])+ }+ return+ }- let detector = CIDetector(ofType: CIDetectorTypeQRCode,- context: nil,- options: [CIDetectorAccuracy: CIDetectorAccuracyHigh])+ let ciImage = CIImage(cgImage: cgImage)+ let features = qrDetector.features(in: ciImage)++ let response: [String: Any]+ if let qrFeature = features.first as? CIQRCodeFeature,+ let messageString = qrFeature.messageString {+ response = [+ "success": true,+ "content": messageString+ ]+ } else {+ response = [+ "success": false,+ "error": "No QR code found in image"+ ]+ }- guard let qrDetector = detector else {- result([- "success": false,- "error": "Unable to create QR code detector"- ])- return+ DispatchQueue.main.async {+ result(response)+ }}+ }- let ciImage = CIImage(cgImage: cgImage)- let features = qrDetector.features(in: ciImage)+ private func scanAllQrCodes(from imagePath: String, result: @escaping FlutterResult) {+ EnteQrPlugin.detectionQueue.async {+ guard let image = UIImage(contentsOfFile: imagePath) else {+ DispatchQueue.main.async {+ result([+ "success": false,+ "error": "Unable to load image from path: \(imagePath)"+ ])+ }+ return+ }- let imageWidth = CGFloat(cgImage.width)- let imageHeight = CGFloat(cgImage.height)+ guard let cgImage = image.cgImage else {+ DispatchQueue.main.async {+ result([+ "success": false,+ "error": "Unable to get CGImage from UIImage"+ ])+ }+ return+ }- var detections: [[String: Any]] = []+ let detector = CIDetector(ofType: CIDetectorTypeQRCode,+ context: nil,+ options: [CIDetectorAccuracy: CIDetectorAccuracyHigh])++ guard let qrDetector = detector else {+ DispatchQueue.main.async {+ result([+ "success": false,+ "error": "Unable to create QR code detector"+ ])+ }+ return+ }- for feature in features {- guard let qrFeature = feature as? CIQRCodeFeature,- let messageString = qrFeature.messageString else {- continue+ let ciImage = CIImage(cgImage: cgImage)+ let features = qrDetector.features(in: ciImage)++ let imageWidth = CGFloat(cgImage.width)+ let imageHeight = CGFloat(cgImage.height)++ var detections: [[String: Any]] = []++ for feature in features {+ guard let qrFeature = feature as? CIQRCodeFeature,+ let messageString = qrFeature.messageString else {+ continue+ }++ // Core Image has origin at bottom-left with y-up.+ // Convert to normalized [0,1] coords with origin top-left, y-down.+ let minX = min(qrFeature.topLeft.x, qrFeature.bottomLeft.x)+ let maxX = max(qrFeature.topRight.x, qrFeature.bottomRight.x)+ // In CI coords, topLeft.y > bottomLeft.y (y goes up)+ let minYci = min(qrFeature.bottomLeft.y, qrFeature.bottomRight.y)+ let maxYci = max(qrFeature.topLeft.y, qrFeature.topRight.y)++ // Add padding around finder patterns (matching Android's 15%)+ let padX = (maxX - minX) * 0.15+ let padY = (maxYci - minYci) * 0.15+ let paddedMinX = max(minX - padX, 0)+ let paddedMaxX = min(maxX + padX, imageWidth)+ let paddedMinYci = max(minYci - padY, 0)+ let paddedMaxYci = min(maxYci + padY, imageHeight)++ // Flip y: top-left origin+ let normX = paddedMinX / imageWidth+ let normY = 1.0 - (paddedMaxYci / imageHeight)+ let normW = (paddedMaxX - paddedMinX) / imageWidth+ let normH = (paddedMaxYci - paddedMinYci) / imageHeight++ detections.append([+ "content": messageString,+ "x": Double(normX),+ "y": Double(normY),+ "width": Double(normW),+ "height": Double(normH),+ ])}- // Core Image has origin at bottom-left with y-up.- // Convert to normalized [0,1] coords with origin top-left, y-down.- let minX = min(qrFeature.topLeft.x, qrFeature.bottomLeft.x)- let maxX = max(qrFeature.topRight.x, qrFeature.bottomRight.x)- // In CI coords, topLeft.y > bottomLeft.y (y goes up)- let minYci = min(qrFeature.bottomLeft.y, qrFeature.bottomRight.y)- let maxYci = max(qrFeature.topLeft.y, qrFeature.topRight.y)-- // Add padding around finder patterns (matching Android's 15%)- let padX = (maxX - minX) * 0.15- let padY = (maxYci - minYci) * 0.15- let paddedMinX = max(minX - padX, 0)- let paddedMaxX = min(maxX + padX, imageWidth)- let paddedMinYci = max(minYci - padY, 0)- let paddedMaxYci = min(maxYci + padY, imageHeight)-- // Flip y: top-left origin- let normX = paddedMinX / imageWidth- let normY = 1.0 - (paddedMaxYci / imageHeight)- let normW = (paddedMaxX - paddedMinX) / imageWidth- let normH = (paddedMaxYci - paddedMinYci) / imageHeight-- detections.append([- "content": messageString,- "x": Double(normX),- "y": Double(normY),- "width": Double(normW),- "height": Double(normH),- ])- }+ let response: [String: Any]+ if detections.isEmpty {+ response = [+ "success": false,+ "error": "No QR code found in image"+ ]+ } else {+ response = [+ "success": true,+ "detections": detections+ ]+ }- if detections.isEmpty {- result([- "success": false,- "error": "No QR code found in image"- ])- } else {- result([- "success": true,- "detections": detections- ])+ DispatchQueue.main.async {+ result(response)+ }}}}