美文网首页iOS Developer
iOS 扫一扫简单实现

iOS 扫一扫简单实现

作者: 静花寒 | 来源:发表于2015-12-24 14:56 被阅读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;

    }

    }

    相关文章

      网友评论

        本文标题:iOS 扫一扫简单实现

        本文链接:https://www.haomeiwen.com/subject/vyvvhttx.html