首页 > 安全资讯 >

扫描二维码

16-01-20

其实扫描二维码,用到了这个框架: import @interface QRCodeViewController () @property (nonatomic, strong) AVCaptureSession *session; @end viewDidLoad: 摄像头设备 摄像头设备

其实扫描二维码,用到了这个框架:
#import 
@interface QRCodeViewController ()

@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"];
设置扫描区域
注意rectOfInterest,这个方法里面的顺序是:y,x,height,width
    //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];
}

到这里为止,就完成了二维码的扫描:
扫描结果

相关文章
最新文章
热点推荐