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

标签 javascript ios react-native react-native-camera

当前行为

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

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

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

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

预期行为

我希望摄像头看到代码占据的屏幕比我多得多,关注它,阅读代码并继续运行代码onBarCodeRead

我尝试过什么来修复它?

  • 禁用 autoFocus,因为这是针对 Android 的修复,这里运气不好。
  • 手动设置focusDepth
  • 手动将 autoFocusPointOfInterest 设置为屏幕中心。
  • zoom 更改为 0.2,然后慢慢增加到开始看起来很傻的程度。
  • onGoogleVisionBarcodesDetected 设置为仅 console.log 内容,因为这是针对 android 的另一个修复。
  • 更新 react-native-camera@2.6.0
  • 更新到 react-native-camera@git+https://git@github.com/react-native-community/react-native-camera.git 的 master 分支

我怎样才能重新创建它?

  • 创建新的 react-native 项目
  • yarn 添加 react-native-camera/npm install react-native-camera --save
  • 设置 type={RNCamera.Constants.Type.front} 以使用前置摄像头。
  • 设置 autoFocus={RNCamera.Constants.AutoFocus.on}(默认情况下它是打开的,这只是确保它。
  • 设置onBarCodeRead={() => alert('找到条形码')}
  • 尝试扫描 Code39/Code128 - (creatable here)
  • 尝试扫描它,您会发现相机不会聚焦在它上面,而是始终聚焦在背景上。如果您用手指遮住相机,情况也是如此,当您将手指移开时,您预计相机会偏离背景并尝试重新对焦。事实并非如此,它将在中/远距离保持聚焦。

使用的软件和版本

  • iOS:12.1.4
  • react-native-camera: ^2.1.1/2.6.0
  • native react :0.57.7
  • react :16.6.1

代码

我在 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 它只会返回,前提是它不满足任何其他条件。

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

仅当以下条件返回 true 时,该方法才会将焦点模式 [device setFocusMode:focusMode]; 更改为 AVCaptureFocusModeContinuousAutoFocus p>

[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/

相关文章:

javascript - jQuery 立即验证

javascript - 在给定的时间后平滑滚动到中心当前部分

javascript - React Native onPress 事件延迟

database - Realm with React Native,如何通过列表对象进行查询

javascript - YouTube 嵌入式视频无法在 IE8 的模态窗口中播放

php - 没有设置 HttpOnly 标志的 session Cookie

iphone - 如何更改 xcode 代码签名身份和应用程序名称?

ios - GCM 注册未准备好使用 ios9 中的身份验证凭据

ios - 这个 objective-c 代码是否创建了一个强大的引用循环?

React-Native 在图像中使用状态需要