扫描二维码

   //获取摄像设备

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

    // 如果是在模拟器上会出现crush 

    if ([session canAddInput:input]) {

        

        // 可以添加输入流

    }

 

 

    [session addInput:input];

    [session addOutput:output];

    //设置扫码支持的编码格式(如下设置条形码和二维码兼容)

    output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13CodeAVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];

    // zlj 创建输出对象

    AVCaptureVideoPreviewLayer *layer = [AVCaptureVideoPreviewLayer layerWithSession:session];

    layer.videoGravity = AVLayerVideoGravityResizeAspectFill;

    CGRect tempBound = self.view.layer.bounds;

    tempBound.size.height -= ScaleH(98);

    layer.frame = tempBound;

    [self.view.layer insertSublayer:layer atIndex:0];

    //开始捕获

    [session startRunning];

 

#pragma mark -- AVCaptureMetadataOutputObjectsDelegate

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{

    

    if (metadataObjects.count > 0 && firstIn) {

        AVMetadataMachineReadableCodeObject *metadataObject = metadataObjects[0];

        //输出扫描字符串

//        NSLog(@"QRCode : %@", metadataObject.stringValue);

        [viewModel getMeetingSing:metadataObject.stringValue];

        firstIn = NO;

    }

}

posted @ 2016-05-31 14:55  徒步阳光855  阅读(141)  评论(0编辑  收藏  举报