How to set scanning bound using AVFoundation

与世无争的帅哥 提交于 2019-12-11 05:28:09

问题


I am trying barcode scanner in one of my iOS application. I had successfully implemented the barcode scanner.

But currently barcode scanning is displayed in full screen only. But what I want is that, the video should be viewed in full screen and the barcode should be scanned in particular portion only. That is, if the barcode is placed in that portion then only it should be displayed. Below is my current code:

session=[[AVCaptureSession alloc]init];
device=[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error=nil;

input=[AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (input) {
    [session addInput:input];
}
else{
    NSLog(@"Errod : %@",error);
}

output=[[AVCaptureMetadataOutput alloc]init];
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
[session addOutput:output];

output.metadataObjectTypes=[output availableMetadataObjectTypes];

prevLayer=[AVCaptureVideoPreviewLayer layerWithSession:session];
[prevLayer setFrame:self.view.bounds];
[prevLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[self.view.layer addSublayer:prevLayer];

[session startRunning];

[self.view bringSubviewToFront:self.lblCode];
[self.view bringSubviewToFront:self.imgShade1];
[self.view bringSubviewToFront:self.imgShade2];

回答1:


This is what you are after:

CGRect visibleMetadataOutputRect = [prevLayer metadataOutputRectOfInterestForRect:areaOfInterest];

output.rectOfInterest = visibleMetadataOutputRect;

where areaOfInterest is a CGRect. Hope this solves the issue.




回答2:


Maybe it's late to answer this question, but I've just overcome this issue myself. So, hope to help others later.

The keyword is rectOfInterest of AVCaptureMetadataOutput, and here's how I set mine.

CGSize size = self.view.bounds.size;
_output.rectOfInterest = CGRectMake(cropRect.origin.y/size.height, cropRect.origin.x/size.width, cropRect.size.height/size.height, cropRect.size.width/size.width);

For more detail, you can check Document from Apple Inc.

Good Luck. :)



来源:https://stackoverflow.com/questions/23284762/how-to-set-scanning-bound-using-avfoundation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!