菜鸟教程小白 发表于 2022-12-11 19:27:13

ios rectOfInterest AVCaptureMetadataOutput 忽略


                                            <p><p>我在使用 AVCaptureMetadataOutput 和 rectOfInterest 时遇到了一些扫描限制问题。我已成功绘制 UIImage 以确保扫描区域正确。 </p>

<p>但是,无论我做什么,只有当扫描项目位于屏幕的绝对中心时才有效。 <strong>它似乎忽略了我的直肠。</strong></p>

<p>输出尺寸</p>

<pre><code>Display size width: 414, height: 736
Percent size width: 0.724638, height: 0.407609
Crop search area: {{57, 218}, {300, 300}}
Crop search converted: {{0.29619565217391297, 0.13768115942028991},         {0.40760869565217395, 0.72463768115942029}}
</code></pre>

<p>这是绘制的黄色矩形
<a href="/image/7gORX.jpg" rel="noreferrer noopener nofollow"><img src="/image/7gORX.jpg" alt="enter image description here"/></a> </p>

<p>还有实际的方法。完整代码可以在这里查看 <a href="https://github.com/fbacker/react-native-camera/blob/barcode-finder/ios/RCTCameraManager.m" rel="noreferrer noopener nofollow">https://github.com/fbacker/react-native-camera/blob/barcode-finder/ios/RCTCameraManager.m</a> </p>

<pre><code>- (void)startSession {
#if TARGET_IPHONE_SIMULATOR
return;
#endif
dispatch_async(self.sessionQueue, ^{
    if (self.presetCamera == AVCaptureDevicePositionUnspecified) {
      self.presetCamera = AVCaptureDevicePositionBack;
    }

    AVCaptureStillImageOutput *stillImageOutput = [ init];
    if ()
    {
      stillImageOutput.outputSettings = @{AVVideoCodecKey : AVVideoCodecJPEG};
      ;
      self.stillImageOutput = stillImageOutput;
    }

    AVCaptureMovieFileOutput *movieFileOutput = [ init];
    if ()
    {
      ;
      self.movieFileOutput = movieFileOutput;
    }

    AVCaptureMetadataOutput *metadataOutput = [ init];
    CGRect scanBarcodeArea = CGRectMake(0, 0, 0, 0);
    if () {

      ;
      ;
      ;

      // we only want to scan specified area
      // NOTE; Seems we can only set the actual rect after session started, else it doesn&#39;t work
      if (self.barcodeFinderVisible) {

          NSNumber *imageWidth = ;
          NSNumber *imageHeight = ;
          double imageUseWidth = ;
          double imageUseHeight = ;
          double cropWidth = imageUseWidth * self.barcodeFinderPercentageSizeWidth;
          double cropHeight = imageUseHeight * self.barcodeFinderPercentageSizeHeight;
          double cropX = (imageUseWidth/2)-(cropWidth/2);
          double cropY = (imageUseHeight/2)-(cropHeight/2);
          CGRect scanLimit = CGRectMake(cropX, cropY, cropWidth, cropHeight);
          scanBarcodeArea = ;
          ;

      /* ############################
         DEBUG PURPOSE, get some values and draw yellow rect of actual scanning area
      */

          /*
          NSLog(@&#34;Display size width: %@, height: %@&#34;, imageWidth, imageHeight);
          NSLog(@&#34;Percent size width: %f, height: %f&#34;, self.barcodeFinderPercentageSizeWidth, self.barcodeFinderPercentageSizeHeight);
          NSLog(@&#34;Crop search area: %@&#34;, NSStringFromCGRect(scanLimit));
          NSLog(@&#34;Crop search converted: %@&#34;, NSStringFromCGRect(scanBarcodeArea));

          // PAUSE AND PLAY WILL DRAW YELLOW RECT, IF VALID, might appear auto as well. who knows.
          UIView *scanAreaView = [ initWithFrame:scanLimit];
          scanAreaView.layer.borderColor = .CGColor;
          scanAreaView.layer.borderWidth = 4;
          ;
          */
      }
    }
    self.metadataOutput = metadataOutput;

    __weak RCTCameraManager *weakSelf = self;
    [self setRuntimeErrorHandlingObserver:[NSNotificationCenter.defaultCenter addObserverForName:AVCaptureSessionRuntimeErrorNotification object:self.session queue:nil usingBlock:^(NSNotification *note) {
      RCTCameraManager *strongSelf = weakSelf;
      dispatch_async(strongSelf.sessionQueue, ^{
      // Manually restarting the session since it must have been stopped due to an error.
      ;
      });
    }]];

    ;
});
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>据我所知,</p>

<pre><code>scanBarcodeArea = ;
      ;
</code></pre>

<p>此代码不能总是返回正确的矩形,有时返回 {{0,0},{0,0}},因为我使用矩形导航,当我导航到相机屏幕,然后返回,并导航到相机再次,我得到了 {{0,0},{0,0}},有人说 previewLayer 没有初始好。 </p>

<p>即使我把这段代码放在</p>之后

<pre><code>;
</code></pre>

<p>当我第一次运行我的应用程序并第一次导航到相机屏幕时,也会得到 {{0,0},{0,0}}。</p>

<p>所以,我使用这些打击代码而不是 metadataOutputRectOfInterestForRect: 来计算矩形范围。</p>

<pre><code>      NSNumber *imageWidth = ;
      NSNumber *imageHeight = ;

      double imageUseWidth = ;
      double imageUseHeight = ;
      double cropWidth = imageUseWidth * self.barcodeFinderPercentageSizeWidth;
      double cropHeight = imageUseHeight * self.barcodeFinderPercentageSizeHeight;
      double cropX = (imageUseWidth/2)-(cropWidth/2);
      double cropY = ((imageUseHeight)/2)-(cropHeight/2);

      // transform to camera coordinate
      double scanX = cropY / imageUseHeight;
      double scanY = cropX / imageUseWidth;
      double scanWidth = cropHeight / imageUseHeight;
      double scanHeight = cropWidth / imageUseWidth;

      CGRect rectOfInterest = CGRectMake(scanX, scanY, scanWidth, scanHeight);
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios rectOfInterest AVCaptureMetadataOutput 忽略,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/47817616/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/47817616/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios rectOfInterest AVCaptureMetadataOutput 忽略