如何使用AVFoundation设置扫描绑定

我正在尝试我的iOS应用程序中的条形码扫描仪。 我已经成功实施了条码扫描器。

但目前条形码扫描仅以全屏显示。 但是我想要的是,应该全屏查看video,并且应该只扫描特定部分的条形码。 也就是说,如果条形码放在那个部分,那么只有它应该被显示。 以下是我目前的代码:

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]; 

这是你以后的事情:

 CGRect visibleMetadataOutputRect = [prevLayer metadataOutputRectOfInterestForRect:areaOfInterest]; output.rectOfInterest = visibleMetadataOutputRect; 

areaOfInterest是一个CGRect。 希望这可以解决这个问题。

也许这个问题回答迟了,但是我自己也解决了这个问题。 所以,希望以后能帮助别人。

关键字是AVCaptureMetadataOutput的rectOfInterest,这里是我如何设置我的。

 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.的文档

祝你好运。 🙂