IOS7 uses native API to scan QR code and barcode

 

 

Scan QR code barcode using IOS7 native API

http://my.oschina.net/u/2340880/blog/405847

 

Before IOS7, developers generally used third-party libraries for code scanning programming. ZBarSDK is commonly used. After IOS7, the AVMetadataObject class of the system provides us with an interface for parsing QR codes. After testing, the efficiency of scanning and processing using the native API is very high, much higher than that of third-party libraries.

1. Example of usage

 

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

@interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate>//Agent for processing acquisition information
{
    AVCaptureSession * session;//Intermediate bridge between input and output
}
@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //Get the camera device
    AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    //create input stream
    AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
    //create output stream
    AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc]init];
    //Set the proxy to refresh in the main thread
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
    //Initialize the link object
    session = [[AVCaptureSession alloc]init];
    //High quality acquisition rate
    [session setSessionPreset:AVCaptureSessionPresetHigh];
    
    [session addInput:input];
    [session addOutput:output];
    //Set the encoding format supported by the scan code (set the barcode and QR code compatibility as follows)
    output.metadataObjectTypes=@[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];
    // start capturing
    [session startRunning];
}

 After that, we can already see the content captured by the camera on our UI. As long as the method in the agent is implemented, the scanning of the QR code barcode can be completed.

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
    if (metadataObjects.count>0) {
        //[session stopRunning];
        AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ];
        // output scan string
        NSLog(@"%@",metadataObject.stringValue);
    }
}

 

2. Some optimizations

Through the above code test, we can find that the parsing and processing efficiency of the system is quite high, and the API officially provided by IOS is indeed very powerful. However, we can do further optimization to improve the efficiency even more:

First , there is a property like this in the AVCaptureMetadataOutput class (available after IOS7.0):

@property(nonatomicCGRect rectOfInterest;

The general meaning of this attribute is to tell the system the area it needs to pay attention to. Most APP's scan code UI will have a box to remind you to put the barcode in that area. The function of this attribute is here, it can set a range, only Process the information of the image captured in this range. As a result, it is conceivable that the efficiency of our code will be greatly improved when using this property. A few things to note:

1. This CGRect parameter is not the same as the normal Rect range. Its four values ​​range from 0 to 1, indicating the ratio.

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

3. The same is true for width and height settings.

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

output.rectOfInterest=CGRectMake(0.5,0,0.51);

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326695489&siteId=291194637