我试图为OS X制作一个QR码生成器,但是我还没有继续制作一个比我更喜欢使用CIQRCodeGenerator的黑色和白色的QRCode对于CIImage过滤器我将如何完成这项工作我已经在我的应用程序中实现了一个示例代码: -
+ (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];
}
有人可以指出我正确的方向。
答案 0 :(得分:10)
现在有效的代码: -
+ (NSImage *)createQRImageForString:(NSString *)string backgroundColor:(CIColor*)iBackgroundColor foregroundColor:(CIColor*)iForegroundColor size:(CGSize)size {
CIImage *image;
CIFilter *filter;
CIFilter *filterColor;
// Setup the QR filter with our string
filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[filter setDefaults];
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
[filter setValue:data forKey:@"inputMessage"];
image = [filter valueForKey:@"outputImage"];
filterColor = [CIFilter filterWithName:@"CIFalseColor" keysAndValues:@"inputImage", image, @"inputColor0", iForegroundColor, @"inputColor1", iBackgroundColor, nil];
//[filterColor setDefaults];
image = [filterColor valueForKey:@"outputImage"];
//image = [CIImage imageWithColor:[CIColor colorWithRed:1 green: 0 blue: 0]];
// 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];
}
答案 1 :(得分:5)
CIQRCodeGenerator
仅生成黑白QR码,但您可以轻松地将生成的图像转换为其他颜色方案。
创建CIFalseColor
过滤器的实例,将其inputImage
设置为生成器中的outputImage
,将inputColor0
和inputColor1
设置为您的颜色想要使用而不是黑色和白色。然后将假色滤镜的outputImage
绘制到CG上下文中。
您还可以考虑使用CIMaskToAlpha
过滤器将QR码图像中的黑色或白色变为透明;那么你就可以把它放在任何背景颜色之上。 (只是不要把它放在太忙的背景上,否则人们将无法扫描它。)
答案 2 :(得分:5)
这是迅速为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)
}