1. 首先导入二维码依赖的框架
并导入头文件
遵守协议 AVCaptureMetadataOutputObjectsDelegate
2.二维码的基本设置
(1)定义两个将要用到的属性
AVCaptureSession * session;//输入输出的中间桥梁
AVCaptureDevice *device;
(2)实现基本属性设置
//获取摄像设备
device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//创建输入流
AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
//创建输出流
AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc]init];
//设置代理 在主线程里刷新
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
//初始化链接对象
session = [[AVCaptureSession alloc]init];
//高质量采集率
[session setSessionPreset:AVCaptureSessionPresetHigh];
[session addInput:input];
[session addOutput:output];
//设置扫码支持的编码格式(如下设置条形码和二维码兼容)
output.metadataObjectTypes=@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];
preview=[[UIView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT)];
AVCaptureVideoPreviewLayer * layer = [AVCaptureVideoPreviewLayer layerWithSession:session];
layer.videoGravity=AVLayerVideoGravityResizeAspectFill;
layer.frame=preview.layer.bounds;
[self.view addSubview:preview];
[preview.layer addSublayer:layer];
3. 扫描有效界面的设置
(1)扫描页面的限制
这里的扫描区域和普通的fram不一样,x=y y=x (亲手试一下就知道了)
//扫描区域
output.rectOfInterest=CGRectMake(0.05f, 0.12f, 0.5f, 0.76f);
//设置UI界面
[self setScanView];
//开始捕获
[session startRunning];
[ self createTimer ];
(2)扫描有效界面UI的设置,这里我定义了几个宏(参考这看一下)
//二维码的扫描区域
- ( void )setScanView
{
UIView * upView = [[ UIView alloc ] initWithFrame : CGRectMake ( 0 , 0 , WIDTH , SCANVIEW_EdgeTop )];
upView. alpha = TINTCOLOR_ALPHA ;
upView. backgroundColor = [ UIColor blackColor ];
[ preview addSubview :upView];
//左侧的view
UIView *leftView = [[ UIView alloc ] initWithFrame : CGRectMake ( 0 , SCANVIEW_EdgeTop , SCANVIEW_EdgeLeft , WIDTH - 2 * SCANVIEW_EdgeLeft )];
leftView. alpha = TINTCOLOR_ALPHA ;
leftView. backgroundColor = [ UIColor blackColor ];
[ preview addSubview :leftView];
/******************中间扫描区域****************************/
UIImageView *scanCropView=[[ UIImageView alloc ] initWithFrame : CGRectMake ( SCANVIEW_EdgeLeft , SCANVIEW_EdgeTop , WIDTH - 2 * SCANVIEW_EdgeLeft , WIDTH - 2 * SCANVIEW_EdgeLeft )];
//scanCropView.image=[UIImage imageNamed:@""];
//scanCropView. layer . borderColor =[ UIColor getThemeColor ]. CGColor ;
scanCropView. layer . borderWidth = 2.0 ;
scanCropView. backgroundColor =[ UIColor clearColor ];
[ preview addSubview :scanCropView];
//右侧的view
UIView *rightView = [[ UIView alloc ] initWithFrame : CGRectMake ( WIDTH - SCANVIEW_EdgeLeft , SCANVIEW_EdgeTop , SCANVIEW_EdgeLeft , WIDTH - 2 * SCANVIEW_EdgeLeft )];
rightView. alpha = TINTCOLOR_ALPHA ;
rightView. backgroundColor = [ UIColor blackColor ];
[ preview addSubview :rightView];
//底部view
UIView *downView = [[ UIView alloc ] initWithFrame : CGRectMake ( 0 , WIDTH - 2 * SCANVIEW_EdgeLeft + SCANVIEW_EdgeTop , WIDTH , HEIGHT -( WIDTH - 2 * SCANVIEW_EdgeLeft + SCANVIEW_EdgeTop )- 64 )];
//downView.alpha = TINTCOLOR_ALPHA;
downView. backgroundColor = [[ UIColor blackColor ] colorWithAlphaComponent : TINTCOLOR_ALPHA ];
[ preview addSubview :downView];
//用于说明的label
UILabel *labIntroudction= [[ UILabel alloc ] init ];
labIntroudction. backgroundColor = [ UIColor clearColor ];
labIntroudction. frame = CGRectMake ( 5 , 5 , WIDTH , 50 );
labIntroudction. numberOfLines = 0;
labIntroudction. font =[ UIFont systemFontOfSize : 15.0 ];
labIntroudction. textAlignment = NSTextAlignmentCenter ;
labIntroudction. textColor =[ UIColor whiteColor ];
labIntroudction. text = NSLocalizedString(@"duizhunzidongsaomiao",@"") ;
[downView addSubview :labIntroudction];
UIView *darkView = [[ UIView alloc ] initWithFrame : CGRectMake ( 0 , downView. frame . size . height - 100.0 , WIDTH , 100.0 )];
darkView. backgroundColor = [[ UIColor blackColor ] colorWithAlphaComponent : DARKCOLOR_ALPHA ];
[downView addSubview :darkView];
//用于开关灯操作的button
UIButton *openButton=[[ UIButton alloc ] initWithFrame : CGRectMake (WIDTH/2-90 , 20 , 180.0 , 40.0 )];
[openButton setTitle : NSLocalizedString(@"kaiqishanguangdeng",@"") forState: UIControlStateNormal ];
[openButton setTitleColor :[ UIColor whiteColor ] forState : UIControlStateNormal ];
openButton. titleLabel . textAlignment = NSTextAlignmentCenter ;
//openButton. backgroundColor =[ UIColor getThemeColor ];
openButton. titleLabel . font =[ UIFont systemFontOfSize : 22.0 ];
[openButton addTarget : self action : @selector (turnOnLede) forControlEvents : UIControlEventTouchUpInside ];
[darkView addSubview :openButton];
//画中间的基准线
_QrCodeline = [[ UIView alloc ] initWithFrame : CGRectMake ( SCANVIEW_EdgeLeft , SCANVIEW_EdgeTop , WIDTH - 2 * SCANVIEW_EdgeLeft , 2 )];
_QrCodeline . backgroundColor = [ UIColor whiteColor ];
[ preview addSubview : _QrCodeline ];
}
4. 二维码扫描线的实现
上边写到了调用方法
[ self createTimer ];
(1)定义一个计时器
- ( void )createTimer
{
//创建一个时间计数
_timer=[NSTimer scheduledTimerWithTimeInterval: 2.0 target: self selector: @selector (moveUpAndDownLine) userInfo: nil repeats: YES ];
}
(2)实现扫描线的移动
//二维码的横线移动
- ( void )moveUpAndDownLine
{
CGFloat Y= _QrCodeline . frame . origin . y ;
//CGRectMake(SCANVIEW_EdgeLeft, SCANVIEW_EdgeTop, VIEW_WIDTH-2*SCANVIEW_EdgeLeft, 1)]
if (WIDTH- 2 *SCANVIEW_EdgeLeft+SCANVIEW_EdgeTop==Y){
[UIView beginAnimations: @"asa" context: nil ];
[UIView setAnimationDuration: 2 ];
_QrCodeline.frame=CGRectMake(SCANVIEW_EdgeLeft, SCANVIEW_EdgeTop, WIDTH- 2 *SCANVIEW_EdgeLeft, 1 );
[UIView commitAnimations];
} else if (SCANVIEW_EdgeTop==Y){
[UIView beginAnimations: @"asa" context: nil ];
[UIView setAnimationDuration: 2 ];
_QrCodeline.frame=CGRectMake(SCANVIEW_EdgeLeft, WIDTH- 2 *SCANVIEW_EdgeLeft+SCANVIEW_EdgeTop, WIDTH- 2 *SCANVIEW_EdgeLeft, 1 );
[UIView commitAnimations];
}
}
5. 闪光灯的控制
//二维码界面的闪光灯控制
-(void)turnOnLede {
device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (![device hasTorch]) {//判断是否有闪光灯
UIAlertView *alter = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"tishi",@"") message:NSLocalizedString(@"shoudiantongtishi",@"") delegate:nil cancelButtonTitle:NSLocalizedString(@"queren",@"") otherButtonTitles:nil, nil];
[alter show];
}
islighton = 1-islighton;
if (islighton) {
[self turnOnLede:YES];
}else{
[self turnOffLede:YES];
}
}
//打开闪光灯
-(void) turnOnLede:(bool)update
{
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOn];
[device unlockForConfiguration];
}
//关闭闪光灯
-(void) turnOffLede:(bool)update
{
[device lockForConfiguration:nil];
[device setTorchMode: AVCaptureTorchModeOff];
[device unlockForConfiguration];
}
6. 二维码扫描完成,自动执行delegate方法
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
对所得到的内容进行操作