iOS自带二维码扫描

作者: Billy_W | 来源:发表于2016-11-29 10:34 被阅读717次
  • 今天项目需要做一个二维码扫描,虽然有很多二维码扫描的第三方可以用,但是考虑到项目中的需要,所以我放弃了使用三方库,而采用了苹果原生的扫描。
  • 原生的二维码扫描有一个坑,那就是扫描范围的确定。只要记得扫描范围是X与Y互换位置,W与H互换位置,就没有什么问题了。
  • 下面进入正题:
    1.因为使用原生二维码扫描,所以需要加入头文件添加delegate
#import <AVFoundation/AVFoundation.h>
<AVCaptureMetadataOutputObjectsDelegate>

2.接着是使用到的类

@property (strong,nonatomic)AVCaptureDevice * device;
@property (strong,nonatomic)AVCaptureDeviceInput * input;
@property (strong,nonatomic)AVCaptureMetadataOutput * output;
@property (strong,nonatomic)AVCaptureSession * session;
@property (weak, nonatomic) IBOutlet UIView *outputView;//xib中扫描的View
@property (strong,nonatomic)AVCaptureVideoPreviewLayer * preview;
@property (strong, nonatomic) NSTimer * timer;//为了做扫描动画的定时器
@property (strong, nonatomic) UIImageView * lineImage;//扫描动画的横线

3.懒加载一个扫描动画的图片

-(UIImageView *)lineImage{
    if (!_lineImage) {
        CGFloat outputW = self.outputView.frame.size.width;
        _lineImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,outputW, 2)];
        _lineImage.image = [UIImage imageNamed:@"ray"];
    }
    return _lineImage;
}

4.使用前的设置,我将它设置在了viewDidLoad当中

-viewDidLoad{
[super viewDidLoad];
   // Device
    _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];
    }
//设置条码类型
    _output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode];
    
    //设置条码位置
    CGFloat X = (ScreenW/2-100)/ScreenW;
    CGFloat Y = (ScreenH/2-100)/ScreenH;
    CGFloat W = 200/ScreenW;
    CGFloat H = 200/ScreenH;
    //设置扫描范围(注意,X与Y交互,W与H交换)
    [_output setRectOfInterest:CGRectMake(Y, X, H, W)];
//添加扫描画面
    _preview =[AVCaptureVideoPreviewLayer layerWithSession:_session];
    _preview.videoGravity =AVLayerVideoGravityResizeAspectFill;
    _preview.frame = CGRectMake(0, 0, ScreenW, ScreenH);//self.view.layer.bounds;
    [self.view.layer insertSublayer:_preview atIndex:0];
    //开始扫描
    [_session startRunning];

 //添加扫描动画定时器
[self.outputView addSubview:self.lineImage];
    // Do any additional setup after loading the view from its nib.
    _timer = [NSTimer scheduledTimerWithTimeInterval:2.5f
                                              target:self
                                            selector:@selector(lineAction)
                                            userInfo:nil
                                             repeats:YES];
}

5.二维码扫描的代理事件

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    NSString *stringValue;
    if ([metadataObjects count] >0){
        //停止扫描
        [_session stopRunning];
       AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
       stringValue = metadataObject.stringValue;//stringValue是扫描拿到的内容,更具内容进行后续工作。
    }
}

6.添加扫描动画的事件

 - (void)lineAction{
    CGFloat outputW = self.outputView.frame.size.width;
    CGFloat outputH = self.outputView.frame.size.height;
    [UIView animateWithDuration:2.4f animations:^{
        CGRect frame = CGRectMake(0, outputH, outputW, 2);
        self.lineImage.frame = frame;
    } completion:^(BOOL finished) {
        CGRect frame = CGRectMake(0, 0, outputW, 2);
        self.lineImage.frame = frame;
    }];
}

7.该变扫描视屏方向

_preview.connection.videoOrientation = [self videoOrientationFromCurrentDeviceOrientation];
-(AVCaptureVideoOrientation) videoOrientationFromCurrentDeviceOrientation {
    switch (self.interfaceOrientation) {
        case UIInterfaceOrientationPortrait: {
            return AVCaptureVideoOrientationPortrait;
        }
        case UIInterfaceOrientationLandscapeLeft: {
            return AVCaptureVideoOrientationLandscapeLeft;
        }
        case UIInterfaceOrientationLandscapeRight: {
            return AVCaptureVideoOrientationLandscapeRight;
        }
        case UIInterfaceOrientationPortraitUpsideDown: {
            return AVCaptureVideoOrientationPortraitUpsideDown;
        }
        default:return AVCaptureVideoOrientationPortrait;
    }
}
  • 搞定......
  • 最后放上一张效果图
原生的扫描就是快,😄

相关文章

  • iOS开发:二维码扫描与生成(扫描篇)

    iOS开发中经常要用到生成二维码与扫描二维码的功能,iOS7开始,系统支持原生的扫描二维码,iOS7 扫描二维码可...

  • 实现一个原声的扫描识别二维码的功能

    二维码扫描识别的有名的第三方库有zbar,zxing,ios7以后iOS自带的AVFoundation框架可以实现...

  • ZBar

    iOS中使用ZBar扫描二维码自定义扫描界面

  • 二维码扫描

    二维码扫描主要使用苹果自带的AVFoundation框架 设置二维码周边黑框 设置二维码扫描layer AVCap...

  • iOS扫描生成二维码

    iOS7以后系统自带了扫描生成二维码的方法,随着版本的迭代,现在的应用已经逐渐可以抛弃iOS6以前的系统,只支持到...

  • avcapture

    iOS人脸识别、自定义相机、图像扫描、系统自带二维码识别基于 OpenCV 的人脸识别 视频捕获 用到的类,主要有...

  • iOS开发之二维码扫描

    iOS开发之二维码扫描 在 iOS7 以前,在IOS中实现二维码和条形码扫描,我们所知的有,两大开源组件 ZBar...

  • IOS中生成二维码、扫描二维码

    title : IOS中生成二维码、扫描二维码category : UI IOS中生成二维码、保存二维码 标签(空...

  • iOS二维码扫描

    前面我们已经学会了iOS二维码生成相关的知识,这篇文章主要是讲解一下iOS二维码扫描相关的知识. 二维码扫描步骤 ...

  • 二维码扫描和创建(上)

    扫描二维码(包括读取和解码) 扫描二维码OC的开源库有ZBar和ZXing。iOS7以后iOS拥有原生的扫码功能。...

网友评论

本文标题:iOS自带二维码扫描

本文链接:https://www.haomeiwen.com/subject/czsapttx.html