iOS二维码扫描

作者: 微末凡尘_ | 来源:发表于2016-11-21 23:44 被阅读128次

    效果图

    WechatIMG1.jpeg

    核心代码

    初始化AVCaptureSession

    - (void)initAVCaptureWithView:(UIView *)parentView {
        
        _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        
        _input = [AVCaptureDeviceInput deviceInputWithDevice:_device error:nil];
        
        _output = [[AVCaptureMetadataOutput alloc] init];
        [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
        
        _session = [[AVCaptureSession alloc] init];
        _session.sessionPreset = (_screenRect.size.height < 500) ? AVCaptureSessionPreset640x480 : AVCaptureSessionPresetHigh;
        
        if ([_session canAddInput:_input]) {
            [_session addInput:_input];
        }
        if ([_session canAddOutput:_output]) {
            [_session addOutput:_output];
        }
        
        _output.metadataObjectTypes = @[AVMetadataObjectTypeEAN13Code,
                                        AVMetadataObjectTypeEAN8Code,
                                        AVMetadataObjectTypeCode128Code,
                                        AVMetadataObjectTypeQRCode];
    
        CGSize screenSize = _screenRect.size;
        CGRect screenDrawRect = CGRectMake(0, 0, screenSize.width, screenSize.height);
        
        _scanRect = CGRectMake((screenDrawRect.size.width - _transparentAreaSize.width) / 2,
                               (screenDrawRect.size.height - _transparentAreaSize.height) / 2,
                               _transparentAreaSize.width,
                               _transparentAreaSize.height);
        
        _output.rectOfInterest = CGRectMake(_scanRect.origin.y / screenSize.height,
                                            _scanRect.origin.x / screenSize.width,
                                            _scanRect.size.height / screenSize.height,
                                            _scanRect.size.width / screenSize.width);
        
        _previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
        _previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
        [_previewLayer setFrame:_screenRect];
        [parentView.layer insertSublayer:_previewLayer atIndex:0];
        
        [_session startRunning];
    }
    

    获取结果

    - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects
           fromConnection:(AVCaptureConnection *)connection {
        
        if (metadataObjects.count > 0) {
            [self.scanLineTimer setFireDate:[NSDate distantFuture]];
            [self.session stopRunning];
            
            AVMetadataMachineReadableCodeObject *obj = metadataObjects[0];
            if ([self.delegate respondsToSelector:@selector(didFinshedScanningQRCode:)]) {
                [self.delegate didFinshedScanningQRCode:[obj stringValue]];
            }
        }
    }
    

    Demo地址

    https://github.com/codelyw/iOSDemo

    相关文章

      网友评论

        本文标题:iOS二维码扫描

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