From a77946f23adbc46ea4be6dce323ebe4f5b8bac8f Mon Sep 17 00:00:00 2001 From: Robin Genz Date: Thu, 4 May 2023 19:50:51 +0200 Subject: [PATCH] fix(barcode-scanning): `scan(...)` method does not detect any barcodes on iOS --- .changeset/eighty-toes-return.md | 5 +++++ .../ios/Plugin/BarcodeScannerHelper.swift | 14 +++++++------- .../ios/Plugin/BarcodeScannerPlugin.swift | 2 +- .../ios/Plugin/BarcodeScannerView.swift | 2 +- 4 files changed, 14 insertions(+), 9 deletions(-) create mode 100644 .changeset/eighty-toes-return.md diff --git a/.changeset/eighty-toes-return.md b/.changeset/eighty-toes-return.md new file mode 100644 index 0000000..3983207 --- /dev/null +++ b/.changeset/eighty-toes-return.md @@ -0,0 +1,5 @@ +--- +'@capacitor-mlkit/barcode-scanning': patch +--- + +fix(ios): `scan(...)` method does not detect any barcodes diff --git a/packages/barcode-scanning/ios/Plugin/BarcodeScannerHelper.swift b/packages/barcode-scanning/ios/Plugin/BarcodeScannerHelper.swift index 48367d7..47f38da 100644 --- a/packages/barcode-scanning/ios/Plugin/BarcodeScannerHelper.swift +++ b/packages/barcode-scanning/ios/Plugin/BarcodeScannerHelper.swift @@ -8,18 +8,18 @@ import MLKitBarcodeScanning // swiftlint:disable cyclomatic_complexity public class BarcodeScannerHelper { // swiftlint:disable identifier_name - public static func normalizeCornerPoints(cornerPoints: [NSValue], imageSize: CGSize) -> [NSValue] { + public static func normalizeCornerPoints(cornerPoints: [NSValue], imageSize: CGSize, scale: CGFloat = UIScreen.main.scale) -> [NSValue] { let screenSize: CGRect = UIScreen.main.bounds let imageWidth = imageSize.width let imageHeight = imageSize.height let isPortrait = UIDevice.current.orientation == .portrait || UIDevice.current.orientation == .portraitUpsideDown var normalizedCornerPoints = [NSValue]() for cornerPoint in cornerPoints { - var x = Int((cornerPoint.cgPointValue.x / CGFloat(imageWidth)) * screenSize.width * UIScreen.main.scale) - var y = Int((cornerPoint.cgPointValue.y / CGFloat(imageHeight)) * screenSize.height * UIScreen.main.scale) + var x = Int((cornerPoint.cgPointValue.x / CGFloat(imageWidth)) * screenSize.width * scale) + var y = Int((cornerPoint.cgPointValue.y / CGFloat(imageHeight)) * screenSize.height * scale) if isPortrait { - x = Int((1 - (cornerPoint.cgPointValue.y / CGFloat(imageHeight))) * screenSize.width * UIScreen.main.scale) - y = Int((cornerPoint.cgPointValue.x / CGFloat(imageWidth)) * screenSize.height * UIScreen.main.scale) + x = Int((1 - (cornerPoint.cgPointValue.y / CGFloat(imageHeight))) * screenSize.width * scale) + y = Int((cornerPoint.cgPointValue.x / CGFloat(imageWidth)) * screenSize.height * scale) } let point = CGPoint(x: x, y: y) let value = NSValue(cgPoint: point) @@ -28,10 +28,10 @@ public class BarcodeScannerHelper { return normalizedCornerPoints } - public static func createBarcodeResultForBarcode(_ barcode: Barcode, imageSize: CGSize?) -> JSObject { + public static func createBarcodeResultForBarcode(_ barcode: Barcode, imageSize: CGSize?, scale: CGFloat = UIScreen.main.scale) -> JSObject { var cornerPointsResult = [[Int]]() if let cornerPoints = barcode.cornerPoints, let imageSize = imageSize { - let normalizedCornerPoints = normalizeCornerPoints(cornerPoints: cornerPoints, imageSize: imageSize) + let normalizedCornerPoints = normalizeCornerPoints(cornerPoints: cornerPoints, imageSize: imageSize, scale: scale) for cornerPoint in normalizedCornerPoints { var value = [Int]() value.append(Int(cornerPoint.cgPointValue.x)) diff --git a/packages/barcode-scanning/ios/Plugin/BarcodeScannerPlugin.swift b/packages/barcode-scanning/ios/Plugin/BarcodeScannerPlugin.swift index 412fde3..22468da 100644 --- a/packages/barcode-scanning/ios/Plugin/BarcodeScannerPlugin.swift +++ b/packages/barcode-scanning/ios/Plugin/BarcodeScannerPlugin.swift @@ -113,7 +113,7 @@ public class BarcodeScannerPlugin: CAPPlugin { } var barcodeResults = JSArray() for barcode in barcodes ?? [] { - barcodeResults.append(BarcodeScannerHelper.createBarcodeResultForBarcode(barcode, imageSize: nil)) + barcodeResults.append(BarcodeScannerHelper.createBarcodeResultForBarcode(barcode, imageSize: nil, scale: 1)) } call.resolve([ "barcodes": barcodeResults diff --git a/packages/barcode-scanning/ios/Plugin/BarcodeScannerView.swift b/packages/barcode-scanning/ios/Plugin/BarcodeScannerView.swift index f7852fe..29fd2bf 100644 --- a/packages/barcode-scanning/ios/Plugin/BarcodeScannerView.swift +++ b/packages/barcode-scanning/ios/Plugin/BarcodeScannerView.swift @@ -263,7 +263,7 @@ public protocol BarcodeScannerViewDelegate { return barcodes.filter { barcode in if let cornerPoints = barcode.cornerPoints, let imageSize = imageSize { let normalizedCornerPoints = BarcodeScannerHelper.normalizeCornerPoints(cornerPoints: cornerPoints, - imageSize: imageSize) + imageSize: imageSize, scale: 1) let topLeft = normalizedCornerPoints[0].cgPointValue let topRight = normalizedCornerPoints[1].cgPointValue