美文网首页
iOS 手动调节扫二维码的 焦距

iOS 手动调节扫二维码的 焦距

作者: 王家小雷 | 来源:发表于2019-10-24 15:42 被阅读0次

    pragma mark - AV capture methods

    • (void)setupCaptureSession {
      // 1
      if (_captureSession) return;
      // 2
      _videoDevice = [AVCaptureDevice
      defaultDeviceWithMediaType:AVMediaTypeVideo];
      if (!_videoDevice) {
      NSLog(@"No video camera on this device!");
      return;
      }
    // 3
    _captureSession = [[AVCaptureSession alloc] init];
    //高质量采集率
    [_captureSession setSessionPreset:AVCaptureSessionPresetHigh];
    
    // 4
    _videoInput = [[AVCaptureDeviceInput alloc]
                   initWithDevice:_videoDevice error:nil];
    //
    
    // 5
    if ([_captureSession canAddInput:_videoInput]) {
        [_captureSession addInput:_videoInput];
    }
    

    // //新增自动对焦功能
    // [_videoInput.device lockForConfiguration:nil];
    // //自动平衡
    // if ([_videoDevice isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeAutoWhiteBalance]) {
    // [_videoInput.device setWhiteBalanceMode:AVCaptureWhiteBalanceModeAutoWhiteBalance];
    // }
    // //先进行判断是否支持d控制对焦,不开启自动对焦功能,很难识别二维码
    // if (_videoDevice.isFocusPointOfInterestSupported && [_videoDevice isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {
    // [_videoInput.device setFocusMode:AVCaptureFocusModeContinuousAutoFocus];
    // }
    // [_videoInput.device unlockForConfiguration];
    //新增自动对焦功能

    // 6
    _previewLayer = [[AVCaptureVideoPreviewLayer alloc]
                     initWithSession:_captureSession];
    _previewLayer.videoGravity =
    AVLayerVideoGravityResizeAspectFill;
    
    
    // capture and process the metadata
    _metadataOutput = [[AVCaptureMetadataOutput alloc] init];
    dispatch_queue_t metadataQueue =
    dispatch_queue_create("com.1337labz.featurebuild.metadata", 0);
    

    // [_metadataOutput setMetadataObjectsDelegate:self
    // queue:metadataQueue];
    [_metadataOutput setMetadataObjectsDelegate:self
    queue:dispatch_get_main_queue()];
    if ([_captureSession canAddOutput:_metadataOutput]) {
    [_captureSession addOutput:_metadataOutput];
    }
    //不加调焦距扫不出来
    _metadataOutput.metadataObjectTypes = @[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeCode39Code,AVMetadataObjectTypeCode93Code,AVMetadataObjectTypeCode128Code,AVMetadataObjectTypeCode39Mod43Code,AVMetadataObjectTypeEAN8Code,AVMetadataObjectTypeEAN13Code];

    [_captureSession addObserver:self forKeyPath:@"running" options:NSKeyValueObservingOptionNew context:nil];
    

    // [self autoJiaoju];
    }

    pragma mark --调焦距

    // slider变动时改变label值

    • (void)sliderValueChanged:(id)sender {
      UISlider *slider = (UISlider *)sender;
      // self.valueLabel.text = [NSString stringWithFormat:@"%.1f", slider.value];
      //改变焦距 记住这里的输出链接类型要选中这个类型,否则屏幕会花的
      CGFloat kCameraScale=slider.value;
      [_videoDevice lockForConfiguration:nil];
      [_videoDevice rampToVideoZoomFactor:kCameraScale withRate:1.0];
      [_videoDevice unlockForConfiguration];

    }

    相关文章

      网友评论

          本文标题:iOS 手动调节扫二维码的 焦距

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