iOS,二维码

懒方法加载,边框和扫描线

-(UIImageView *)bgImageView{

    

    if (!_bgImageView) {

        _bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width - 280 ) /2.0, 110, 280, 280)];

        _bgImageView.image = [UIImage imageNamed:@"pick_bg"];

    }

    

    return _bgImageView;

    

}

 

-(UIImageView *)lineImageView{

    

    if (!_lineImageView) {

        _lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width - 280 ) /2.0, 110, 280, 2)];

        _lineImageView.image = [UIImage imageNamed:@"line"];

    }

    return _lineImageView;

}

 

 

#life——cycle

- (void)viewDidLoad {

    [super viewDidLoad];

    // IOS7之前,都是使用第三方库zbar来实现扫面二维码的功能,IOS7之后系统提供了二维码扫描的方法(真机有效)

    self.view.backgroundColor = [UIColor lightGrayColor];

    

    [self.view addSubview:self.bgImageView];

    [self.view addSubview:self.lineImageView];

    

    //开始扫描二维码(条形码)

    [self readQrcode];

}

 

 

——创建二维码

-(void)readQrcode{

    

    //实例化设备对象

    self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    

    NSError *error;

    //实例化设备输入

    self.input = [[AVCaptureDeviceInput alloc] initWithDevice:self.device error:&error];

    

    if (error) {

        NSLog(@"error = %@",error);

    }

    

    

    //实例化设备输出

    self.output = [AVCaptureMetadataOutput new];

    //设置代理

    [self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];

    

    //实例化扫描会话

    self.session = [AVCaptureSession new];

    

    //添加会话输入

    [self.session addInput:self.input];

    //添加会话输出

    [self.session addOutput:self.output];

    

    //设置输出支持的二维码或者条形码类型(一定要添加到会话之后再设置类型,否则会崩溃)

    [self.output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeCode39Mod43Code,AVMetadataObjectTypeCode93Code,AVMetadataObjectTypeCode128Code]];

    

    //添加扫描图层

    AVCaptureVideoPreviewLayer *layer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];

    

    //设置图层布局(铺满)

    layer.videoGravity = AVLayerVideoGravityResizeAspectFill;

    

    //设置图层大小和位置

    layer.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - 280 ) /2.0, 110, 280, 280);

    

    //把扫描的图层出入到self.View的最底层layer当中

    [self.view.layer insertSublayer:layer atIndex:0];

    

    //开始扫描

    [self.session startRunning];

    

    //循环移动扫描线

    [self lineMoveAround];

    

}

 

 

-(void)lineMoveAround{

    

 

    [UIView animateWithDuration:2.0f animations:^{

        

        //移动到最底下

        CGRect rect = self.lineImageView.frame;

        rect.origin.y +=280;

        self.lineImageView.frame = rect;

        

    } completion:^(BOOL finished) {

        

        [UIView animateWithDuration:2.0f animations:^{

            

            //移动到最顶上

            CGRect rect = self.lineImageView.frame;

            rect.origin.y -=280;

            self.lineImageView.frame = rect;

            

        } completion:^(BOOL finished) {

            //利用递归

            [self lineMoveAround];

            

            

        }];

        

        

    }];

    

    

}

 

 

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    

    [self dismissViewControllerAnimated:YES completion:nil];

    

}

 

 

#pragma mark- AVCaptureMetadataOutputObjectsDelegate

//扫描成功之后的回调方法

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

    

    //扫描跳转到对应应用的AppStore

    

    //停止扫描

    [self.session stopRunning];

    

    //openURL跳转到其他应用(AppStore)

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[metadataObjects[0] stringValue]]];

    

    

}

 

posted @ 2015-11-05 10:34  handsomeBoys  阅读(139)  评论(0)    收藏  举报