核心图像颜色的颜色和图案
Colorwithpattern for coreimage color
我正在使用 CIAztecCodeGenerator 生成 Aztec 代码。
我正在尝试为其前景色设置一个图案而不是纯色,但是它呈现为blank/white我想知道是否有人知道我做错了什么。
[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
请注意,两个缩放步骤中的数字取决于您要显示代码的大小以及图案图像在代码上方的缩放方式。
我正在使用 CIAztecCodeGenerator 生成 Aztec 代码。
我正在尝试为其前景色设置一个图案而不是纯色,但是它呈现为blank/white我想知道是否有人知道我做错了什么。
[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
请注意,两个缩放步骤中的数字取决于您要显示代码的大小以及图案图像在代码上方的缩放方式。