How do you change the Back and Foreground Color of a CIFilter CIQRCodeGenerator Filter

后端 未结 3 1950
名媛妹妹
名媛妹妹 2021-02-15 00:17

I\'m trying to make a QR Code Generator for OS X but i haven\'t had succession in making a QRCode that can be more colourful that the black and white ones I\'m using the CIQRCod

3条回答
  •  余生分开走
    2021-02-15 01:11

    Heres the Code that works Now :-

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

提交回复
热议问题