前置摄像头不自动对焦或手动对焦

Front Facing Camera does not Auto Focus or Manually Focus

当前行为

我正在使用 react-native-camera 和 iPads/iPhones 并且我使用前置摄像头扫描条形码(Code39、Code128、QR 等)但是当使用前置摄像头时,它不会聚焦在条形码或我放在相机附近的任何东西上。后置摄像头绝对完美,但前置摄像头却不行。

我无法测试 android,因为我不是纯粹为 android 构建 iOS。我似乎找不到任何关于让前置摄像头对焦的信息。

如果我站在背景中,将我的 Code39 靠近相机举起,但在底部留一个小缝隙,它不会尝试对卡片对焦,而是在背景中始终对我对焦。

我还在他们的 GitHub 页面上提出了一个 issue here,但来这里是想看看是否有人 运行 之前参与过这个问题,有解决办法等等

预期行为

我希望相机看到代码比我占据更多的屏幕,专注于它,阅读代码并继续 运行 代码 onBarCodeRead

我尝试过什么来修复它?

如何重新创建它?

使用的软件和版本

代码

我在 react-native-modal 中渲染相机,并将我的代码放在下面。

<RNCamera 
  style={styles.camera}
  type={RNCamera.Constants.Type.front}
  flashMode={RNCamera.Constants.FlashMode.off}
  autoFocus={RNCamera.Constants.AutoFocus.on}
  captureAudio={false}
  onBarCodeRead={(barcode) => {
    if (this.state.isModalVisible) {
      this.setState({
        isModalVisible : false
      }, () => this.captureQR(barcode.data));
    }
}}>

相关包码

我发现了一些似乎相关的代码:

RNCamera.m method updateFocusDepth

- (void)updateFocusDepth
{
    AVCaptureDevice *device = [self.videoCaptureDeviceInput device];
    NSError *error = nil;

    if (device == nil || self.autoFocus < 0 || device.focusMode != RNCameraAutoFocusOff || device.position == RNCameraTypeFront) {
        return;
    }

    if (![device respondsToSelector:@selector(isLockingFocusWithCustomLensPositionSupported)] || ![device isLockingFocusWithCustomLensPositionSupported]) {
        RCTLogWarn(@"%s: Setting focusDepth isn't supported for this camera device", __func__);
        return;
    }

    if (![device lockForConfiguration:&error]) {
        if (error) {
            RCTLogError(@"%s: %@", __func__, error);
        }
        return;
    }

    __weak __typeof__(device) weakDevice = device;
    [device setFocusModeLockedWithLensPosition:self.focusDepth completionHandler:^(CMTime syncTime) {
        [weakDevice unlockForConfiguration];
    }];
}

更具体地说,这里只有这一部分:

如果 device.position == RNCameraTypeFront 它只会 return 提供它不满足任何其他条件。

    if (device == nil || self.autoFocus < 0 || device.focusMode != RNCameraAutoFocusOff || device.position == RNCameraTypeFront) {
        return;
    }

IOS 有 three Focus Modes。您需要使用 AVCaptureFocusModeContinuousAutoFocus

AVCaptureFocusModeContinuousAutoFocus: The camera continuously autofocuses as needed.

You use the isFocusModeSupported: method to determine whether a device supports a given focus mode, then set the mode using the focusMode property.

react-native-camera 会在两种不同的情况下改变焦点(你可以在这行用 xcode 设置断点):

  1. focusWithMode 方法仅在您的前置摄像头支持 isFocusPointOfInterestSupportedAVCaptureFocusModeContinuousAutoFocus
  2. 时设置焦点

只有满足以下条件returns true

,该方法才会将对焦模式[device setFocusMode:focusMode];更改为AVCaptureFocusModeContinuousAutoFocus
[device isFocusPointOfInterestSupported] && [device isFocusModeSupported:focusMode]

如果条件returnsfalse,没有autofocus,但是图片可能be focused on ExposureMode [device setExposureMode:exposureMode];

  1. updateAutoFocusPointOfInterest 当用户点击屏幕时根据他触摸的 x, y 坐标更改焦点。

因为有几个帖子 (post 1, post 2, post 3, post 4) on Whosebug stating that different iphone versions do not support all type of autofocus with the frontcamera, I suggest you to set breakpoints on those line of codes and check the value of isFocusModeSupported and isFocusPointOfInterestSupported