ISO7 Qr code detailed introduction and usage
- 2020-09-16 07:48:01
- OfStack
Use the built-in system to generate/scan 2-d codes
iOS7 starts with apple integrating the scan ### generated 2d code steps
Import the CoreImage framework #import < CoreImage/CoreImage.h >
Filter CIFilte to generate 2-d code ### 2-d code content (traditional barcodes can only put Numbers)
Plain text
Business card
URL
Generate 2 dimensional code
// 1. Create filters
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
// 2. Restore the default
[filter setDefaults];
// 3. Add data to the filter ( Regular expression / Account number and password )
NSString *dataString = @"http://www.520it.com";
NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
[filter setValue:data forKeyPath:@"inputMessage"];
// 4. Get output 2 D code
CIImage *outputImage = [filter outputImage];
// Because the generated 2 Dimension code is fuzzy, so it goes through createNonInterpolatedUIImageFormCIImage:outputImage To get high definition 2 D code pictures
// 5. According to 2 D code
self.imageView.image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:300];
/**
* According to the CIImage Generates the specified size UIImage
*
* @param image CIImage
* @param size Image width
*/
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
{
CGRect extent = CGRectIntegral(image.extent);
CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
// 1. create 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 the picture
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
return [UIImage imageWithCGImage:scaledImage];
}
Scan 2-d code
// 1. Create a capture session
AVCaptureSession *session = [[AVCaptureSession alloc] init];
self.session = session;
// 2. Add input device ( Data is input from the camera )
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
[session addInput:input];
// 3. Add output data ( The sample object --> Class object --> Yuan class object --> Root metaclass object )
AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
[session addOutput:output];
// 3.1. Sets the type of input metadata ( Type is 2 D code data )
[output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
// 4. Add scan layer
AVCaptureVideoPreviewLayer *layer = [AVCaptureVideoPreviewLayer layerWithSession:session];
layer.frame = self.view.bounds;
[self.view.layer addSublayer:layer];
self.layer = layer;
// 5. Start scanning
[session startRunning];
The method that scans will call
// This method executes when the data is scanned
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
if (metadataObjects.count > 0) {
// Get the scan data, and finally 1 The latest scanned data in 10 hours
AVMetadataMachineReadableCodeObject *object = [metadataObjects lastObject];
NSLog(@"%@", object.stringValue);
// Stop the scan
[self.session stopRunning];
// Remove the preview layer
[self.layer removeFromSuperlayer];
} else {
NSLog(@" No data was scanned ");
}
}
Above is the IOS 2 dimensional code of the data collation, the follow-up continue to supplement relevant information, thank you for your support to this site!