前置摄像头不自动对焦或手动对焦
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
我尝试过什么来修复它?
- 禁用
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 b运行ch
如何重新创建它?
- 创建新的 react-native 项目
yarn add react-native-camera
/ npm install react-native-camera --save
- 设置
type={RNCamera.Constants.Type.front}
使用前置摄像头。
- 设置
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
- 本机反应:0.57.7
- 反应: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
它只会 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
设置断点):
focusWithMode
方法仅在您的前置摄像头支持 isFocusPointOfInterestSupported
和 AVCaptureFocusModeContinuousAutoFocus
时设置焦点
只有满足以下条件returns true
,该方法才会将对焦模式[device setFocusMode:focusMode];
更改为AVCaptureFocusModeContinuousAutoFocus
[device isFocusPointOfInterestSupported] && [device isFocusModeSupported:focusMode]
如果条件returnsfalse
,没有autofocus
,但是图片可能be focused on ExposureMode [device setExposureMode:exposureMode];
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
当前行为
我正在使用 react-native-camera
和 iPads/iPhones 并且我使用前置摄像头扫描条形码(Code39、Code128、QR 等)但是当使用前置摄像头时,它不会聚焦在条形码或我放在相机附近的任何东西上。后置摄像头绝对完美,但前置摄像头却不行。
我无法测试 android,因为我不是纯粹为 android 构建 iOS。我似乎找不到任何关于让前置摄像头对焦的信息。
如果我站在背景中,将我的 Code39 靠近相机举起,但在底部留一个小缝隙,它不会尝试对卡片对焦,而是在背景中始终对我对焦。
我还在他们的 GitHub 页面上提出了一个 issue here,但来这里是想看看是否有人 运行 之前参与过这个问题,有解决办法等等
预期行为
我希望相机看到代码比我占据更多的屏幕,专注于它,阅读代码并继续 运行 代码 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 b运行ch
如何重新创建它?
- 创建新的 react-native 项目
yarn add react-native-camera
/npm install react-native-camera --save
- 设置
type={RNCamera.Constants.Type.front}
使用前置摄像头。 - 设置
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
- 本机反应:0.57.7
- 反应: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
它只会 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
设置断点):
focusWithMode
方法仅在您的前置摄像头支持isFocusPointOfInterestSupported
和AVCaptureFocusModeContinuousAutoFocus
时设置焦点
只有满足以下条件returns true
[device setFocusMode:focusMode];
更改为AVCaptureFocusModeContinuousAutoFocus
[device isFocusPointOfInterestSupported] && [device isFocusModeSupported:focusMode]
如果条件returnsfalse
,没有autofocus
,但是图片可能be focused on ExposureMode [device setExposureMode:exposureMode];
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