无第三方的二维码扫描

//1.初始化捕捉设备(AVCaptureDevice),类型为AVMediaTypeVideo

AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

//2.用captureDevice创建输入流

AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];

if (!input) {

NSLog(@"%@", [error localizedDescription]);

return NO;

}

//3.创建媒体数据输出流

AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];

//4.实例化捕捉会话

_captureSession = [[AVCaptureSession alloc] init];

//4.1.将输入流添加到会话

[_captureSession addInput:input];

//4.2.将媒体输出流添加到会话中

[_captureSession addOutput:captureMetadataOutput];

//5.创建串行队列,并加媒体输出流添加到队列当中

dispatch_queue_t dispatchQueue;

dispatchQueue = dispatch_queue_create("myQueue", NULL);

//5.1.设置代理

[captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];

//5.2.设置输出媒体数据类型为QRCode

[captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]];

//6.实例化预览图层

_videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];

//7.设置预览图层填充方式

[_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

//8.设置图层的frame

[_videoPreviewLayer setFrame:_viewPreview.layer.bounds];

//9.将图层添加到预览view的图层上

[_viewPreview.layer addSublayer:_videoPreviewLayer];

//10.设置扫描范围

captureMetadataOutput.rectOfInterest = CGRectMake(0.2f, 0.2f, 0.8f, 0.8f);

//10.1.扫描框

_boxView = [[UIView alloc] 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 = [UIColor greenColor].CGColor;

_boxView.layer.borderWidth = 1.0f;

[_viewPreview addSubview:_boxView];

//10.2.扫描线

_scanLayer = [[CALayer alloc] init];

_scanLayer.frame = CGRectMake(0, 0, _boxView.bounds.size.width, 1);

_scanLayer.backgroundColor = [UIColor brownColor].CGColor;

[_boxView.layer addSublayer:_scanLayer];

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.2f target:self selector:@selector(moveScanLayer:) userInfo:nil repeats:YES];

[timer fire];

//10.开始扫描

[_captureSession startRunning];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • # 扫描二维码 ### 以下是.m文件的完整代码 #import "ScanViewController.h"#d...
    gao_smile阅读 2,944评论 0 0
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 10,541评论 5 13
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 12,722评论 6 30
  • CIDetector 这个api是苹果在ios8之后提供的。所以用苹果自带的AVFundation扫描,如果从相册...
    然亦伞阅读 3,627评论 1 0
  • 如何播放视频 iOS提供了MPMoviePlayerController、MPMoviePlayerViewCon...
    JonesCxy阅读 3,446评论 0 0

友情链接更多精彩内容