滤镜类 CIFilter
CIFilter是CoreImage中提供的图像过滤器,也可以将其理解为滤镜。许多美颜应用,图像处理应用等都是为原图添加了滤镜效果。系统通过了很多的滤镜效果, 分成多种类别,在CIFilter类中可以看到。
系统通过的过滤效果分类
系统提供了从指定分类中获取各种滤镜效果的方法:
获取各种滤镜的方法// 尝试打印模糊类别过滤器
print(CIFilter.filterNames(inCategory: kCICategoryBlur))
下面处理两种较为常见的用法: 高斯模糊、二维码及条形码生成
1、高斯模糊 处理
func createGaussianBlurImage(_ image: UIImage) -> UIImage? {
guard let ciImage = CIImage(image: image) else { return nil }
// 创建高斯模糊滤镜类
guard let blurFilter = CIFilter(name: "CIGaussianBlur") else { return nil }
// key 可以在控制台打印 po blurFilter.inputKeys
// 设置图片
blurFilter.setValue(ciImage, forKey: "inputImage")
// 设置模糊值
blurFilter.setValue(20, forKey: "inputRadius")
// 从滤镜中 取出图片
guard let outputImage = blurFilter.outputImage else { return nil }
// 创建上下文
let context = CIContext(options: nil)
// 根据滤镜中的图片 创建CGImage
guard let cgImage = context.createCGImage(outputImage, from: ciImage.extent) else { return nil }
return UIImage(cgImage: cgImage)
}
2、二维码生成 处理
包含给二维码添加前景色、背景色、头像操作
/// 生成二维码
func generateQRCode(_ content: String, size: CGFloat, avatar: UIImage?, foregroundColor: UIColor = .black, backgroundColor: UIColor = .white) -> UIImage? {
guard let generateFilter = CIFilter(name: "CIQRCodeGenerator") else { return nil }
// 设置二维码内容
generateFilter.setValue(content.data(using: .utf8), forKey: "inputMessage")
// 设置二维码的级别(纠错率) L: 7% M(默认): 15% Q: 25% H: 30%
generateFilter.setValue("H", forKeyPath: "inputCorrectionLevel")
// 直接返回 UIImage(ciImage: outputImage) 会是模糊的二维码
guard let outputImage = generateFilter.outputImage else { return nil }
// 转化为 清晰的图像
guard let clearImage = generateNonInterpolatedQRCode(outputImage, size: size) else { return nil }
// 设置二维码 颜色
guard let colorsImage = setQRCodeColors(clearImage, foregroundColor: foregroundColor, backgroundColor: backgroundColor) else { return nil}
// 返回插入头像的二维码
return insertAvatarToQRCode(avatar, qrCodeImage: colorsImage)
}
/// 生成清晰的 二维码
func generateNonInterpolatedQRCode(_ ciImage: CIImage, size: CGFloat) -> UIImage? {
// 调整图片大小及位置(小数跳转为整数)位置值向下调整,大小只向上调整
let extent = ciImage.extent.integral
// 获取图片大小
let scale = min(size / extent.width, size / extent.height)
let outputImageWidth = extent.width * scale
let outputImageHeight = extent.height * scale
// 创建依赖于设备的灰度颜色通道
let space = CGColorSpaceCreateDeviceGray()
// 创建图形上下文
let bitmapContext = CGContext(data: nil, width: Int(outputImageWidth), height: Int(outputImageHeight), bitsPerComponent: 8, bytesPerRow: 0, space: space, bitmapInfo: 0)
// 设置缩放
bitmapContext?.scaleBy(x: scale, y: scale)
// 设置上下文渲染等级
bitmapContext?.interpolationQuality = .none
// 上下文
let context = CIContext(options: nil)
// 创建 cgImage
guard let cgImage = context.createCGImage(ciImage, from: extent) else { return nil }
// 绘图
bitmapContext?.draw(cgImage, in: extent)
// 从图形上下文中创建图片
guard let scaledImage = bitmapContext?.makeImage() else { return nil }
// 返回UIImage
return UIImage(cgImage: scaledImage)
}
/// 设置二维码前景色 和背景色
func setQRCodeColors(_ image: UIImage, foregroundColor: UIColor, backgroundColor: UIColor) -> UIImage? {
guard let colorFilter = CIFilter(name: "CIFalseColor") else { return nil }
let ciImage = CIImage(image: image)
// 设置图片
colorFilter.setValue(ciImage, forKey: "inputImage")
// 设置前景色
colorFilter.setValue(CIColor(color: foregroundColor), forKey: "inputColor0")
// 设置背景色
colorFilter.setValue(CIColor(color: backgroundColor), forKey: "inputColor1")
// 输出图片
guard let outputImage = colorFilter.outputImage else { return nil }
return UIImage(ciImage: outputImage)
}
/// 往 二维码中 插入头像
func insertAvatarToQRCode(_ avatar: UIImage?, qrCodeImage: UIImage) -> UIImage? {
guard let avatarSize = avatar?.size else { return qrCodeImage }
let qrCodeSize = qrCodeImage.size
// 开启上下文
UIGraphicsBeginImageContext(qrCodeSize)
// 设置头像的最大值
var maxAvatarSize = min(avatarSize.width, avatarSize.height)
maxAvatarSize = min(qrCodeSize.width / 3, maxAvatarSize)
// 绘制二维码图片
qrCodeImage.draw(in: CGRect(origin: .zero, size: qrCodeSize))
// 绘制头像
avatar?.draw(in: CGRect(x: (qrCodeSize.width - maxAvatarSize) / 2, y: (qrCodeSize.height - maxAvatarSize) / 2, width: maxAvatarSize, height: maxAvatarSize))
// 获取图片
let outputImage = UIGraphicsGetImageFromCurrentImageContext()
// 关闭上下文
UIGraphicsEndImageContext()
return outputImage
}
3、条形码生成
/// 生成条形码
func generateBarcode(_ content: String, size: CGSize) -> UIImage? {
guard let barcodeFilter = CIFilter(name: "CICode128BarcodeGenerator") else { return nil }
// 条形码内容
barcodeFilter.setValue(content.data(using: .utf8), forKey: "inputMessage")
// 左右间距
barcodeFilter.setValue(0, forKey: "inputQuietSpace")
// 高度 -> "inputBarcodeHeight"
guard let outputImage = barcodeFilter.outputImage else { return nil }
// 调整图片大小及位置(小数跳转为整数)位置值向下调整,大小只向上调整
let extent = outputImage.extent.integral
// 条形码放大 处理模糊
let scaleX = size.width / extent.width
let scaleY = size.height / extent.height
let clearImage = UIImage(ciImage: outputImage.transformed(by: CGAffineTransform(scaleX: scaleX, y: scaleY)))
return clearImage
}
/// 往 条形码中插入 文本
func insertTextToBarcode(_ text: String?, attributes: [NSAttributedString.Key: Any]?, height: CGFloat, barcodeImage: UIImage) -> UIImage? {
guard let text = text else { return barcodeImage }
let barcodeSize = barcodeImage.size
// 开启上下文
UIGraphicsBeginImageContext(CGSize(width: barcodeSize.width, height: barcodeSize.height + 20))
// 绘制条形码图片
barcodeImage.draw(in: CGRect(origin: .zero, size: barcodeSize))
// 文本样式
let style = NSMutableParagraphStyle()
style.alignment = .center
let defaultAttributes: [NSAttributedString.Key: Any] = [
.font: UIFont.systemFont(ofSize: 15),
.foregroundColor: UIColor.black,
.kern: 2,
.paragraphStyle: style
]
let attributes = attributes ?? defaultAttributes
// 绘制文本
(text as NSString).draw(in: CGRect(x: 0, y: barcodeSize.height, width: barcodeSize.width, height: height), withAttributes: attributes)
// 获取图片
let outputImage = UIGraphicsGetImageFromCurrentImageContext()
// 关闭上下文
UIGraphicsEndImageContext()
return outputImage
}
二维码识别
// CIDetectorTypeFace 脸部识别
// CIDetectorTypeRectangle 矩形识别
// CIDetectorTypeQRCode 二维码识别
// CIDetectorTypeText 文本识别
guard let image = resultImageView.image else { return }
guard let ciImage = CIImage(image: image) else { return }
// 识别二维码
let detector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy: CIDetectorAccuracyHigh])
let features = detector?.features(in: ciImage)
guard let feature = features?.first as? CIQRCodeFeature else { return }
print(feature.messageString)
参考自:
swift 二维码的生成
iOS开发之CoreImage框架使用(上)
CGContext 和 CIContext
iOS swift语言生成条形码
网友评论