)setupScanQRCode { _session = [[
AVCaptureSession alloc] init]; [_session setSessionPreset:
AVCaptureSessionPresetHigh];
if ([_session canAddInput:
self.input]) { [_session addInput:
self.input]; }
if ([_session canAddOutput:
self.output]) { [_session addOutput:
self.output]; } [
self.output setMetadataObjectTypes:
self.output.availableMetadataObjectTypes]; [
self setOutputInterest]; _preview = [
AVCaptureVideoPreviewLayer layerWithSession:_session]; _preview.videoGravity =
AVLayerVideoGravityResizeAspectFill; _preview.frame =
self.view.layer.bounds;
self.view.layer.backgroundColor = [[
UIColor blackColor]
CGColor]; [
self.view.layer insertSublayer:_preview atIndex:
0];
self.maskLayer = [[
CALayer alloc]init];
self.maskLayer.frame =
self.view.layer.bounds;
self.maskLayer.delegate =
self; [
self.view.layer insertSublayer:
self.maskLayer above:_preview]; [
self.maskLayer setNeedsDisplay];}
设置扫描范围
关于扫描范围,这是一个坑,稍稍不注意,就会踩进去了。扫描的范围是通过这个参数rectOfInterest来设置的,但这个参数不是普通的CGRect,而是0~1的一个范围比例。正确的创建为CGRectMake(y/Height,x/Width,height/Height,width/Width),这里左边是扫描区域的x,y,width,height,右边的是当前控制器view的Width和Height。具体的代码实现如下:
5/9 首页 上一页 3 4 5 6 7 8 下一页 尾页