0

I create one QRCode Generator with deferent color I want to remove the Gray color in Frame and have really one white color or clear color after I use the filter this gray color generate some time

https://imgur.com/wKEkQ4v enter image description here

let data = string.data(using: .isoLatin1, allowLossyConversion: false)
        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(color: UIColor.clear), forKey: "inputColor1")
            colorFilter.setValue(CIColor(color: UIColor.black), forKey: "inputColor0")

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


            if let output = colorFilter.outputImage?.transformed(by: transform) {



                let image = convert(cmage:(output.transformed(by: CGAffineTransform(scaleX: scaleX, y: scaleY))))
                return image

  }
        }
        return nil
}

func convert(cmage:CIImage) -> UIImage
    {
        let context:CIContext = CIContext.init(options: nil)
        let cgImage:CGImage = context.createCGImage(cmage, from: cmage.extent)!
        let image:UIImage = UIImage.init(cgImage: cgImage)
        return image
    }

if I don't use the filter for change the color I don't have this problem or If I used the blue color I have one frame with aqua blue color

MidDev
  • 182
  • 1
  • 15
  • why would you need to use a filter `CIFalseColor` after generating your QRCode? What are you trying to achieve? I would simply remove the CIFalseColor and apply a simple scale transform to the QRCode image generated by the qr code filter. – Leo Dabus Jan 06 '19 at 17:27
  • Btw CIQRCodeGenerator working code sample here https://stackoverflow.com/a/51181064/2303865 – Leo Dabus Jan 06 '19 at 17:30
  • @LeoDabus I want to change color to blue qr code or red etc... but in all the qr code frame I received the same problems in frame – MidDev Jan 06 '19 at 17:31
  • @LeoDabus I want to change the qr code color – MidDev Jan 06 '19 at 17:33
  • I didn't understand your question, your picture is white and already have the grey shadow – Leo Dabus Jan 06 '19 at 17:33
  • @LeoDabus I want to create the arcade with deferent color but I don't want this shadow frame – MidDev Jan 06 '19 at 17:34
  • looks like the problem is in your qrcode generator. try using the one I posted – Leo Dabus Jan 06 '19 at 17:41
  • @LeoDabus in your posted code you can't change the qr code color I want to change but I don't need a shadow – MidDev Jan 06 '19 at 17:50

1 Answers1

2

Using the extension from the link I posted as a starting point:

extension String {
    func qrCode(background: UIColor = .white, color: UIColor = .black, output: CGSize = CGSize(width: 250, height: 250))-> UIImage? {
        guard
            let data = data(using: .isoLatin1),
            let filter = CIFilter(name: "CIQRCodeGenerator")
            else { return nil }
        filter.setValue(data, forKey: "inputMessage")
        filter.setValue("M", forKey: "inputCorrectionLevel")
        guard let image = filter.outputImage
            else { return nil }
        let size = image.extent.integral
        let matrix = CGAffineTransform(scaleX: output.width / size.width, y: output.height / size.height)
        UIGraphicsBeginImageContextWithOptions(output, false, 0)
        defer { UIGraphicsEndImageContext() }
        guard
            let colorFilter = CIFilter(name: "CIFalseColor",
                                   parameters: ["inputImage" : image.transformed(by: matrix),
                                                "inputColor1": CIColor(color: background) ,
                                                "inputColor0": CIColor(color: color)]),
            let coloredImage = colorFilter.outputImage
        else { return nil }
        UIGraphicsBeginImageContextWithOptions(output, false, 0)
        defer { UIGraphicsEndImageContext() }
        UIImage(ciImage: coloredImage).draw(in: CGRect(origin: .zero, size: output))
        return UIGraphicsGetImageFromCurrentImageContext()
    }
}


let link = "https://stackoverflow.com/questions/51178573/swift-image-data-from-ciimage-qr-code-how-to-render-cifilter-output?noredirect=1"
if let coloredQRCode = link.qrCode(color: .red, output: CGSize(width: 500, height: 500)) {
    coloredQRCode
}

enter image description here

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571