【问题标题】:How do you change the Back and Foreground Color of a CIFilter CIQRCodeGenerator Filter如何更改 CIFilter CIQRCodeGenerator 过滤器的背景颜色和前景颜色
【发布时间】:2014-08-04 07:12:31
【问题描述】:

我正在尝试为 OS X 制作一个二维码生成器,但我还没有成功制作一个比黑白更彩色的二维码我正在使用 CIQRCodeGenerator 进行 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];
}

所以请有人指出我正确的方向。

【问题讨论】:

    标签: macos cocoa qr-code cifilter


    【解决方案1】:

    这是一个 swift 示例(针对谷歌员工)

    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)
    }
    

    【讨论】:

    • 来自文档:根据 ISO/IEC 18004:2006 标准生成表示输入数据的输出图像。输出图像中代码的每个模块(方点)的宽度和高度为一个点。 要从字符串或 URL 创建 QR 码,请使用 NSISOLatin1StringEncoding 字符串编码将其转换为 NSData 对象
    【解决方案2】:

    这是现在可以使用的代码:-

    + (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];
    }
    

    【讨论】:

    • 太棒了。感谢您发布此信息。如果我在我的 Mac 应用程序中使用这种方法,我希望你不介意。哦,也许是我的天真,但是在返回最终的NSImage之前不应该释放scaledImage吗?
    • 欢迎您 这些基本的东西需要暴露在互联网上,以便更多人了解这些复杂的方法,如过滤器,如二维码。该方法仍然有效,无需释放 scaledImage 对我来说足够稳定。
    • 来自文档:要从字符串或 URL 创建 QR 码,请使用 .isoLatin1 字符串编码将其转换为 Data 对象developer.apple.com/library/archive/documentation/…
    • 你能更新你的答案吗? return [[NSImage alloc] initWithCGImage:scaledImage size:NSZeroSize];给了我错误。
    【解决方案3】:

    CIQRCodeGenerator 仅生成黑白二维码,但您可以轻松地将生成的图像转换为另一种配色方案。

    创建CIFalseColor 过滤器的实例,将其inputImage 设置为生成器中的outputImage,并将其inputColor0inputColor1 设置为您要使用的颜色,而不是黑白。然后将伪彩色滤镜的outputImage 绘制到您的CG 上下文中。

    您还可以考虑使用CIMaskToAlpha 过滤器将二维码图像中的黑色或白色变为透明;然后你就可以把它放在任何背景颜色的上面。 (只是不要把它放在太忙的背景上,否则人们将无法扫描它。)

    【讨论】:

    • 我猜 inputColor0 是背景色,inputColor1 是前景色
    • 实际上是相反的:)
    • 太棒了!可以添加渐变前景而不是默认的黑色吗?
    猜你喜欢
    • 2011-11-11
    • 1970-01-01
    • 2018-12-25
    • 1970-01-01
    • 2013-09-03
    • 2013-08-08
    相关资源
    最近更新 更多