菜鸟教程小白 发表于 2022-12-12 15:11:54

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


                                            <p><p><strong>当前行为</strong></p>

<p>我正在使用 <a href="https://github.com/react-native-community/react-native-camera" rel="noreferrer noopener nofollow"><code>react-native-camera</code></a>使用 iPad/iPhone,我使用前置摄像头扫描条码(Code39、Code128、QR 等)。但是当使用前置摄像头时,它不会专注于条码或任何我稍微靠近的东西相机。后置摄像头工作得非常完美,但前置摄像头却不行。</p>

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

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

<p>我还提出了 <a href="https://github.com/react-native-community/react-native-camera/issues/2231" rel="noreferrer noopener nofollow">issue here</a>在他们的 GitHub 页面上,但是来这里看看是否有人以前遇到过这个问题,有解决方法等。</p>

<p><strong>预期行为</strong></p>

<p>我希望摄像头看到代码比我占用更多的屏幕,专注于它,阅读代码并继续运行代码<code>onBarCodeRead</code></p>

<p><strong>我尝试过什么解决方法?</strong></p>

<ul>
<li>禁用 <code>autoFocus</code>,因为这是针对 Android 的修复,这里没有运气。</li>
<li>手动设置<code>focusDepth</code>。</li>
<li>手动将 <code>autoFocusPointOfInterest</code> 设置为屏幕中心。</li>
<li>将 <code>zoom</code> 更改为 0.2,然后慢慢增加到它开始看起来很傻的程度。</li>
<li>将 <code>onGoogleVisionBarcodesDetected</code> 设置为 console.log 一些内容,因为这是针对 android 的另一个修复。</li>
<li>更新<code>[email protected]</code> </li>
<li>更新到 <code>react-native-camera@git+https://[email protected]/react-native-community/react-native-camera.git 上的 master 分支</code></li>
</ul>

<p><strong>如何重新创建它?</strong></p>

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

<p><strong>使用的软件和版本</strong></p>

<ul>
<li>iOS:12.1.4</li>
<li>react-native-camera:^2.1.1/2.6.0</li>
<li> react 原生:0.57.7</li>
<li> react :16.6.1</li>
</ul>

<p><strong>代码</strong></p>

<p>我在 <a href="https://github.com/react-native-community/react-native-modal" rel="noreferrer noopener nofollow"><code>react-native-modal</code></a> 中渲染相机我已经把我的代码放在下面了。</p>

<pre><code>&lt;RNCamera
style={styles.camera}
type={RNCamera.Constants.Type.front}
flashMode={RNCamera.Constants.FlashMode.off}
autoFocus={RNCamera.Constants.AutoFocus.on}
captureAudio={false}
onBarCodeRead={(barcode) =&gt; {
    if (this.state.isModalVisible) {
      this.setState({
      isModalVisible : false
      }, () =&gt; this.captureQR(barcode.data));
    }
}}&gt;
</code></pre>

<p><strong>相关包代码</strong></p>

<p>我发现了一些似乎相关的代码:</p>

<p>在 <code>RNCamera.m</code> <a href="https://github.com/react-native-community/react-native-camera/blob/master/ios/RN/RNCamera.m#L275" rel="noreferrer noopener nofollow">method <code>updateFocusDepth</code></a> </p>

<pre><code>- (void)updateFocusDepth
{
    AVCaptureDevice *device = ;
    NSError *error = nil;

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

    if (! || !) {
      RCTLogWarn(@&#34;%s: Setting focusDepth isn&#39;t supported for this camera device&#34;, __func__);
      return;
    }

    if (!) {
      if (error) {
            RCTLogError(@&#34;%s: %@&#34;, __func__, error);
      }
      return;
    }

    __weak __typeof__(device) weakDevice = device;
    [device setFocusModeLockedWithLensPosition:self.focusDepth completionHandler:^(CMTime syncTime) {
      ;
    }];
}
</code></pre>

<p>更具体地说就是这里的这一部分:</p>

<p>如果 <code>device.position == RNCameraTypeFront</code> 如果不满足任何其他条件,它将返回。</p>

<pre><code>    if (device == nil || self.autoFocus &lt; 0 || device.focusMode != RNCameraAutoFocusOff || device.position == RNCameraTypeFront) {
      return;
    }
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>IOS 有 <a href="https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/04_MediaCapture.html#//apple_ref/doc/uid/TP40010188-CH5-SW2#Focus_Modes" rel="noreferrer noopener nofollow">three <code>Focus Modes</code></a> .您需要使用 <code>AVCaptureFocusModeContinuousAutoFocus</code></p>
<blockquote>
<p><a href="https://developer.apple.com/documentation/avfoundation/avcapturefocusmode/avcapturefocusmodecontinuousautofocus" rel="noreferrer noopener nofollow">AVCaptureFocusModeContinuousAutoFocus</a>: The camera continuously autofocuses as needed.</p>
<p><strong>You use the <a href="https://developer.apple.com/documentation/avfoundation/avcapturedevice/1390215-isfocusmodesupported" rel="noreferrer noopener nofollow">isFocusModeSupported</a>: method to determine whether a device supports a given <code>focus mode</code></strong>, then set the mode using the focusMode property.</p>
</blockquote>
<p><code>react-native-camera</code> 会在两种不同的场景下改变焦点(你可以在这行用 <code>xcode</code> 设置断点):</p>
<ol>
<li> <a href="https://github.com/react-native-community/react-native-camera/blob/960c5b1c1687742bcadd570f676e2689339e48b0/ios/RCT/RCTCameraManager.m#L1054" rel="noreferrer noopener nofollow"><code>focusWithMode</code></a>仅当您的前置摄像头支持 <code>isFocusPointOfInterestSupported</code> 和 <code>AVCaptureFocusModeContinuousAutoFocus</code></li> 时,该方法才会设置焦点
</ol>
<p>只有当以下条件返回 <code>true</code></p>
<pre><code> &amp;&amp;
</code></pre>
<p>如果条件返回<code>false</code>,则没有<code>autofocus</code>,<strong>但是</strong>图片可能<a href="https://stackoverflow.com/a/17226418/7295772" rel="noreferrer noopener nofollow">be focused on</a>曝光模式 <a href="https://github.com/react-native-community/react-native-camera/blob/960c5b1c1687742bcadd570f676e2689339e48b0/ios/RCT/RCTCameraManager.m#L1059" rel="noreferrer noopener nofollow"><code>;</code></a> </p>
<ol 开始=“2”>
<li> <a href="https://github.com/react-native-community/react-native-camera/blob/1b9fb63e56368f90be2b94e32815e2837b53da41/ios/RN/RNCamera.m#L240" rel="noreferrer noopener nofollow"><code>updateAutoFocusPointOfInterest</code></a>当用户点击屏幕时,根据触摸的 <code>x, y</code> 坐标更改焦点。</li>
</ol>
<p>由于stackoverflow上有几篇帖子(<a href="https://stackoverflow.com/a/17226418/7295772" rel="noreferrer noopener nofollow">post 1</a>、<a href="https://stackoverflow.com/a/18621765/7295772" rel="noreferrer noopener nofollow">post 2</a>、<a href="https://stackoverflow.com/a/17226418/7295772" rel="noreferrer noopener nofollow">post 3</a>、<a href="https://stackoverflow.com/a/9730338/7295772" rel="noreferrer noopener nofollow">post 4</a>)指出不同的iphone版本不支持所有类型的前置摄像头自动对焦,我建议你<strong>在这些代码行上设置断点</strong>并检查<a href="https://developer.apple.com/documentation/avfoundation/avcapturedevice/1390215-isfocusmodesupported" rel="noreferrer noopener nofollow"><code>isFocusModeSupported</code></a>的值和 <a href="https://developer.apple.com/documentation/avfoundation/avcapturedevice/1390436-isfocuspointofinterestsupported" rel="noreferrer noopener nofollow"><code>isFocusPointOfInterestSupported</code></a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于javascript - 前置摄像头无法自动对焦或手动对焦,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/55846066/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/55846066/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: javascript - 前置摄像头无法自动对焦或手动对焦