作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为 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/
我是一名优秀的程序员,十分优秀!