1 / 14
文档名称:

iOS二维码扫描(原生,可限制扫描区域).doc

格式:doc   大小:46KB   页数:14页
下载后只包含 1 个 DOC 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

iOS二维码扫描(原生,可限制扫描区域).doc

上传人:学习的一点 2021/7/25 文件大小:46 KB

下载得到文件列表

iOS二维码扫描(原生,可限制扫描区域).doc

文档介绍

文档介绍:iOS二维码扫描(原生,可限制扫描区域)
须知:
在 iOS7 以前,在iOS中实现二维码和条形码扫描,我们所知的有,两大开源组件ZBar与ZXing. 这两大组件我们都有用过,这里总结下各自的缺点:
1 .ZBar在扫描的灵敏度上,和内存的使用上相对于ZXing上都是较优的,但是对于 “圆角二维码” 的扫描确很困难
2 .ZXing 是 Google Code上的一个开源的条形码扫描库,是用java设计的,连Google Glass 都在使用的。但有人为了追求更高效率以及可移植性,出现了c++ port. Github上的Objectivc-C port,其实就是用OC代码封装了一下而已,而且已经停止维护。这样效率非常低,在instrument下面可以看到CPU和内存疯涨,在内存小的机器上很容易崩溃
3 .AVFoundation无论在扫描灵敏度和性能上来说都是最优的,所以毫无疑问我们应该切换到AVFoundation,需要兼容iOS ,

#import
,连线,设置需要的代理
***@interface ViewController ()
***@property ( strong , nonatomic ) AVCaptureDevice * device;
***@property ( strong , nonatomic ) AVCaptureDeviceInput * input;
***@property ( strong , nonatomic ) AVCaptureMetadataOutput * output;
***@property ( strong , nonatomic ) AVCaptureSession * session;
***@property ( strong , nonatomic ) AVCaptureVideoPreviewLayer * previewLayer;
/*** 专门用于保存描边的图层 ***/
***@property (nonatomic,strong) CALayer *containerLayer;
***@end

- (void)viewDidLoad {
[super viewDidLoad];
// 开始扫描二维码
[self startScan];
}

#pragma mark -------- 懒加载---------
- (AVCaptureDevice *)device
{
if (_device == nil) {
_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
}
return _device;
}
- (AVCaptureDeviceInput *)input
{
if (_input == nil) {
_input = [AVCaptureDeviceInput deviceInputWithDevice: error:nil];
}
return _input;
}
- (AVCaptureSession *)session
{
if (_session == nil) {
_session = [[AVCaptureSession alloc] init];
}
return _session;
}
- (AVCaptureVideoPreviewLayer *)previewLayer
{
if (_previewLayer == nil) {
_previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:];
}
return _previewLayer;
}
// 设置输出对象解析数据时感兴趣的范围
// 默认值是 CGRect(x: 0, y: 0, width: 1, height: 1)
// 通过对这个值的观察, 我们发现传入的是比例
// 注意: 参照是以横屏的左上角作为, 而不是以竖屏
//