Swift Generate a Qrcode

How to generate a QR image in iOS app using swift

Here is the code for generating the QR code with logo image in centre:

import UIKit
import EventKit
import CoreImage

class ViewController: UIViewController {

@IBOutlet weak var imageView: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()

guard let qrURLImage = URL(string: "https://www.thepramodkumar.com/")?.qrImage(using: #colorLiteral(red: 0.3490196078, green: 0.768627451, blue: 0.6823529412, alpha: 1), logo: #imageLiteral(resourceName: "logo")) else { return }

imageView.image = qrURLImage

}
}

extension URL {

/// Creates a QR code for the current URL in the given color.
func qrImage(using color: UIColor, logo: UIImage? = nil) -> UIImage? {

guard let tintedQRImage = qrImage?.tinted(using: color) else {
return nil
}

guard let logo = logo?.cgImage else {
return UIImage(ciImage: tintedQRImage)
}

guard let final = tintedQRImage.combined(with: CIImage(cgImage: logo)) else {
return UIImage(ciImage: tintedQRImage)
}

return UIImage(ciImage: final)
}

/// Returns a black and white QR code for this URL.
var qrImage: CIImage? {
guard let qrFilter = CIFilter(name: "CIQRCodeGenerator") else { return nil }
let qrData = absoluteString.data(using: String.Encoding.ascii)
qrFilter.setValue(qrData, forKey: "inputMessage")

let qrTransform = CGAffineTransform(scaleX: 12, y: 12)
return qrFilter.outputImage?.transformed(by: qrTransform)
}
}

extension CIImage {
/// Inverts the colors and creates a transparent image by converting the mask to alpha.
/// Input image should be black and white.
var transparent: CIImage? {
return inverted?.blackTransparent
}

/// Inverts the colors.
var inverted: CIImage? {
guard let invertedColorFilter = CIFilter(name: "CIColorInvert") else { return nil }

invertedColorFilter.setValue(self, forKey: "inputImage")
return invertedColorFilter.outputImage
}

/// Converts all black to transparent.
var blackTransparent: CIImage? {
guard let blackTransparentFilter = CIFilter(name: "CIMaskToAlpha") else { return nil }
blackTransparentFilter.setValue(self, forKey: "inputImage")
return blackTransparentFilter.outputImage
}

/// Applies the given color as a tint color.
func tinted(using color: UIColor) -> CIImage? {
guard
let transparentQRImage = transparent,
let filter = CIFilter(name: "CIMultiplyCompositing"),
let colorFilter = CIFilter(name: "CIConstantColorGenerator") else { return nil }

let ciColor = CIColor(color: color)
colorFilter.setValue(ciColor, forKey: kCIInputColorKey)
let colorImage = colorFilter.outputImage

filter.setValue(colorImage, forKey: kCIInputImageKey)
filter.setValue(transparentQRImage, forKey: kCIInputBackgroundImageKey)

return filter.outputImage!
}
}

extension CIImage {

/// Combines the current image with the given image centered.
func combined(with image: CIImage) -> CIImage? {
guard let combinedFilter = CIFilter(name: "CISourceOverCompositing") else { return nil }
let centerTransform = CGAffineTransform(translationX: extent.midX - (image.extent.size.width / 2), y: extent.midY - (image.extent.size.height / 2))
combinedFilter.setValue(image.transformed(by: centerTransform), forKey: "inputImage")
combinedFilter.setValue(self, forKey: "inputBackgroundImage")
return combinedFilter.outputImage!
}
}

Github Repo : https://github.com/bestiosdeveloper/QACodeDemo

How to generate QRCode image with parameters in swift?

You say you need a QR reader, but here you are solely talking about QR generation. Those are two different topics.

In terms of QR generation, you just need to put your four values in the QR payload. Right now you’re just passing a string literal, but you can just update that string to include your four properties in whatever easily decoded format you want.

That having been said, when writing apps like this, you often want to able to scan your QR code not only from within the app, but also any QR scanning app, such as the built in Camera app, and have it open your app. That influences how you might want to encode your payload.

The typical answer would be to make your QR code payload be a URL, using, for example, a universal link. See Supporting Universal Links in Your App. So, first focus on enabling universal links.

Once you’ve got the universal links working (not using QR codes at all, initially), the question then becomes how one would programmatically create the universal link that you’d supply to your QR generator routine, above. For that URLComponents is a great tool for encoding URLs. For example, see Swift GET request with parameters. Just use your universal link for the host used in the URL.


FWIW, while I suggest just encoding a universal link URL into your QR code, above, another option would be some other deep linking pattern, such as branch.io.

How to generate a QRCode with number of details

Try to convert the details from Dictionary into JSON data and then set it to CIFilter.

var jsonDict = [String: Any]()
jsonDict.updateValue("Your_Name", forKey: "name")
jsonDict.updateValue("1234567890", forKey: "number")
guard let jsonData = try? JSONSerialization.data(withJSONObject: jsonDict, options: [.prettyPrinted]) else {
return
}
let filter = CIFilter(name: "CIQRCodeGenerator")
filter?.setValue(jsonData, forKey: "InputMessage")

Can I generate a QR code that contains both URL and text values?

You can add the data you want as a query parameter on the QR code's URL, unless there are privacy issues with the data you're appending to the URL.

How do I clearly display the QR code in swift5?

Try this out buddy.

 func generateQRCode(from string: String) -> UIImage? {
let data = string.data(using: String.Encoding.ascii)

if let filter = CIFilter(name: "CIQRCodeGenerator") {
filter.setValue(data, forKey: "inputMessage")
let transform = CGAffineTransform(scaleX: 3, y: 3)

if let output = filter.outputImage?.transformed(by: transform) {
return UIImage(ciImage: output)
}
}

return nil
}

let image = generateQRCode(from: "Game of Thrones")

How to create and Implement basic login with QRcode?

The problem is in the following lines of code:

let idfromQR = object.stringValue
let viewController = MonitorimiViewController()
viewController.id = idfromQR!

let controller = self.storyboard?.instantiateViewController(withIdentifier: "tabBarController")

controller?.modalTransitionStyle = .flipHorizontal
controller?.modalPresentationStyle = .fullScreen
self.present(controller!, animated: true, completion: nil)

You are assigning the qr code response to a view controller that is never getting presented. So you should instead assign it to one of the tabbar controller's view controllers.

Replace the code above with this code to solve your problem:

guard let tabBarController = self.storyboard?.instantiateViewController(withIdentifier: "tabBarController") as? UITabBarController else {
// Something wrong with your identifier
return
}

(tabBarController.viewControllers[0] as? MonitorimiViewController)?.id = idfromQR!
tabBarController.modalTransitionStyle = .flipHorizontal
tabBarController.modalPresentationStyle = .fullScreen
self.present(tabBarController, animated: true, completion: nil)

This assumes that MonitorimiViewController is the first index view controller in your tab bar controller, if it is not just change the 0 index to another number.



Related Topics



Leave a reply



Submit