更新!!!
我正在使用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}}
请帮我解决。
最佳答案
我找到了一个简单的解决方案,它为我工作。
只需在[_captureSession startRunning]
之后移动两行代码
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/