_session,_preViewLayer为实例变量
AVCaptureDevice*device = [AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput*input = [AVCaptureDeviceInputdeviceInputWithDevice:deviceerror:nil];
AVCaptureMetadataOutput*output = [[AVCaptureMetadataOutputalloc]init];
[outputsetMetadataObjectsDelegate:selfqueue:dispatch_get_main_queue()];
_session= [[AVCaptureSessionalloc]init];
[_sessionaddInput:input];
[_sessionaddOutput:output];
//设置源设置输出数据类型
[outputsetMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
_preViewLayer= [[AVCaptureVideoPreviewLayeralloc]initWithSession:_session];
_preViewLayer.frame=self.view.bounds;
[self.view.layeraddSublayer:_preViewLayer];
[_sessionstartRunning];
代理方法
- (void)captureOutput:(AVCaptureOutput*)captureOutput didOutputMetadataObjects:(NSArray*)metadataObjects fromConnection:(AVCaptureConnection*)connection
{
[self.sessionstopRunning];
[self.preViewLayerremoveFromSuperlayer];
if(metadataObjects.count>0) {
NSString*string = [[metadataObjectslastObject]stringValue];
self.label.text= string;
}
}
网友评论