import UIKit
import CoreImage
YXFilter 是CIFilter的子类
// YXFilter : CIFilter {}
添加枚举区分二维码和条码
extension YXFilter {
@objc public enum QrCodeType:Int {
//二维码
case qr = 1
//条形码
case bar = 2
}
}
生成二维码或者条形码
/// 生成二维码
/// - Parameters:
/// - qrContent: 二维码内容
/// - size: 大小
/// - codeType: qr 二维码 bar 条码
/// - Returns: 目标图片
@objc public class func filterToCreatQrCode(qrContent:String,size:CGFloat,codeType:YXFilter.QrCodeType) -> UIImage? {
var name:String = ""
if codeType == .bar {
name = "CICode128BarcodeGenerator"
}
if codeType == .qr {
name = "CIQRCodeGenerator"
}
let filter:YXFilter = YXFilter.init(name: name)!
guard let data:Data = qrContent.data(using: .utf8) else {
return nil
}
//设置内容
filter.setValue(data, forKey: "inputMessage")
if codeType == .qr {
//设置纠错级别 L 20%、M 37%、Q 55%、H 65%
filter.setValue("M", forKey: "inputCorrectionLevel")
}
if codeType == .bar {
filter.setValue(5, forKey: "inputQuietSpace")
filter.setValue(size, forKey: "inputBarcodeHeight")
}
guard let cioutImage = filter.outputImage else {
return nil
}
let imageSize = CGSize(width: size, height: size)
let extent:CGRect = cioutImage.extent.integral
let scale = min(imageSize.width/extent.width, imageSize.height/extent.height)
let width:size_t = size_t(extent.width * scale)
let height:size_t = size_t(extent.height * scale)
let cs:CGColorSpace = CGColorSpaceCreateDeviceGray()
guard let cgContent:CGContext = CGContext.init(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 0, space: cs, bitmapInfo: 0) else {
return nil
}
// 关联GPU
guard let glContext:EAGLContext = EAGLContext.init(api: .openGLES3) else {
return nil
}
let cicontext:CIContext = CIContext.init(eaglContext: glContext)
guard let cgimage:CGImage = cicontext.createCGImage(cioutImage, from: extent) else {
return nil
}
cgContent.interpolationQuality = CGInterpolationQuality.none
cgContent.scaleBy(x: scale, y: scale)
cgContent.draw(cgimage, in: extent)
guard let newCgimage = cgContent.makeImage() else {
return nil
}
return UIImage.init(cgImage: newCgimage, scale: scale, orientation: .up)
}
生成指定颜色的二维码
/// 生成二维码
/// - Parameters:
/// - color: 二维码颜色
/// - backColor: 二维码背景色
/// - qrContent: 二维码内容
/// - size: 大小尺寸
/// - codeType: .qr 二维码 .bar 条码
/// - Returns: 目标二维码
@objc public class func filterToCreatQrCodeWithColor(color:UIColor,backColor:UIColor,qrContent:String,size:CGFloat,codeType:YXFilter.QrCodeType) -> UIImage? {
guard let image = self.filterToCreatQrCode(qrContent: qrContent, size: size, codeType: codeType) else {
return nil
}
guard let filter = YXFilter.init(name: "CIFalseColor") else {
return nil
}
guard let ciimage = CIImage.init(image: image) else {
return nil
}
let ciColor = CIColor.init(color: color)
let ciBackColor = CIColor.init(color: backColor)
filter.setValue(ciimage, forKey: "inputImage")
filter.setValue(ciColor, forKey: "inputColor0")
filter.setValue(ciBackColor, forKey: "inputColor1")
guard let outimage = filter.outputImage else {
return nil
}
return UIImage.init(ciImage: outimage, scale: UIScreen.main.scale, orientation: .up)
}
二维码中间添加图片,注意设置二维码纠错级别
/// 生成二维码,中间带图片
/// - Parameters:
/// - centerImage: 二维码中的图片
/// - centerImageSize: 二维码中图片的大小
/// - qrContent: 二维码内容
/// - size: 二维码大小
/// - Returns: 目标二维码图片
@objc public class func filterToCreatQrCodeWithImage(centerImage:UIImage,centerImageSize:CGSize,qrContent:String,size:CGFloat) -> UIImage? {
guard let image = self.filterToCreatQrCode(qrContent: qrContent, size: size, codeType: .qr) else {
return nil
}
let newImageSize = CGSize(width: size, height: size)
let newImageRect = CGRect.init(origin: CGPoint.zero, size: newImageSize)
let centerRcte = CGRect(x: (size - centerImageSize.width)/2, y: (size - centerImageSize.height)/2, width: centerImageSize.width, height: centerImageSize.height)
UIGraphicsBeginImageContextWithOptions(newImageSize, true, image.scale)
image.draw(in: newImageRect)
centerImage.draw(in: centerRcte)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
网友评论