Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the color of my QR Code

Tags:

ios

swift

qr-code

I'm working in an app that should contain a QR code with info, I know how to do that, but I have to set the color of the Qrcode from black to red and I as I understand it should be easy, but I can't find a good answer to make this possible, I'll let my code and my screen of the QrCode, thanks.

import UIKit

class QRCodeViewController: UIViewController {
    @IBOutlet weak var imgQRCode: UIImageView!

    func generateQRCode(from string: String) -> UIImage? {
        let data = string.data(using: String.Encoding.utf8)
        if let filter = CIFilter(name: "CIQRCodeGenerator") {
            filter.setValue(data, forKey: "inputMessage")
            filter.setValue("H", forKey: "inputCorrectionLevel")

            guard let qrCodeImage = filter.outputImage
                else {
                    return nil
            }
            let scaleX = imgQRCode.frame.size.width / qrCodeImage.extent.size.width
            let scaleY = imgQRCode.frame.size.height / qrCodeImage.extent.size.height
            let transform = CGAffineTransform(scaleX: scaleX, y: scaleY)

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

    override func viewDidLoad() {
        super.viewDidLoad()

        let image = generateQRCode(from: "Este es el código generado")
        imgQRCode.image = image
    }
}

QRCODE

like image 844
Del Avatar asked Feb 16 '17 17:02

Del


People also ask

Can you change the look of a QR Code?

If you'd like to edit the external look of your QR Codes, you can do so for both Dynamic and Static QR Codes. QR Codes made with QR Code Generator PRO can be customized with colors, logos, different edge styles, patterns, and custom frames with an editable CTA (call to action).


1 Answers

Change your code to something like this.

func generateQRCode(from string: String) -> UIImage? {
    let data = string.data(using: String.Encoding.utf8)
    if let filter = CIFilter(name: "CIQRCodeGenerator") {
        guard let colorFilter = CIFilter(name: "CIFalseColor") else { return nil }

        filter.setValue(data, forKey: "inputMessage")

        filter.setValue("H", forKey: "inputCorrectionLevel")
        colorFilter.setValue(filter.outputImage, forKey: "inputImage")
        colorFilter.setValue(CIColor(red: 1, green: 1, blue: 1), forKey: "inputColor1") // Background white
        colorFilter.setValue(CIColor(red: 1, green: 0, blue: 0), forKey: "inputColor0") // Foreground or the barcode RED
        guard let qrCodeImage = colorFilter.outputImage
            else {
                return nil
        }
        let scaleX = imgQRCode.frame.size.width / qrCodeImage.extent.size.width
        let scaleY = imgQRCode.frame.size.height / qrCodeImage.extent.size.height
        let transform = CGAffineTransform(scaleX: scaleX, y: scaleY)


        if let output = colorFilter.outputImage?.applying(transform) {
            return UIImage(ciImage: output)
        }
    }
    return nil
}

UPDATE need for Swift 3/4

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

enter image description here

like image 132
MwcsMac Avatar answered Oct 03 '22 02:10

MwcsMac