• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 15:11:54 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

当前行为

我正在使用 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 的另一个修复。
  • 更新[email protected]
  • 更新到 react-native-camera@git+https://[email protected]/react-native-community/react-native-camera.git 上的 master 分支

如何重新创建它?

  • 创建新的 react-native 项目
  • yarn add react-native-camera/npm install react-native-camera --save
  • 设置 type={RNCamera.Constants.Type.front} 以使用前置摄像头。
  • set autoFocus={RNCamera.Constants.AutoFocus.on}(反正默认是开启的,这只是确保它。
  • 设置 onBarCodeRead={() => alert('barcode found')}
  • 尝试扫描 Code39/Code128 - (creatable here)
  • 尝试扫描它,你会发现相机不会聚焦在它上面,而是保持聚焦在背景上。如果您用手指盖住相机也是如此,当您将手指移开时,您会期望相机与背景失焦并尝试重新对焦。情况并非如此,它会在中/远距离保持对焦。

使用的软件和版本

  • iOS:12.1.4
  • react-native-camera:^2.1.1/2.6.0
  • 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 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap