iOS二维码扫描

导入相关配置

#import <AVFoundation/AVFoundation.h>

#define kScreenHight     [UIScreen mainScreen].bounds.size.height

#define kScreenWidth     [UIScreen mainScreen].bounds.size.width

 

@interface RootViewController ()<AVCaptureMetadataOutputObjectsDelegate>

{

    AVCaptureDevice *_device;

    AVCaptureDeviceInput *_input;

    AVCaptureMetadataOutput *_output;

    AVCaptureSession *_session;

    AVCaptureVideoPreviewLayer *_layer;

}

初始化控件

- (void)initDevice{

    _device  = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    _input   = [[AVCaptureDeviceInput alloc] initWithDevice:_device error:nil];

    _output  = [[AVCaptureMetadataOutput alloc] init];

    [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];

    _session = [[AVCaptureSession alloc] init];

    [_session setSessionPreset:AVCaptureSessionPresetHigh];

    if ([_session canAddInput:_input]) {

        [_session addInput:_input];

    }

    if ([_session canAddOutput:_output]) {

        [_session addOutput:_output];

    }

    _layer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_session];

    [_layer setFrame:self.view.layer.bounds];

    

    [_output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code]];

//设置扫描的类型 二维码 条形码等

    [_output setRectOfInterest:CGRectMake(((kScreenHight/4+64)*1.0)/kScreenHight,0.250.5, 0.5)];

//相对于屏幕宽高的值 x,y值的坐标互换 应注意

    _layer.videoGravity = AVLayerVideoGravityResizeAspectFill;

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

    [_session startRunning];

    [self configUI];

}

 

 //实现代理方法

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

    NSString *stringValue ;

    if ([metadataObjects count]>0) {

        [_session stopRunning];

        [timer setFireDate:[NSDate distantFuture]];

        AVMetadataMachineReadableCodeObject *metaData = [metadataObjects objectAtIndex:0];

        stringValue = metaData.stringValue;

    }

  //stringValue 为扫描结果      

  UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"二维码扫描" message:stringValue delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

    [al show];

}

 

以上系统二维码就完成了

相关美化  (可以动的线)

_lineView                     = [[UIView alloc] initWithFrame:CGRectMake(kScreenWidth/4+2, kScreenHight/4+64+5, kScreenWidth/2-4, 2)];

    [_lineView setBackgroundColor:[UIColor blackColor]];

    _lineView.layer.cornerRadius  = 5;

    _lineView.layer.masksToBounds = YES;

    _lineView.alpha               = 0.6;

    [self.view addSubview:_lineView];

    timer =[NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:@selector(moveLine) userInfo:nil repeats:YES];

 

- (void)moveLine{

        [UIView animateWithDuration:0.8 animations:^{

            [_lineView setFrame:CGRectMake(kScreenWidth/4+2, kScreenHight/4+64+5+kScreenWidth/2-10, kScreenWidth/2-4, 2)];

        } completion:^(BOOL finished) {

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                [UIView animateWithDuration:0.8 animations:^{

                    [_lineView setFrame:CGRectMake(kScreenWidth/4+2, kScreenHight/4+64+5, kScreenWidth/2-4, 2)];

                    

                }];

            });

            

        }];

}

 

周围模糊效果

- (void)configUI{

    UIView *view1                 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHight/4 + 64)];

    [view1 setBackgroundColor:[UIColor blackColor]];

    view1.alpha                   = 0.6;

    [self.view addSubview:view1];

 

    UIView *view2                 = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHight/4+64, kScreenWidth/4, kScreenWidth/2)];

    [view2 setBackgroundColor:[UIColor blackColor]];

    view2.alpha                   = 0.6;

    [self.view addSubview:view2];

 

    UIView *view3                 = [[UIView alloc] initWithFrame:CGRectMake((kScreenWidth*3)/4, kScreenHight/4+64, kScreenWidth/2, kScreenWidth/2)];

    [view3 setBackgroundColor:[UIColor blackColor]];

    view3.alpha                   = 0.6;

    [self.view addSubview:view3];

 

    UIView *view4                 = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHight/4+64+kScreenWidth/2, kScreenWidth, kScreenHight -(kScreenHight/4+64+kScreenWidth/2))];

    [view4 setBackgroundColor:[UIColor blackColor]];

    view4.alpha                   = 0.6;

    [self.view addSubview:view4];

 }

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。