Skip to content

Commit

Permalink
Merge pull request #6360 from vector-im/andy/6358_cross_sign
Browse files Browse the repository at this point in the history
Use ZXing library to generate QR codes
  • Loading branch information
Anderas authored Jun 29, 2022
2 parents b0fc49e + 10dda21 commit 8890369
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
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

0 comments on commit 8890369

Please sign in to comment.