星空网 > 软件开发 > 操作系统

iOS 自带AVFoundation库实现二维码扫描

.h里

#import <AVFoundation/AVFoundation.h>

@interface ErWeiViewController : UIViewController<AVCaptureMetadataOutputObjectsDelegate>

-(BOOL)startReading;

-(void)stopReading;

 

//捕捉会话

@property (nonatomic, strong) AVCaptureSession *captureSession;

//展示layer

@property (nonatomic, strong) AVCaptureVideoPreviewLayer *videoPreviewLayer;

@property(nonatomic, assign)BOOL isReading;

@property (strong, nonatomic) UIView *boxView;

@property (strong, nonatomic) CALayer *scanLayer;

 

@property (strong, nonatomic)  UIView *viewPreview;

@property (strong, nonatomic)  UILabel *lblStatus;

@property (strong, nonatomic)  UIButton *startBtn;

- (void)startStopReading:(id)sender;

 

 

.m里

- (void)viewDidLoad {

    [super viewDidLoad];

       _captureSession = nil;

    _isReading = NO;

    [self startReading];

    

}

-(BOOL)startReading {

    _viewPreview = [[UIView alloc]initWithFrame:self.view.bounds];

    [self.view addSubview:_viewPreview];

    NSError *error;

    //1.初始化捕捉设备(AVCaptureDevice),类型为AVMediaTypeVideo

    AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    //2.用captureDevice创建输入流

    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];

    if (!input) {

        NSLog(@"%@", [error localizedDescription]);

        return NO;

    }

    //3.创建媒体数据输出流

    AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];

    //4.实例化捕捉会话

    _captureSession = [[AVCaptureSession alloc] init];

    //4.1.将输入流添加到会话

    [_captureSession addInput:input];

    //4.2.将媒体输出流添加到会话中

    [_captureSession addOutput:captureMetadataOutput];

    //5.创建串行队列,并加媒体输出流添加到队列当中

    dispatch_queue_t dispatchQueue;

    dispatchQueue = dispatch_queue_create("myQueue", NULL);

    //5.1.设置代理

    [captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];

    //5.2.设置输出媒体数据类型为所有类型

captureMetadataOutput.metadataObjectTypes=@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];

    //6.实例化预览图层

    _videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];

    //7.设置预览图层填充方式

    [_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

    //8.设置图层的frame

    [_videoPreviewLayer setFrame:_viewPreview.layer.bounds];

    //9.将图层添加到预览view的图层上

    [_viewPreview.layer addSublayer:_videoPreviewLayer];

    //10.设置扫描范围

    captureMetadataOutput.rectOfInterest = CGRectMake(0.2f, 0.2f, 0.8f, 0.8f);

    //10.1.扫描框

    _boxView = [[UIView alloc] initWithFrame:CGRectMake(_viewPreview.bounds.size.width * 0.2f, _viewPreview.bounds.size.height * 0.2f, _viewPreview.bounds.size.width - _viewPreview.bounds.size.width * 0.4f, _viewPreview.bounds.size.height - _viewPreview.bounds.size.height * 0.4f)];

    _boxView.layer.borderColor = [UIColor greenColor].CGColor;

    _boxView.layer.borderWidth = 1.0f;

    [_viewPreview addSubview:_boxView];

    //10.2.扫描线

    _scanLayer = [[CALayer alloc] init];

    _scanLayer.frame = CGRectMake(0, 0, _boxView.bounds.size.width, 1);

    _scanLayer.backgroundColor = [UIColor brownColor].CGColor;

    [_boxView.layer addSublayer:_scanLayer];

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.2f target:self selector:@selector(moveScanLayer:) userInfo:nil repeats:YES];

    [timer fire];

    

    //10.开始扫描

    [_captureSession startRunning];

    return YES;

}

 

#pragma mark - AVCaptureMetadataOutputObjectsDelegate协议方法

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection

{

    //判断是否有数据

    if (metadataObjects != nil && [metadataObjects count] > 0) {

        AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];

        //判断回传的数据类型  captureMetadataOutput.metadataObjectTypes  数组里的类型进行遍历对比 添加if判断类型,

//        下面只以AVMetadataObjectTypeQRCode 为例子

//        if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {

//            [_lblStatus performSelectorOnMainThread:@selector(setText:) withObject:[metadataObj stringValue] waitUntilDone:NO];

//            NSLog(@"11111 = %@", _lblStatus.text);

//            [self performSelectorOnMainThread:@selector(stopReading) withObject:nil waitUntilDone:NO];

//            _isReading = NO;

//        }

 

    }

    [self dismissViewControllerAnimated:YES completion:^{

        

        

    }];

 

}

//实现计时器方法moveScanLayer:(NSTimer *)timer

 

- (void)moveScanLayer:(NSTimer *)timer

{

    CGRect frame = _scanLayer.frame;

    if (_boxView.frame.size.height < _scanLayer.frame.origin.y) {

        frame.origin.y = 0;

        _scanLayer.frame = frame;

    }else{

        frame.origin.y += 5;

        [UIView animateWithDuration:0.1 animations:^{

            _scanLayer.frame = frame;

        }];

    }

}

- (void)startStopReading:(id)sender {

    if (!_isReading) {

        if ([self startReading]) {

            [_startBtn setTitle:@"Stop" forState:UIControlStateNormal];

            [_lblStatus setText:@"Scanning for QR Code"];

        }

    }

    else{

        [self stopReading];

        [_startBtn setTitle:@"Start!" forState:UIControlStateNormal];

    }

    _isReading = !_isReading;

}

-(void)stopReading{

    [_captureSession stopRunning];

    _captureSession = nil;

    [_scanLayer removeFromSuperlayer];

    [_videoPreviewLayer removeFromSuperlayer];

}

 




原标题:iOS 自带AVFoundation库实现二维码扫描

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

VAT税务报税指南把握报税细节轻松缴税:https://www.kjdsnews.com/a/1339106.html
VAT税务报税指南完整介绍:https://www.kjdsnews.com/a/1339107.html
注册欧盟商标的优势:https://www.kjdsnews.com/a/1339108.html
鲍威尔重磅讲话“吓崩”市场,非美货币集体重挫,人民币差点就破“7”了:https://www.kjdsnews.com/a/1339109.html
美国FBA海运 美国海运专线服务解析:https://www.kjdsnews.com/a/1339110.html
3.8妇女节品牌推广:5大海外社媒营销案例分享!:https://www.kjdsnews.com/a/1339111.html
16个独特的商业创意,带你收获真正赚钱的创业理念:https://www.kjdsnews.com/a/1840836.html
2017天台景区收门票吗 2017天台景区收门票吗多少钱:https://www.vstour.cn/a/404226.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流