iOS 系统方法实现扫一扫功能

导入头文件:

#import 
#import 

代理:


AV...haha

@property (strong,nonatomic) AVCaptureDevice *avDevice;
@property (strong,nonatomic) AVCaptureDeviceInput *avInput;
@property (strong,nonatomic) AVCaptureMetadataOutput *avOutput;
@property (strong,nonatomic) AVCaptureSession *avSession;
@property (strong,nonatomic) AVCaptureVideoPreviewLayer *avPreview;

当每次进入这个扫一扫页面时,判断是否开启了权限,
如果没有开启相机权限"弹出提示页"
点击跳转设置页面;

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    /*
     typedef NS_ENUM(NSInteger, AVAuthorizationStatus) {
     AVAuthorizationStatusNotDetermined = 0,// 系统还未知是否访问,第一次开启相机时
     AVAuthorizationStatusRestricted, // 受限制的
     AVAuthorizationStatusDenied, //不允许
     AVAuthorizationStatusAuthorized // 允许状态
     } NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
     */
    AVAuthorizationStatus authStatus =  [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied) {
        UIAlertView *alt = [[UIAlertView alloc]initWithTitle:@"\"AppName\"想访问您的相机" 
        message:@"是否允许此App使用你的相机?"
        delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"立即开启", nil];
        [alt show];
    }
}

CaptureSession 这是个捕获会话,也就是说你可以用这个对象从输入设备捕获数据流。 AVCaptureVideoPreviewLayer 可以通过输出设备展示被捕获的数据流。

- (void)setupCamera{
    self.avDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    self.avInput = [AVCaptureDeviceInput deviceInputWithDevice:self.avDevice error:nil];
    self.avOutput = [[AVCaptureMetadataOutput alloc]init];
    [self.avOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
    self.avSession = [[AVCaptureSession alloc]init];
    [self.avSession setSessionPreset:AVCaptureSessionPresetHigh];
    if ([self.avSession canAddInput:self.avInput]){
        [self.avSession addInput:self.avInput];
    } 
    if ([self.avSession canAddOutput:self.avOutput]){
        [self.avSession addOutput:self.avOutput];
    }
    
    // 二维码类型 AVMetadataObjectTypeQRCode
    if ([self.avOutput.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeQRCode]){
        self.avOutput.metadataObjectTypes = [NSArray arrayWithObject:AVMetadataObjectTypeQRCode];
       // AVMetadataObjectTypeAztecCode 条形码
    } else{
        return ; }
    
    self.avPreview =[AVCaptureVideoPreviewLayer layerWithSession:self.avSession];
    self.avPreview.videoGravity = AVLayerVideoGravityResizeAspectFill;
    //全屏扫一扫
     self.avPreview.frame =CGRectMake(0, 64, KScreenWidth, KScreenHeight-64);
    [self.view.layer insertSublayer:self.avPreview atIndex:0];
    [self.avSession startRunning];//开始扫描
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:
(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    if ([metadataObjects count] > 0){
        [self.avSession stopRunning];//停止扫描
        AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
        NSString *stringValue = metadataObject.stringValue;//扫描出来的字符串
    }
    //NSLog(@"%@",metadataObjects);
    //我这里扫描出来的是json字符串,转字典,拿到自己需要用的字段;
    NSData *JSONData = [stringValue dataUsingEncoding:NSUTF8StringEncoding];
    dic = [NSJSONSerialization JSONObjectWithData:JSONData 
                                                   options:NSJSONReadingMutableLeaves error:nil];
//这里判断是否是URL地址链接
        NSString *regex =@"[a-zA-z]+://[^\\s]*";
        NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
        if ([urlTest evaluateWithObject:stringValue]) {
            //如果是跳转浏览器
            NSURL *url = [[NSURL alloc] initWithString: stringValue ];
            [[UIApplication sharedApplication] openURL:url];
            [self.avSession startRunning];//重新开始扫描
         }
}

到这里系统扫描功能就结束了

你可能感兴趣的:(iOS 系统方法实现扫一扫功能)