π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Revert "Move iOS QR detection off main thread"
Amrithesh Kakkoth Ente6fa9442a93d6
diff --git a/mobile/apps/auth/plugins/qr/ios/Classes/EnteQrPlugin.swift b/mobile/apps/auth/plugins/qr/ios/Classes/EnteQrPlugin.swiftindex 6903a921e6..7a94d2fbe5 100644--- a/mobile/apps/auth/plugins/qr/ios/Classes/EnteQrPlugin.swift+++ b/mobile/apps/auth/plugins/qr/ios/Classes/EnteQrPlugin.swift@@ -2,8 +2,6 @@ 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()@@ -40,163 +38,137 @@ public class EnteQrPlugin: NSObject, FlutterPlugin {result(FlutterMethodNotImplemented)}}-+private func scanQrCode(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- }+ 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"+ ])+ }+ }- guard let cgImage = image.cgImage else {- DispatchQueue.main.async {- result([- "success": false,- "error": "Unable to get CGImage from UIImage"- ])- }- 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+ }- 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- }+ guard let cgImage = image.cgImage else {+ result([+ "success": false,+ "error": "Unable to get CGImage from UIImage"+ ])+ return+ }- 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"- ]- }+ let detector = CIDetector(ofType: CIDetectorTypeQRCode,+ context: nil,+ options: [CIDetectorAccuracy: CIDetectorAccuracyHigh])- DispatchQueue.main.async {- result(response)- }+ guard let qrDetector = detector else {+ result([+ "success": false,+ "error": "Unable to create QR code detector"+ ])+ return}- }- 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 ciImage = CIImage(cgImage: cgImage)+ let features = qrDetector.features(in: ciImage)- guard let cgImage = image.cgImage else {- DispatchQueue.main.async {- result([- "success": false,- "error": "Unable to get CGImage from UIImage"- ])- }- return- }+ let imageWidth = CGFloat(cgImage.width)+ let imageHeight = CGFloat(cgImage.height)- 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- }+ var detections: [[String: Any]] = []- 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),- ])+ for feature in features {+ guard let qrFeature = feature as? CIQRCodeFeature,+ let messageString = qrFeature.messageString else {+ continue}- let response: [String: Any]- if detections.isEmpty {- response = [- "success": false,- "error": "No QR code found in image"- ]- } else {- response = [- "success": true,- "detections": detections- ]- }+ // 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),+ ])+ }- DispatchQueue.main.async {- result(response)- }+ if detections.isEmpty {+ result([+ "success": false,+ "error": "No QR code found in image"+ ])+ } else {+ result([+ "success": true,+ "detections": detections+ ])}}}