IOS create color QR codes
Because the QR codes created by the system are all black by default, I suddenly want to change the QR code color. The specific operation is a bit complicated, and there are many C syntaxes used in it, and Swift is difficult to write, so I use OC by default. I only pasted the code of the .m file, and the .h file is the declaration of several class functions.
#import "UIImage+" @implementation UIImage (CreateQRCode) + (UIImage *)createQRCode:(NSString *)string andSize:(CGSize)size andColor:(UIColor *)color { UIImage *qrcode = [self createNonInterpolatedUIImageFormCIImage:[self createQRForString:string] withSize:size]; const CGFloat *_components = CGColorGetComponents(); CGFloat red = _components[0] * ; CGFloat green = _components[1] * ; CGFloat blue = _components[2] * ; return [self imageBlackToTransparent:qrcode withRed:red andGreen:green andBlue:blue]; } + (void)setImageViewShadow:(UIImageView *)view { = CGSizeMake(0, 2); = 2; = [UIColor blackColor].CGColor; = 0.5; = [UIColor clearColor]; } #pragma mark - Create grayscale maps, only grayscale maps can change colors+ (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGSize)size { CGRect extent = CGRectIntegral(); CGFloat scale = MIN(/CGRectGetWidth(extent), /CGRectGetHeight(extent)); size_t width = CGRectGetWidth(extent) * scale; size_t height = CGRectGetHeight(extent) * scale; // iOS does not support device dependency or universal color space. iOS apps must use device color space// The device color space is mainly used in IOS applications, because other color spaces cannot be used on IOS. In most cases, Mac OS X applications should use common color space instead of device color space.// CGColorSpaceCreateDeviceGray: Create device dependent grayscale color space// CGColorSpaceCreateDeviceRGB: Create device dependent RGB color space// CGColorSpaceCreateDeviceCMYK: Create device dependent CMYK color space CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();//This is the main attribute of changing the color of the QR code. It must be grayscale space. Its function is to transform UIImage into grayscale map CGContextRef bitmapRef = CGBitmapContextCreate(NULL, width, height, 8, 0, cs, kCGImageAlphaNone); CIContext * context = [CIContext contextWithOptions:NULL]; CGImageRef bitmapImage = [context createCGImage:image fromRect:extent]; CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationHigh); CGContextScaleCTM(bitmapRef, scale, scale); CGContextDrawImage(bitmapRef, extent, bitmapImage); CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef); CGContextRelease(bitmapRef); CGImageRelease(bitmapImage); return [UIImage imageWithCGImage:scaledImage]; } #pragma mark - the main code for creating QR codes+ (CIImage *)createQRForString:(NSString *)qrString { NSData *stringData = [qrString dataUsingEncoding:NSUTF8StringEncoding]; CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; [qrFilter setValue:stringData forKey:@"inputMessage"]; [qrFilter setValue:@"M" forKey:@"inputCorrectionLevel"]; return ; } #pragma mark - Change the color of the QR codevoid ProviderReleaseData (voidvoid *info, const voidvoid *data, size_t size){ free((void*)data); } + (UIImage*)imageBlackToTransparent:(UIImage*)image withRed:(CGFloat)red andGreen:(CGFloat)green andBlue:(CGFloat)blue{ const int imageWidth = ; const int imageHeight = ; size_t bytesPerRow = imageWidth * 4; uint32_t* rgbImageBuf = (uint32_t*)malloc(bytesPerRow * imageHeight); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(rgbImageBuf, imageWidth, imageHeight, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast); CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), ); int pixelNum = imageWidth * imageHeight; uint32_t* pCurPtr = rgbImageBuf; for (int i = 0; i < pixelNum; i++, pCurPtr++){ if ((*pCurPtr & 0xFFFFFF00) < 0x99999900){ uint8_t* ptr = (uint8_t*)pCurPtr; ptr[3] = red; //0~255 ptr[2] = green; ptr[1] = blue; }else{ uint8_t* ptr = (uint8_t*)pCurPtr; ptr[0] = 0; } } CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, rgbImageBuf, bytesPerRow * imageHeight, ProviderReleaseData); CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight, 8, 32, bytesPerRow, colorSpace, kCGImageAlphaLast | kCGBitmapByteOrder32Little, dataProvider, NULL, true, kCGRenderingIntentDefault); CGDataProviderRelease(dataProvider); UIImage* resultUIImage = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); CGContextRelease(context); CGColorSpaceRelease(colorSpace); return resultUIImage; } @end
Thank you for reading, I hope it can help you. Thank you for your support for this site!