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

ios rectOfInterest AVCaptureMetadataOutput 忽略

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

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

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

输出尺寸

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}}

这是绘制的黄色矩形 enter image description here

还有实际的方法。完整代码可以在这里查看 https://github.com/fbacker/react-native-camera/blob/barcode-finder/ios/RCTCameraManager.m

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

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

    AVCaptureMovieFileOutput *movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
    if ([self.session canAddOutput:movieFileOutput])
    {
      [self.session addOutput:movieFileOutput];
      self.movieFileOutput = movieFileOutput;
    }

    AVCaptureMetadataOutput *metadataOutput = [[AVCaptureMetadataOutput alloc] init];
    CGRect scanBarcodeArea = CGRectMake(0, 0, 0, 0);
    if ([self.session canAddOutput:metadataOutput]) {

        [self.session addOutput:metadataOutput];
        [metadataOutput setMetadataObjectsDelegate:self queue:self.sessionQueue];
        [metadataOutput setMetadataObjectTypes:self.barCodeTypes];

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

          NSNumber *imageWidth = [NSNumber numberWithFloat:self.previewLayer.frame.size.width];
          NSNumber *imageHeight = [NSNumber numberWithFloat:self.previewLayer.frame.size.height];
          double imageUseWidth = [imageWidth doubleValue];
          double imageUseHeight = [imageHeight doubleValue];
          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 = [_previewLayer metadataOutputRectOfInterestForRect:scanLimit];
          [metadataOutput setRectOfInterest:scanBarcodeArea];

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

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

          // PAUSE AND PLAY WILL DRAW YELLOW RECT, IF VALID, might appear auto as well. who knows.
          UIView *scanAreaView = [[UIView alloc] initWithFrame:scanLimit];
          scanAreaView.layer.borderColor = [UIColor yellowColor].CGColor;
          scanAreaView.layer.borderWidth = 4;
          [self.view addSubview:scanAreaView];
          */
        }
    }
    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.
        [strongSelf.session startRunning];
      });
    }]];

    [self.session startRunning];
  });
}



Best Answer-推荐答案


据我所知,

scanBarcodeArea = [_previewLayer metadataOutputRectOfInterestForRect:scanLimit];
      [metadataOutput setRectOfInterest:scanBarcodeArea];

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

即使我把这段代码放在

之后
[self.session startRunning];

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

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

        NSNumber *imageWidth = [NSNumber numberWithFloat:self.previewLayer.frame.size.width];
        NSNumber *imageHeight = [NSNumber numberWithFloat:self.previewLayer.frame.size.height];

        double imageUseWidth = [imageWidth doubleValue];
        double imageUseHeight = [imageHeight doubleValue];
        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);

关于ios rectOfInterest AVCaptureMetadataOutput 忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47817616/

回复

使用道具 举报

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

本版积分规则

关注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