QR Code barcode scanning using IOS7 native API

Source: Internet
Author: User

QR Code barcode scanning using IOS7 native API

Prior to IOS7, developers typically use third-party libraries for code-scanning programming. Commonly used is ZBARSDK,IOS7, in the system's Avmetadataobject class, we provide an interface to parse the two-dimensional code. Tested, the use of native API scanning and processing is very efficient, far higher than the third-party library.

I. Examples of usage methods

The official interface is very simple and the code is as follows:

?
123456789101112131415161718192021222324252627282930313233343536 @interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate>//用于处理采集信息的代理{    AVCaptureSession * session;//输入输出的中间桥梁}@end@implementation ViewController - (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    //获取摄像设备    AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];    //创建输入流    AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];    //创建输出流    AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc]init];    //设置代理 在主线程里刷新    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];        //初始化链接对象    session = [[AVCaptureSession alloc]init];    //高质量采集率    [session setSessionPreset:AVCaptureSessionPresetHigh];        [session addInput:input];    [session addOutput:output];    //设置扫码支持的编码格式(如下设置条形码和二维码兼容)    [email protected][AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];           AVCaptureVideoPreviewLayer * layer = [AVCaptureVideoPreviewLayer layerWithSession:session];    layer.videoGravity=AVLayerVideoGravityResizeAspectFill;    layer.frame=self.view.layer.bounds;    [self.view.layer insertSublayer:layer atIndex:0];    //开始捕获    [session startRunning];}

After that, we can see what the camera captures on our UI, and as long as we implement the methods in the proxy, we can scan the barcode of the QR code:

?
12345678 -(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{    if(metadataObjects.count>0) {        //[session stopRunning];        AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ];        //输出扫描字符串        NSLog(@"%@",metadataObject.stringValue);    }}
Second, some optimization

Through the above code test, we can find that the system's resolution processing efficiency is quite high, iOS official API is also really very powerful, however, we can do further optimization, will be more efficient:

First, there is one such attribute in the Avcapturemetadataoutput class (available after IOS7.0):

@property (nonatomic) CGRect rectofinterest;

This property is roughly meant to tell the system what it needs to be aware of, and most apps have a box in the sweep code UI that reminds you to place the barcode in that area, where it can set a range to handle only the information captured within that range. As a result, we can imagine that the efficiency of our code will be greatly improved, when using this property. A few points to note:

1, this cgrect parameter and the normal rect range is not quite the same, its four values range is 0-1, representing the scale.

2. After testing, the x in this parameter corresponds exactly to the vertical distance from the upper left corner, and y corresponds to the horizontal distance from the upper left corner.

3, Width and height settings are similar.

3. For example, if we want the scanned processing area to be the lower half of the screen, we set

?
1 output.rectOfInterest=CGRectMake(0.5,0,0.5, 1);

Why do Apple design this, or is this parameter my usage there is not right, but also need to know the friend to give a guide.

QR Code barcode scanning using IOS7 native API

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.