Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ZXing library to generate QR codes #6360

Merged
merged 1 commit into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

import UIKit
import MatrixSDK

final class KeyVerificationVerifyByScanningViewController: UIViewController {

Expand Down Expand Up @@ -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) {
Expand Down
41 changes: 17 additions & 24 deletions Riot/Modules/QRCode/QRCodeGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
1 change: 1 addition & 0 deletions changelog.d/6358.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cross-Signing: Use ZXing library to generate QR codes