Created
August 31, 2021 13:58
-
-
Save yageek/4c3607a8b62536930146feda043d9089 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CIImage+CodeGenerator.swift | |
// | |
// Created by Dominik Felber on 04.03.16. | |
// Copyright © 2016 Dominik Felber. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
extension CIImage { | |
enum QRInputCorrectionLevel: String { | |
case L = "L" // 7% | |
case M = "M" // 15% | |
case Q = "Q" // 25% | |
case H = "H" // 30% | |
} | |
func transformedImage(_ size: CGFloat) -> CIImage? { | |
guard self.extent.size.width > 0 else { | |
return nil | |
} | |
let scale = size / self.extent.size.width | |
let transformedImage = self.applying(CGAffineTransform(scaleX: scale, y: scale)) | |
return transformedImage | |
} | |
class func createQRCodeImage(for text: String, size: CGFloat, inputCorrectionLevel: QRInputCorrectionLevel = .M) -> CIImage? { | |
guard let filter = CIFilter(name: "CIQRCodeGenerator") else { | |
return nil | |
} | |
let data = text.data(using: .utf8, allowLossyConversion: true) | |
filter.setValue(data, forKey: "inputMessage") | |
filter.setValue(inputCorrectionLevel.rawValue, forKey: "inputCorrectionLevel") | |
return filter.outputImage?.transformedImage(size) | |
} | |
class func createAztecCodeImage(for text: String, size: CGFloat, inputCorrectionLevel: Float = 23.0, inputLayers: Float = 0.0, inputCompactStyle: Float = 0.0) -> CIImage? { | |
guard let filter = CIFilter(name: "CIAztecCodeGenerator") else { | |
return nil | |
} | |
let data = text.data(using: .utf8, allowLossyConversion: true) | |
filter.setValue(data, forKey: "inputMessage") | |
if inputCorrectionLevel >= 5.0 && inputCorrectionLevel <= 95.0 { | |
filter.setValue(inputCorrectionLevel, forKey: "inputCorrectionLevel") | |
} | |
if inputLayers >= 1.0 && inputLayers <= 32.0 { | |
filter.setValue(inputLayers, forKey: "inputLayers") | |
} | |
if inputCompactStyle >= 0.0 && inputCompactStyle <= 1.0 { | |
filter.setValue(inputCompactStyle, forKey: "inputCompactStyle") | |
} | |
return filter.outputImage?.transformedImage(size) | |
} | |
class func createCode128BarcodeImage(for text: String, size: CGFloat, inputQuietSpace: Float = 7.0) -> CIImage? { | |
guard let filter = CIFilter(name: "CICode128BarcodeGenerator") else { | |
return nil | |
} | |
let data = text.data(using: .ascii, allowLossyConversion: true) | |
filter.setValue(data, forKey: "inputMessage") | |
if inputQuietSpace >= 0.0 && inputQuietSpace <= 20.0 { | |
filter.setValue(inputQuietSpace, forKey: "inputQuietSpace") | |
} | |
return filter.outputImage?.transformedImage(size) | |
} | |
class func createPDF417CodeImage(for text: String, size: CGFloat) -> CIImage? { | |
guard let filter = CIFilter(name: "CIPDF417BarcodeGenerator") else { | |
return nil | |
} | |
let data = text.data(using: .ascii, allowLossyConversion: true) | |
filter.setValue(data, forKey: "inputMessage") | |
return filter.outputImage?.transformedImage(size) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment