OStack程序员社区-中国程序员成长平台

标题: ios - objective-c metadataOutputRectOfInterestForRect 返回 {{0, 0}, {0, 0}} [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 03:37
标题: ios - objective-c metadataOutputRectOfInterestForRect 返回 {{0, 0}, {0, 0}}

更新!!!

我正在使用 metadataOutputRectOfInterestForRect 来转换矩形,以便使用 AVCaptureMetadataOutput rectOfInterest。但是当翻译成 CGRect 时,它总是返回 0。

这是我的代码:

NSError *error;

AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];

if (!input) {
    NSLog(@"%@", [error localizedDescription]);
    return NO;
}

_captureSession = [[AVCaptureSession alloc] init];
[_captureSession addInput:input];

_videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
[_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[_videoPreviewLayer setFrame:_viewPreview.bounds];
[_viewPreview.layer addSublayer:_videoPreviewLayer];
[_viewPreview bringSubviewToFront:_lblGuide];
[_viewPreview bringSubviewToFront:_vFocus];

CGRect visibleMetadataOutputRect = [_videoPreviewLayer metadataOutputRectOfInterestForRect:CGRectMake(0, 0, 100, 100)];
NSLog(@"--- log convert: %@", NSStringFromCGRect(visibleMetadataOutputRect));
NSLog(@"--- log _videoPreviewLayer: %@", NSStringFromCGRect(_videoPreviewLayer.bounds));
NSLog(@"--- log _viewPreview: %@", NSStringFromCGRect(_viewPreview.bounds));

_captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
[_captureSession addOutput:_captureMetadataOutput];

_dispatchQueue = dispatch_queue_create("myQueue", NULL);
[_captureMetadataOutput setMetadataObjectsDelegate:self queue:_dispatchQueue];
[_captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]];
[_captureMetadataOutput setRectOfInterest:visibleMetadataOutputRect];

[_captureSession startRunning];

return YES;

NSlog 结果:

[5082:1768978] --- log convert: {{0, 0}, {0, 0}}
[5082:1768978] --- log _videoPreviewLayer: {{0, 0}, {320, 468}}
[5082:1768978] --- log _viewPreview: {{0, 0}, {320, 468}}

请帮我解决。



Best Answer-推荐答案


我找到了一个简单的解决方案,它对我有用。

只需在 [_captureSession startRunning]

之后移动 2 行代码
CGRect visibleMetadataOutputRect = [_videoPreviewLayer metadataOutputRectOfInterestForRect:self.vFocus.frame];
    [_captureMetadataOutput setRectOfInterest:visibleMetadataOutputRect];

效果很好!

关于ios - objective-c metadataOutputRectOfInterestForRect 返回 {{0, 0}, {0, 0}},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47628533/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4