微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

核心图像颜色的 Colorwithpattern

如何解决核心图像颜色的 Colorwithpattern

我正在使用 CIAztecCodeGenerator 生成 Aztec 代码

我正在尝试为其前景色设置图案而不是纯色,但是它呈现为空白/白色我想知道是否有人知道我做错了什么。

[colorFilter setValue:[CIColor colorWithCGColor:[[UIColor colorWithPatternImage:image] CGColor]] forKey:@"inputColor0"];

解决方法

这有点复杂,但您可以通过以某种方式组合和混合图像来使用自定义模式:

import CoreImage.CIFilterBuiltins // needed for using the type-safe filter interface

// ...

let patternImage = UIImage(named: "<image_name>")!
var patternInput = CIImage(cgImage: patternImage.cgImage!)

// potentially scale the pattern image
patternInput = patternInput.transformed(by: CGAffineTransform(scaleX: 0.5,y: 0.5))

let codeFilter = CIFilter.aztecCodeGenerator()
codeFilter.message = "<message>".data(using: .utf8)!
var codeImage = codeFilter.outputImage!

// invert code so the actual code part is white
let colorInvertFilter = CIFilter.colorInvert()
colorInvertFilter.inputImage = codeImage
codeImage = colorInvertFilter.outputImage!

// potentially scale the barcode (using nearest sampling to retain sharp edges)
codeImage = codeImage.samplingNearest().transformed(by: CGAffineTransform(scaleX: 50,y: 50))

let blendFilter = CIFilter.blendWithMask()
blendFilter.inputImage = patternInput
// using white color as background here,but can be any (or transparent when alpha = 0)
blendFilter.backgroundImage = CIImage(color: CIColor.white).cropped(to: codeImage.extent)
blendFilter.maskImage = codeImage

let output = blendFilter.outputImage!

Objective-C 版本:

// the image containing the pattern you want to show over the aztec code
CIImage *patternImage = [CIImage imageWithData:imageData];

// potentially scale the pattern image,if necessary
patternImage = [patternImage imageByApplyingTransform:CGAffineTransformMakeScale(0.5,0.5)];

// generate aztec code image
CIFilter *qrFilter = [CIFilter filterWithName:@"CIAztecCodeGenerator"];
[qrFilter setValue:stringData forKey:@"inputMessage"];
CIImage *codeImage = qrFilter.outputImage;

// invert code so the actual code part is white (used for masking below)
codeImage = [codeImage imageByApplyingFilter: @"CIColorInvert"];

// potentially scale the aztec code (using nearest sampling to retain sharp edges)
codeImage = [[codeImage imageBySamplingNearest] imageByApplyingTransform:CGAffineTransformMakeScale(50,50)];

// the background color for your aztec code; basically a solid color image of the same size of the code
CIImage *background = [[CIImage imageWithColor:[CIColor whiteColor]] imageByCroppingToRect:codeImage.extent];

//
CIFilter *blendFilter = [CIFilter filterWithName:@"CIBlendWithMask"];
[blendFilter setValue:patternImage forKey:@"inputImage"]; // the pattern image is in the foreground
[blendFilter setValue:background forKey:@"backgroundImage"]; // solid color in the aztec code,but could be any color or image
[blendFilter setValue:codeImage forKey:@"maskImage"]; // use the aztec code as a mask for the pattern image over the background

CIImage *output = blendFilter.outputImage

请注意,两个缩放步骤中的数字取决于您想要显示代码的大小以及应如何在代码上方缩放图案图像。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。