OGeek|极客世界-中国程序员成长平台

标题: javascript - 前置摄像头无法自动对焦或手动对焦 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:11
标题: javascript - 前置摄像头无法自动对焦或手动对焦

当前行为

我正在使用 react-native-camera使用 iPad/iPhone,我使用前置摄像头扫描条码(Code39、Code128、QR 等)。但是当使用前置摄像头时,它不会专注于条码或任何我稍微靠近的东西相机。后置摄像头工作得非常完美,但前置摄像头却不行。

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

如果我站在背景中,请将我的 Code39 举到靠近相机但在底部留一个小间隙,它不会尝试聚焦在卡片上而是在背景中保持聚焦在我身上。

我还提出了 issue here在他们的 GitHub 页面上,但是来这里看看是否有人以前遇到过这个问题,有解决方法等。

预期行为

我希望摄像头看到代码比我占用更多的屏幕,专注于它,阅读代码并继续运行代码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 respondsToSelectorselector(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 如果不满足任何其他条件,它将返回。

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



Best Answer-推荐答案


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. 时,该方法才会设置焦点

只有当以下条件返回 true

[device isFocusPointOfInterestSupported] && [device isFocusModeSupported:focusMode]

如果条件返回false,则没有autofocus但是图片可能be focused on曝光模式 [device setExposureMode:exposureMode];

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

由于stackoverflow上有几篇帖子(post 1post 2post 3post 4)指出不同的iphone版本不支持所有类型的前置摄像头自动对焦,我建议你在这些代码行上设置断点并检查isFocusModeSupported的值和 isFocusPointOfInterestSupported

关于javascript - 前置摄像头无法自动对焦或手动对焦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55846066/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4