更新!!!
我正在使用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}}
请帮我解决。
答案 0 :(得分:2)
我找到了一个简单的解决方案,它对我有用。
在[_captureSession startRunning]
CGRect visibleMetadataOutputRect = [_videoPreviewLayer metadataOutputRectOfInterestForRect:self.vFocus.frame];
[_captureMetadataOutput setRectOfInterest:visibleMetadataOutputRect];
效果很好!