IOS QR code generation and scanning, ios Scanning

Source: Internet
Author: User

IOS QR code generation and scanning, ios Scanning
Attribute

@property (strong,nonatomic)AVCaptureDevice * device;@property (strong,nonatomic)AVCaptureDeviceInput * input;@property (strong,nonatomic)AVCaptureMetadataOutput * output;@property (strong,nonatomic)AVCaptureSession * session;@property (strong,nonatomic)AVCaptureVideoPreviewLayer * layer;@property (nonatomic, strong)UIImageView *imageView;

 

QR code generation
// 1. create a filter CIFilter * filter = [CIFilter filterWithName: @ "CIQRCodeGenerator"]; // 2. restore the default [filter setDefaults]; // 3. add data to the filter (Regular Expression/account and password) NSString * dataString = @ "http://www.520it.com"; NSData * data = [dataString dataUsingEncoding: NSUTF8StringEncoding]; [filter setValue: data forKeyPath: @ "inputMessage"]; // 4. obtain the output QR code CIImage * outputImage = [filter outputImage]; // because the generated QR code is blurred, you can use createNonInterpolatedUIImageFormCIImage: outputImage to obtain the hd qr code image. // 5. display the QR code self. imageView. image = [self createNonInterpolatedUIImageFormCIImage: outputImage withSize: 200];

* CreateNonInterpolatedUIImageFormCIImage: Implementation of the outputImage Method

/*** 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 (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 = limit (); CGContextRef bitmapRef = limit (nil, width, height, 8, 0, cs, (CGBitmapInfo) kCGImageAlphaNone); CIContext * context = [CIContext contextwittions: nil]; CGImageRef bitmapImage = [context createCGImage: image fromRect: extent]; CGContextSetInterpolationQuality (bitmapRef, kCGInterpolationNone); CGContextScaleCTM (bitmapRef, scale, scale); CGContextDrawImage (bitmapRef, extent, bitmapImage); // 2. save bitmap to image CGImageRef scaledImage = CGBitmapContextCreateImage (bitmapRef); CGContextRelease (bitmapRef); CGImageRelease (bitmapImage); return [UIImage imageWithCGImage: scaledImage];}

QR code scanning

// 1. create capture session AVCaptureSession * session = [[AVCaptureSession alloc] init]; self. session = session; // 2. add an input device (data input from the camera) AVCaptureDevice * device = [AVCaptureDevice failed: AVMediaTypeVideo]; AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice: device error: nil: input]; // 3. add output data (example Object> Class Object> Meta Object> root object) AVCaptureMetadataOutput * output = [AVCaptureMetadataOutput alloc] init]; [output setMetadataObjectsDelegate: self queue: dispatch_get_main_queue ()]; [session addOutput: output]; // 3. 1. set the type of the input metadata (type: QR 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];

* Method called after scanning the result

// This method is executed when data is scanned-(void) captureOutput :( AVCaptureOutput *) captureOutput rows :( NSArray *) metadataObjects fromConnection :( AVCaptureConnection *) connection {if (metadataObjects. count> 0) {// obtain the scanned data. The last scanned data is AVMetadataMachineReadableCodeObject * object = [metadataObjects lastObject]; NSLog (@ "% @", object. stringValue); // stop scanning [self. session stopRunning]; // remove the preview layer from [self. layer removeFromSuperlayer];} else {NSLog (@ "no data scanned ");}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.