From 10dda21dab2f506fbd212c455368ee0618c041ff Mon Sep 17 00:00:00 2001 From: Andy Uhnak Date: Tue, 28 Jun 2022 18:11:22 +0100 Subject: [PATCH] Use ZXing library to generate QR codes --- ...cationVerifyByScanningViewController.swift | 8 +++- Riot/Modules/QRCode/QRCodeGenerator.swift | 41 ++++++++----------- changelog.d/6358.bugfix | 1 + 3 files changed, 25 insertions(+), 25 deletions(-) create mode 100644 changelog.d/6358.bugfix diff --git a/Riot/Modules/KeyVerification/Common/Verify/Scanning/KeyVerificationVerifyByScanningViewController.swift b/Riot/Modules/KeyVerification/Common/Verify/Scanning/KeyVerificationVerifyByScanningViewController.swift index c68e4f3586..c56d0fc0e0 100644 --- a/Riot/Modules/KeyVerification/Common/Verify/Scanning/KeyVerificationVerifyByScanningViewController.swift +++ b/Riot/Modules/KeyVerification/Common/Verify/Scanning/KeyVerificationVerifyByScanningViewController.swift @@ -17,6 +17,7 @@ */ import UIKit +import MatrixSDK final class KeyVerificationVerifyByScanningViewController: UIViewController { @@ -215,7 +216,12 @@ final class KeyVerificationVerifyByScanningViewController: UIViewController { private func qrCodeImage(from data: Data) -> UIImage? { let codeGenerator = QRCodeGenerator() - return codeGenerator.generateCode(from: data, with: self.codeImageView.frame.size) + do { + return try codeGenerator.generateCode(from: data, with: codeImageView.frame.size) + } catch { + MXLog.error("[KeyVerificationVerifyByScanningViewController] qrCodeImage: cannot generate QR code - \(error)") + return nil + } } private func presentQRCodeReader(animated: Bool) { diff --git a/Riot/Modules/QRCode/QRCodeGenerator.swift b/Riot/Modules/QRCode/QRCodeGenerator.swift index f3ad32c054..4ac8e6f6ed 100644 --- a/Riot/Modules/QRCode/QRCodeGenerator.swift +++ b/Riot/Modules/QRCode/QRCodeGenerator.swift @@ -15,36 +15,29 @@ */ import Foundation +import ZXingObjC final class QRCodeGenerator { - - // MARK: - Constants - - private enum Constants { - static let qrCodeGeneratorFilter = "CIQRCodeGenerator" - static let qrCodeInputCorrectionLevel = "M" + enum Error: Swift.Error { + case cannotCreateImage } - // MARK: - Public - - func generateCode(from data: Data, with size: CGSize) -> UIImage? { - guard let filter = CIFilter(name: Constants.qrCodeGeneratorFilter) else { - return nil - } - - filter.setValue(data, forKey: "inputMessage") - filter.setValue(Constants.qrCodeInputCorrectionLevel, forKey: "inputCorrectionLevel") // Be sure to use same error resilience level as other platform + func generateCode(from data: Data, with size: CGSize) throws -> UIImage { + let writer = ZXMultiFormatWriter() + let endodedString = String(data: data, encoding: .isoLatin1) + let scale = UIScreen.main.scale + let bitMatrix = try writer.encode( + endodedString, + format: kBarcodeFormatQRCode, + width: Int32(size.width * scale), + height: Int32(size.height * scale), + hints: ZXEncodeHints() + ) - guard let ciImage = filter.outputImage else { - return nil + guard let cgImage = ZXImage(matrix: bitMatrix).cgimage else { + throw Error.cannotCreateImage } - let scaleX = size.width/ciImage.extent.size.width - let scaleY = size.height/ciImage.extent.size.height - - let transform = CGAffineTransform(scaleX: scaleX, y: scaleY) - - let transformedCIImage = ciImage.transformed(by: transform) - return UIImage(ciImage: transformedCIImage) + return UIImage(cgImage: cgImage) } } diff --git a/changelog.d/6358.bugfix b/changelog.d/6358.bugfix new file mode 100644 index 0000000000..a0e99dd206 --- /dev/null +++ b/changelog.d/6358.bugfix @@ -0,0 +1 @@ +Cross-Signing: Use ZXing library to generate QR codes