参考网址
http://www.jianshu.com/p/3fb24fc7b415
全屏显示局部扫描一直有问题 设置RectOfInterest也有问题,方法走,就是效果达不到 发现添加要写在 中间
[self.view.layer insertSublayer:_preview atIndex:0];
[self updateLayout];
[_session startRunning];
- (void)defaultConfig {
_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// Input
_input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
// Output
_output = [[AVCaptureMetadataOutput alloc]init];
[_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
// Session
_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];
}
AVCaptureConnection *outputConnection = [_output connectionWithMediaType:AVMediaTypeVideo];
outputConnection.videoOrientation = [QRUtil videoOrientationFromCurrentDeviceOrientation];
//条形码二维码都支持
_output.metadataObjectTypes = @[AVMetadataObjectTypeEAN13Code,
AVMetadataObjectTypeEAN8Code,
AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypeQRCode];
//只支持二维码
// _output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
// Preview
_preview =[AVCaptureVideoPreviewLayer layerWithSession:_session];
_preview.videoGravity =AVLayerVideoGravityResize;
_preview.frame =[QRUtil screenBounds];
[self.view.layer insertSublayer:_preview atIndex:0];
[self updateLayout];
_preview.connection.videoOrientation = [QRUtil videoOrientationFromCurrentDeviceOrientation];
[_session startRunning];
}
- (void)updateLayout {
_qrView.center = CGPointMake([QRUtil screenBounds].size.width / 2, [QRUtil screenBounds].size.height / 2);
//修正扫描区域
CGFloat screenHeight = self.view.frame.size.height;
CGFloat screenWidth = self.view.frame.size.width;
CGRect cropRect = CGRectMake((screenWidth - self.qrView.transparentArea.width) / 2,
(screenHeight - self.qrView.transparentArea.height) / 2,
self.qrView.transparentArea.width,
self.qrView.transparentArea.height);
// 设置AVCaptureMetadataOutput 的rectOfInterest 的属性就可以了 这样设置就可以:CGRectMake(y的起点/屏幕的高,x的起点/屏幕的宽,扫描的区域的高/屏幕的高,扫描的区域的宽/屏幕的宽)
[_output setRectOfInterest:CGRectMake(cropRect.origin.y / screenHeight,
cropRect.origin.x / screenWidth,
cropRect.size.height / screenHeight,
cropRect.size.width / screenWidth)];
}