扫描二维码

  • 其实扫描二维码,用到了这个框架:
#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"];
  • 设置扫描区域
    • 注意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];
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值