标签:
iOS7以前都是用Zxing Zbar 来实现 扫描二维码以及条形码,从iOS7 出来后,系统自带API很强大,渐渐也就放弃了Zxing Zbar的使用,
直接上主要代码:
- (void)setupCamera { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [HUDUtils showHUDProgress:@""]; AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; if (authStatus == AVAuthorizationStatusAuthorized) {//已经授权 }else { //没有授权 NSString *recommand; if (IS_IOS_8) { recommand = @"无法打开相机,请去设置中打开后继续操作\n设置->好点商家->相机"; }else{ recommand = @"无法接收推送消息,请去设置中打开后继续操作\n设置->隐私->相机->好点商家"; } UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:recommand delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil]; [alert show]; return; } /// Device _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; // Input _input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil]; // Output _output = [[AVCaptureMetadataOutput alloc] init]; [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; _output.rectOfInterest = self.scanImageView.bounds; // Session _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]; } // 条码类型 AVMetadataObjectTypeQRCode _output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeEAN8Code,AVMetadataObjectTypeUPCECode]; dispatch_async(dispatch_get_main_queue(), ^{ // Preview [HUDUtils dismiss]; _preview =[AVCaptureVideoPreviewLayer layerWithSession:self.session]; _preview.videoGravity = AVLayerVideoGravityResizeAspectFill; _preview.frame = self.view.frame; [self.view.layer insertSublayer:self.preview atIndex:0]; /* 配置取景框之外颜色开始 */ // scanCrop UIView *viewTopScan = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, self.scanImageView.top)]; UIView *viewBottomScan = [[UIView alloc] initWithFrame:CGRectMake(0, self.scanImageView.bottom, ScreenWidth, ScreenHeight - self.scanImageView.bottom)]; UIView *viewLeftScan = [[UIView alloc] initWithFrame:CGRectMake(0, self.scanImageView.top, (ScreenWidth-self.scanImageView.width)/2, self.scanImageView.height)]; UIView *viewRightScan = [[UIView alloc] initWithFrame:CGRectMake(self.scanImageView.right, self.scanImageView.top, (ScreenWidth-self.scanImageView.width)/2, self.scanImageView.height)]; viewTopScan.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.2]; viewBottomScan.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.2]; viewLeftScan.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.2]; viewRightScan.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.2]; [self.view addSubview:viewTopScan]; [self.view addSubview:viewBottomScan]; [self.view addSubview:viewLeftScan]; [self.view addSubview:viewRightScan]; /* 配置取景框之外颜色结束 */ self.contentLable.layer.cornerRadius = self.contentLable.height / 2 ; self.contentLable.layer.backgroundColor = [[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8] CGColor]; // Start [_session startRunning]; }); }); }
有一点需要注意,在初始化相机的时候,会耗时,在iPhone 4 机器上体现的尤为明显,也很简单,加上一个异步操作解决问题.
标签:
原文地址:http://www.cnblogs.com/dachengzi/p/4670693.html