如何使用AVFoundation设置扫描范围 [英] How to set scanning bound using AVFoundation
问题描述
我正在我的一个iOS应用程序中尝试使用条形码扫描仪.我已经成功实现了条形码扫描仪.
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];
推荐答案
回答这个问题也许来晚了,但我自己已经克服了这个问题.因此,希望以后能帮助别人.
Maybe it's late to answer this question, but I've just overcome this issue myself. So, hope to help others later.
关键字是AVCaptureMetadataOutput的rectOfInterest,这是我的设置方法.
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);
有关更多详细信息,您可以从Apple Inc.检查文档.
For more detail, you can check Document from Apple Inc.
祝你好运. :)
这篇关于如何使用AVFoundation设置扫描范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!