二维码扫描

此文均在 .m中 


#import <AVFoundation/AVFoundation.h>

#import "QRCodeReaderViewController.h"


@interface ViewController ()<QRCodeReaderDelegate,AVCaptureMetadataOutputObjectsDelegate>//设置代理

{


}


@property (strong,nonatomic)AVCaptureDevice * device;//扫描设备

@property (strong,nonatomic)AVCaptureDeviceInput * input;//输入

@property (strong,nonatomic)AVCaptureMetadataOutput * output;//输出

@property (strong,nonatomic)AVCaptureSession * session;//会话

@property (strong,nonatomic)AVCaptureVideoPreviewLayer * preview;//扫描框


@end



- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    self.view.backgroundColor = [UIColor yellowColor];


//    [self scanBarCode];// 方法一:利用系统的 AVFoundation

    [self QRCodeReader];//方法二:使用 QRCodeReaderViewController 第三方库


}


- (void)scanBarCode

{

    // 取到摄像头设备

    _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    

    // 初始化输入源

    _input = [AVCaptureDeviceInput deviceInputWithDevice:self.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:self.input]){

        [_session addInput:self.input];

    }

    

    if ([_session canAddOutput:self.output]){

        [_session addOutput:self.output];

    }

    

    

    if ([[_output availableMetadataObjectTypes] containsObject:AVMetadataObjectTypeQRCode] || [[_output availableMetadataObjectTypes] containsObject:AVMetadataObjectTypeEAN13Code]){

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

    }

    

    

    //设置输出扫描结果   二维码类型 AVMetadataObjectTypeQRCode,条码类型AVMetadataObjectTypeEAN13Code

    // _output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code];

    _preview = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];

    [_preview setVideoGravity:AVLayerVideoGravityResizeAspectFill];

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

    [self.view.layer addSublayer:_preview];

//     扫码框

//    _preview =[AVCaptureVideoPreviewLayer layerWithSession:self.session];

//    设置扫码框为缩放

//    _preview.videoGravity = AVLayerVideoGravityResizeAspectFill;


   //开始扫描

    [_session startRunning];

}


//系统AVFoundation 扫码结果回调

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

{

    

    NSString *stringValue;

    if ([metadataObjects count] >0)

    {

        AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];

        stringValue = metadataObject.stringValue;

        NSLog(@"扫码结果回调  =%@",stringValue);

    }

//停止扫描

    [_session stopRunning];

}


- (void)QRCodeReader

{

    QRCodeReaderViewController *QR = [[QRCodeReaderViewController alloc]init];

    [QR.codeReader startScanning];


    QR.delegate = self;

    

    [self.navigationController pushViewController:QR animated:YES];

    

// 扫码完成后回调 resultAsString

    [QR setCompletionWithBlock:^(NSString *resultAsString) {

       

        

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

    }];

}


//扫码结果回调

- (void)reader:(QRCodeReaderViewController *)reader didScanResult:(NSString *)result

{

    NSLog(@"------  代理 扫码结果回调  == %@ ",result);

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值