【发布时间】:2019-09-14 16:42:24
【问题描述】:
当前行为
我在 iPad/iPhone 上使用react-native-camera,我使用前置摄像头扫描条码(Code39、Code128、QR 等)。但是在使用前置摄像头时,它不会专注于条形码或我放在相机附近的任何东西。后置摄像头工作得非常完美,但前置摄像头却不行。
我无法测试 android,因为我不是纯粹为 iOS 构建 android。我似乎找不到任何关于让前置摄像头对焦的信息。
如果我站在背景中,请将我的 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
如何重新创建它?
- 创建新的 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.mmethod 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;
}
【问题讨论】:
标签: javascript ios react-native react-native-camera