gpt4 book ai didi

macos - 如何更改 CIFilter CIQRCodeGenerator 过滤器的背景色和前景色

转载 作者:行者123 更新时间:2023-12-03 16:05:00 24 4
gpt4 key购买 nike

我正在尝试为 OS X 制作一个 QR 码生成器,但我还没有成功制作出比黑白 QR 码更丰富多彩的 QR 码,我正在使用 CIImage Filters 的 CIQRCodeGenerator我完成了这项工作,我有一个示例代码,已在我的应用程序中实现:-

+ (NSImage *)createQRImageForString:(NSString *)string size:(CGSize)size {
// Setup the QR filter with our string
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[filter setDefaults];

NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
[filter setValue:data forKey:@"inputMessage"];
CIImage *image = [filter valueForKey:@"outputImage"];

// Calculate the size of the generated image and the scale for the desired image size
CGRect extent = CGRectIntegral(image.extent);
CGFloat scale = MIN(size.width / CGRectGetWidth(extent), size.height / CGRectGetHeight(extent));

// Since CoreImage nicely interpolates, we need to create a bitmap image that we'll draw into
// a bitmap context at the desired size;
size_t width = CGRectGetWidth(extent) * scale;
size_t height = CGRectGetHeight(extent) * scale;
CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 256*4, cs, (CGBitmapInfo)kCGImageAlphaPremultipliedFirst);

#if TARGET_OS_IPHONE
CIContext *context = [CIContext contextWithOptions:nil];
#else
CIContext *context = [CIContext contextWithCGContext:bitmapRef options:nil];
#endif

CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];

CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, extent, bitmapImage);

// Create an image with the contents of our bitmap
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);

// Cleanup
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);

return [[NSImage alloc] initWithCGImage:scaledImage size:NSZeroSize];
}

有人可以给我指出正确的方向吗?

最佳答案

这是一个 swift 示例(供 Google 用户使用)

func qrCode(from string: String) -> UIImage? {
let data = string.data(using: .ascii)

// Generate the code image with CIFilter
guard let filter = CIFilter(name: "CIQRCodeGenerator") else { return nil }
filter.setValue(data, forKey: "inputMessage")

// Scale it up (because it is generated as a tiny image)
let scale = UIScreen.main.scale
let transform = CGAffineTransform(scaleX: scale, y: scale)
guard let output = filter.outputImage?.transformed(by: transform) else { return nil }

// Change the color using CIFilter
let colorParameters = [
"inputColor0": CIColor(color: UIColor.black), // Foreground
"inputColor1": CIColor(color: UIColor.clear) // Background
]
let colored = output.applyingFilter("CIFalseColor", parameters: colorParameters)

return UIImage(ciImage: colored)
}

关于macos - 如何更改 CIFilter CIQRCodeGenerator 过滤器的背景色和前景色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24210185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com