Nowadays, QR codes can be seen everywhere, and QR codes are indispensable for both physical products and various gift certificates. Mobile devices such as mobile phones have become a good application platform for QR codes, whether it is to generate QR codes or scan QR codes. This article analyzes the QR code by generating QR code and scanning QR code. It is easy to use through content analysis.
First, let's talk about generating a QR code
QR codes can store plain text, business cards or URLs
Next, the steps to generate a QR code:
Import the CoreImage framework
Generate QR code through filter CIFilter again
1. Create a filter
2. Restore the default properties of the filter
3. Set content
4. Obtain the output file
5. Display the QR code
Code implementation CoreImage
// Generation of QR code // 1. Create filters CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; // 2. Restore the default properties of the filter [filter setDefaults]; // 3. Set content NSString *str = @"This is the result of a QR code generation"; NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; // Set properties using KVO [filter setValue:data forKey:@"inputMessage"]; // 4. Obtain the output file CIImage *outputImage = [filter outputImage]; // 5. Display the QR code = [UIImage imageWithCIImage:outputImage];
The pictures displayed in this way are not very clear, you can repaint the pictures yourself
Regenerate high-definition pictures: Just search online, you can not care about the specific process for the time being
/** * Generate a UIImage of the specified size based on CIImage * * @param image CIImage * @param size Image width */ - (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size { CGRect extent = CGRectIntegral(); CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent)); // 1. Create a bitmap; size_t width = CGRectGetWidth(extent) * scale; size_t height = CGRectGetHeight(extent) * scale; CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray(); CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone); CIContext *context = [CIContext contextWithOptions:nil]; CGImageRef bitmapImage = [context createCGImage:image fromRect:extent]; CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone); CGContextScaleCTM(bitmapRef, scale, scale); CGContextDrawImage(bitmapRef, extent, bitmapImage); // 2.Save bitmap to picture CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef); CGContextRelease(bitmapRef); CGImageRelease(bitmapImage); return [UIImage imageWithCGImage:scaledImage]; }
Also, when setting the content as a URL, if it has a protocol header, the web page will be automatically opened.
NSString *str = @;
Must be equipped with a protocol header to open
Back to top
Scan the QR code
AVFoundation framework
QR code scanning process
1. Create a capture session AVCaptureSession
Add input device (data input from camera) AVCaptureDevice AVCaptureDeviceInput
2. Add output data (example object-->class object-->metaclass object-->root metaclass object) AVCaptureMetadataOutput
2.1. Set the type of input metadata (the type is QR code data) setMetadataObjectTypes
3. Add scan layer AVCaptureVideoPreviewLayer
4. Start scanning startRunning
5. Implement the callback proxy method to obtain the scan result captureOutput::
#import "" #import <AVFoundation/> @interface ViewController () <AVCaptureMetadataOutputObjectsDelegate> /**Show layer*/ @property (nonatomic, strong) AVCaptureVideoPreviewLayer *layer; /**Catch session*/ @property (nonatomic, strong) AVCaptureSession *session; @end @implementation ViewController - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // Scan the QR code // 1. Create a capture session AVCaptureSession *session = [[AVCaptureSession alloc] init]; = session; // 2. Add input device (data input from the camera) AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil]; [session addInput:input]; // 3. Add output data (example object-->class object-->metaclass object-->root metaclass object) AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init]; [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; [session addOutput:output]; // 3.1. Set the type of input metadata (the type is QR code data) [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]]; // 4. Add a scan layer AVCaptureVideoPreviewLayer *layer = [AVCaptureVideoPreviewLayer layerWithSession:session]; = ; [ addSublayer:layer]; = layer; // 5. Start scanning [session startRunning]; } /** * Callback method to implement output */ - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection { //Storing result data in array metadataObjects if ( > 0) { // Get the final read result AVMetadataMachineReadableCodeObject *object = [metadataObjects lastObject]; NSLog(@"%@",); [ stopRunning]; [ removeFromSuperlayer]; } else { NSLog(@"No data was scanned"); } }
In summary: This QR code is not difficult to use
This article ends here. I hope that the generation and scanning of QR codes of IOS Note 061 will help you in future work and study. The following article will share with you how to use QR code on Apple iOS devices. Friends who need to knowClick here。