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

 墨镜DHED_304 发布于 2023-01-09 13:04

我正在尝试为OS X制作一个QR码生成器,但是我还没有继续制作一个更加丰富多彩的QRCode,黑色和白色我正在使用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];
}

那么有人可以指出我正确的方向.

3 个回答
  • 这是一个快速示例(针对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)
    }
    

    2023-01-09 13:05 回答
  • CIQRCodeGenerator 只生成黑白QR码,但您可以轻松地将生成的图像转换为另一种配色方案.

    创建CIFalseColor过滤器的实例 ,将其设置inputImageoutputImage您的生成器,以及它inputColor0inputColor1您要使用的颜色而不是黑色和白色.然后将假色滤镜绘制outputImage到CG上下文中.

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

    2023-01-09 13:05 回答
  • 这是现在有效的代码: -

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

    2023-01-09 13:05 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有