.h里
#import<AVFoundation/AVFoundation.h>
@interfaceErWeiViewController :UIViewController<AVCaptureMetadataOutputObjectsDelegate>
-(BOOL)startReading;
-(void)stoPReading;
//捕捉会话
@property(nonatomic,strong)AVCapturesession*captureSession;
//展示layer
@property(nonatomic,strong)AVCaptureVideoPreviewLayer*videoPreviewLayer;
@property(nonatomic,assign)BOOLisReading;
@property(strong,nonatomic)UIView*boxView;
@property(strong,nonatomic)CALayer*scanLayer;
@property(strong,nonatomic) UIView*viewPreview;
@property(strong,nonatomic) UILabel*lblStatus;
@property(strong,nonatomic) UIButton*startBtn;
- (void)startStopReading:(id)sender;
.m里
- (void)viewDidLoad {
[superviewDidLoad];
_captureSession=nil;
_isReading=NO;
[selfstartReading];
}
-(BOOL)startReading {
_viewPreview= [[UIViewalloc]initWithFrame:self.view.bounds];
[self.viewaddSubview:_viewPreview];
NSError*error;
//1.初始化捕捉设备(AVCaptureDevice),类型为AVMediaTypeVideo
AVCaptureDevice*captureDevice = [AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];
//2.用captureDevice创建输入流
AVCaptureDeviceInput*input = [AVCaptureDeviceInputdeviceInputWithDevice:captureDeviceerror:&error];
if(!input) {
NSLog(@"%@", [errorlocalizedDescription]);
returnNO;
}
//3.创建媒体数据输出流
AVCaptureMetadataOutput*captureMetadataOutput = [[AVCaptureMetadataOutputalloc]init];
//4.实例化捕捉会话
_captureSession= [[AVCaptureSessionalloc]init];
//4.1.将输入流添加到会话
[_captureSessionaddInput:input];
//4.2.将媒体输出流添加到会话中
[_captureSessionaddOutput:captureMetadataOutput];
//5.创建串行队列,并加媒体输出流添加到队列当中
dispatch_queue_tdispatchQueue;
dispatchQueue =dispatch_queue_create("myQueue",NULL);
//5.1.设置代理
[captureMetadataOutputsetMetadataObjectsDelegate:selfqueue:dispatchQueue];
//5.2.设置输出媒体数据类型为所有类型
captureMetadataOutput.metadataObjectTypes=@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeEAN8Code,AVMetadataObjectTypeCode128Code];
//6.实例化预览图层
_videoPreviewLayer= [[AVCaptureVideoPreviewLayeralloc]initWithSession:_captureSession];
//7.设置预览图层填充方式
[_videoPreviewLayersetVideoGravity:AVLayerVideoGravityResizeaspectFill];
//8.设置图层的frame
[_videoPreviewLayersetFrame:_viewPreview.layer.bounds];
//9.将图层添加到预览view的图层上
[_viewPreview.layeraddSublayer:_videoPreviewLayer];
//10.设置扫描范围
captureMetadataOutput.rectOfInterest=CGRectMake(0.2f,0.2f,0.8f,0.8f);
//10.1.扫描框
_boxView= [[UIViewalloc]initWithFrame:CGRectMake(_viewPreview.bounds.size.width*0.2f,_viewPreview.bounds.size.height*0.2f,_viewPreview.bounds.size.width-_viewPreview.bounds.size.width*0.4f,_viewPreview.bounds.size.height-_viewPreview.bounds.size.height*0.4f)];
_boxView.layer.borderColor= [UIColorgreenColor].CGColor;
_boxView.layer.borderWidth=1.0f;
[_viewPreviewaddSubview:_boxView];
//10.2.扫描线
_scanLayer= [[CALayeralloc]init];
_scanLayer.frame=CGRectMake(0,0,_boxView.bounds.size.width,1);
_scanLayer.backgroundColor= [UIColor
brownColor].CGColor;
[_boxView.layeraddSublayer:_scanLayer];
NSTimer*timer = [NSTimerscheduledTimerWithTimeInterval:0.2ftarget:selfselector:@selector(moveScanLayer:)userInfo:nilrepeats:YES];
[timerfire];
//10.开始扫描
[_captureSessionstartRunning];
returnYES;
}
#pragma mark - AVCaptureMetadataOutputObjectsDelegate协议方法
- (void)captureOutput:(AVCaptureOutput*)captureOutput didOutputMetadataObjects:(NSArray*)metadataObjects fromConnection:(AVCaptureConnection*)connection
{
//判断是否有数据
if(metadataObjects !=nil&& [metadataObjectscount] >0) {
AVMetadataMachineReadableCodeObject*metadataObj = [metadataObjectsobjectAtIndex:0];
//判断回传的数据类型 captureMetadataOutput.metadataObjectTypes 数组里的类型进行遍历对比 添加if判断类型,
// 下面只以AVMetadataObjectTypeQRCode 为例子
// if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {
// [_lblStatus performSelectorOnMainThread:@selector(setText:) withObject:[metadataObj stringValue] waitUntilDone:NO];
// NSLog(@"11111 = %@", _lblStatus.text);
// [self performSelectorOnMainThread:@selector(stopReading) withObject:nil waitUntilDone:NO];
// _isReading = NO;
// }
}
[selfdismissViewControllerAnimated:YEScompletion:^{
}];
}
//实现计时器方法moveScanLayer:(NSTimer *)timer
- (void)moveScanLayer:(NSTimer*)timer
{
CGRectframe =_scanLayer.frame;
if(_boxView.frame.size.height<_scanLayer.frame.origin.y) {
frame.origin.y=0;
_scanLayer.frame= frame;
}else{
frame.origin.y+=5;
[UIViewanimateWithDuration:0.1animations:^{
_scanLayer.frame= frame;
}];
}
}
- (void)startStopReading:(id)sender {
if(!_isReading) {
if([selfstartReading]) {
[_startBtnsetTitle:@"Stop"forState:UIControlStateNormal];
[_lblStatussetText:@"Scanning for QR Code"];
}
}
else{
[selfstopReading];
[_startBtnsetTitle:@"Start!"forState:UIControlStateNormal];
}
_isReading= !_isReading;
}
-(void)stopReading{
[_captureSessionstopRunning];
_captureSession=nil;
[_scanLayerremoveFromSuperlayer];
[_videoPreviewLayerremoveFromSuperlayer];
}