#import <AVFoundation/AVFoundation.h>
@interface QRCodeViewController ()<AVCaptureMetadataOutputObjectsDelegate>
@property (nonatomic, strong) AVCaptureSession *session;
@end
viewDidLoad:
// 摄像头设备
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//设备输入
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
_session = [[AVCaptureSession alloc]init];
if ([_session canAddInput:input])
{
[_session addInput:input];
}
AVCaptureVideoPreviewLayer *layer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
//手动给layer配置frame值
layer.frame = self.view.bounds;
[self.view.layer addSublayer:layer];
AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc]init];
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
//添加输出到会话中
if ([_session canAddOutput:output])
{
[_session addOutput:output];
}
//查看支持的解析类型
NSLog(@"%@",output.availableMetadataObjectTypes);
output.metadataObjectTypes = @[@"org.iso.QRCode"];
//200 *200
CGFloat width = 200/self.view.bounds.size.width;
CGFloat height = 200/self.view.bounds.size.height;
CGFloat x = ((self.view.bounds.size.width - 200) * 0.5) / self.view.bounds.size.width;
CGFloat y = ((self.view.bounds.size.height - 200) * 0.5) / self.view.bounds.size.height;
output.rectOfInterest = CGRectMake(y, x, height, width);
- (void)addOtherLay:(CGRect)rect
{
//Rect为保留的矩形frame值
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
CGFloat leftWidth = (screenWidth - rect.size.width) * 0.5;
CAShapeLayer *layerTop = [CAShapeLayer layer];
layerTop.fillColor = [UIColor blackColor].CGColor;
layerTop.opacity = 0.5;
layerTop.path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, screenWidth, rect.origin.y)].CGPath;
[self.view.layer addSublayer:layerTop];
CAShapeLayer *layerLeft = [CAShapeLayer layer];
layerLeft.fillColor = [UIColor blackColor].CGColor;
layerLeft.opacity = 0.5;
layerLeft.path = [UIBezierPath bezierPathWithRect:CGRectMake(0, rect.origin.y, leftWidth, rect.size.height)].CGPath;
[self.view.layer addSublayer:layerLeft];
CAShapeLayer *layerRight = [CAShapeLayer layer];
layerRight.fillColor = [UIColor blackColor].CGColor;
layerRight.opacity = 0.5;
layerRight.path = [UIBezierPath bezierPathWithRect:CGRectMake(screenWidth - leftWidth, rect.origin.y, leftWidth, rect.size.height)].CGPath;
[self.view.layer addSublayer:layerRight];
CAShapeLayer *layerBottom = [CAShapeLayer layer];
layerBottom.fillColor = [UIColor blackColor].CGColor;
layerBottom.opacity = 0.5;
layerBottom.path = [UIBezierPath bezierPathWithRect:CGRectMake(0, CGRectGetMaxY(rect), screenWidth, screenHeight - CGRectGetMaxY(rect))].CGPath;
[self.view.layer addSublayer:layerBottom];
}
//添加遮盖
[self addOtherLay:CGRectMake((self.view.bounds.size.width - 200) * 0.5, (self.view.bounds.size.height - 200) * 0.5, 200, 200)];
[_session startRunning];
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
AVMetadataMachineReadableCodeObject *codeObject = metadataObjects.lastObject;
NSLog(@"%@",codeObject.stringValue);
[_session stopRunning];
}
到这里为止,就完成了二维码的扫描:
原文:http://blog.csdn.net/yi_zz32/article/details/50545240