iOS Developer

iOS 扫一扫简单实现

2015-12-24  本文已影响373人  静花寒

_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;

}

}

上一篇下一篇

猜你喜欢

热点阅读