Painted painted levels: fostered fostered fostered
Tags: “iOS CIFilter” “CIQRCodeGenerator” “CICode128BarcodeGenerator” “qr code and logo” author: Xs h. review: QiShare team
Following the text of iOS scanning TWO-DIMENSIONAL code/bar code and iOS scanning album image two-dimensional code, this paper introduces the relevant technical points of iOS generation of two-dimensional code/bar code. Take a look at the sample effect:
First, generate two-dimensional code
CIFilter (commonly used for filtering) in CoreImage framework provides the method of generating TWO-DIMENSIONAL code as follows:
GenerateQRCode :(NSString *)code size:(CGSize)size {NSData *codeData = [code dataUsingEncoding:NSUTF8StringEncoding]; CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator" withInputParameters:@{@"inputMessage": codeData, @"inputCorrectionLevel": QiInputCorrectionLevelH}]; // UIImage directly converted from filter.outputimage is not clear, Need do hd processing UIImage * codeImage = [QiCodeManager scaleImage: filter. The outputImage toSize: size]; return codeImage; }Copy the code
With the help of CIFilter, the steps to generate two-dimensional code are very simple:
1. Prepare codeData of two-dimensional code content; 2. Use CIQRCodeGenerator to create filter; 3. Set input parameters for filter: inputMessage: the TWO-DIMENSIONAL code data to be generated, inputCorrectionLevel: the fault tolerance value of two-dimensional code; 4. The QR code image of CIImage type was obtained through filter.outputImage, but the image was not clear and needed to be optimized (see below);
PS:
1. Print filter.inputKeys to view input parameters corresponding to different names (for example, inputCorrectionLevel parameter corresponding to CIQRCodeGenerator); 2. InputCorrectionLevel corresponds to four fault tolerance rates (as shown below). The greater the fault tolerance rate, the greater the allowed area of TWO-DIMENSIONAL code contamination; static NSString *QiInputCorrectionLevelL = @”L”; / /! < L: 7% static NSString *QiInputCorrectionLevelM = @”M”; / /! < M: 15% static NSString *QiInputCorrectionLevelQ = @”Q”; / /! < Q: 25% static NSString *QiInputCorrectionLevelH = @”H”; / /! < H: 30%
Second, generate bar code
The generation of barcode also relies on CIFilter, and is similar to the generation of TWO-DIMENSIONAL code, as follows:
GenerateCode128 :(NSString *)code size:(CGSize)size {NSData *codeData = [code dataUsingEncoding:NSUTF8StringEncoding]; / / use the CICode128BarcodeGenerator create filter CIFilter * filter = [CIFilter filterWithName: @ "CICode128BarcodeGenerator" withInputParameters:@{@"inputMessage": codeData, @"inputQuietSpace": @.0}]; // UIImage directly converted from filter.outputimage is not clear, Need do hd processing UIImage * codeImage = [QiCodeManager scaleImage: filter. The outputImage toSize: size]; return codeImage; }Copy the code
The steps to generate a bar code are the same as those to generate a QR code:
1. Prepare the content data of barcode codeData; 2, use CICode128BarcodeGenerator create filter; 3. Set input parameters for Filter: inputMessage: barcode data to be generated, inputQuietSpace: barcode white space, inputBarcodeHeight: barcode height; 4. The QR code image of CIImage type was obtained through filter.outputImage, but the image was not clear and needed to be optimized (see below);
3. Optimize the QR code/bar code
1, the picture changed to HD
As can be seen from the above, filter.outputimage is processed after the two-dimensional code/bar code is generated, in order to change the generated picture into high definition. The code is as follows:
// scaleImage (to generate high-quality image) + (UIImage *)scaleImage:(CIImage *)image toSize:(CGSize)size {//! IntegralRect = image.extent; // CGRectIntegral(image.extent); ImageRef = [[CIContext context] createCGImage: Image fromRect:integralRect]; / /! Create context CGFloat sideScale = fminf(sie.width/integralRect.sie.width, size.width / integralRect.size.height) * [UIScreen mainScreen].scale; Size_t contextRefWidth = ceilf(integralrect.sie.width * sideScale); size_t contextRefHeight = ceilf(integralRect.size.height * sideScale); CGContextRef contextRef = CGBitmapContextCreate(nil, contextRefWidth, contextRefHeight, 8, 0, CGColorSpaceCreateDeviceGray(), (CGBitmapInfo)kCGImageAlphaNone); / / gray, opaque CGContextSetInterpolationQuality (contextRef kCGInterpolationNone); // Set context without interpolating CGContextScaleCTM(contextRef, sideScale, sideScale); // Set CGContextDrawImage(contextRef, integralRect, imageRef); // Draw imageRef in context integralRect //! From the context to obtain CGImageRef CGImageRef scaledImageRef = CGBitmapContextCreateImage (contextRef); CGContextRelease(contextRef); CGImageRelease(imageRef); / /! To transfer CGImageRefc UIImage UIImage * scaledImage = [UIImage imageWithCGImage: scaledImageRef scale: [UIScreen mainScreen]. Scale orientation:UIImageOrientationUp]; return scaledImage; }Copy the code
2, pictures and logo
For two-dimensional code, often see in the middle of the logo style (wechat public number two-dimensional code, etc.), can be used to draw the way to achieve, as follows:
// combinateCodeImage (code+logo) + (UIImage *)combinateCodeImage:(UIImage *)codeImage andLogo:(UIImage *)logo { UIGraphicsBeginImageContextWithOptions(codeImage.size, YES, [UIScreen mainScreen].scale); [codeImage drawInRect:(CGRect){.0,.0, codeimage.sie.width, codeimage.sie.height}]; CGFloat logoSide = fminf(codeimage.size. Width, codeimage.size. Height) / 4; CGFloat logoX = (codeImage.size.width - logoSide) / 2; CGFloat logoY = (codeImage.size.height - logoSide) / 2; CGRect logoRect = (CGRect){logoX, logoY, logoSide, logoSide}; UIBezierPath *cornerPath = [UIBezierPath bezierPathWithRoundedRect:logoRect cornerRadius:logoSide / 5]; [cornerPath setLineWidth: 2.0]; [[UIColor whiteColor] set]; [cornerPath stroke]; [cornerPath addClip]; // Draw the logo in context [LOGO drawInRect:logoRect]; / / read the image from the context of codeImage = UIGraphicsGetImageFromCurrentImageContext (); UIGraphicsEndImageContext(); return codeImage; }Copy the code
The sample source code QiQRCode is available from GitHub’s QiShare open source library.
Ways to focus on us are:
QiShare(GitHub) QiShare(CocoaChina) QiShare(StackOverflow) QiShare(wechat)
Recommended articles:
IOS Xcode Bitcode iOS drawRect iOS write high quality Objective-C code (eight) strange dance weekly