关于二维码扫描范围的问题

这几天需要用到二维码,所以自己就找了网上的代码看了一下,貌似自己应该是懂了,但是有时候 用别人的代码,还不如自己去写,

一方面你需要去花时间看别人的代码,另一方面如果你了里面的参数,有时候不会如你的所愿表达你想要的,好了废话不多说了,二维码关键的代码

差不多就是 调用了 系统的照相机,通过调用AVCaptureDevice 类里面这个接口来从而达到你想要的。

    //获取摄像设备

    AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    //创建输入流

    AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];

    if (!input) return;

    //创建输出流

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

    //设置代理 在主线程里刷新

    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];

    //设置有效扫描区域 这里需要重点注意下这个问题     x y 是颠倒的 把我是坑了 后面 我才反应过来

    CGRect scanCrop= CGRectMake(0.2,0.4,0.5,0.6);

    output.rectOfInterest = scanCrop;

    //初始化链接对象

    _session = [[AVCaptureSession alloc]init];

    //高质量采集率

    [_session setSessionPreset:AVCaptureSessionPresetHigh];

    

    [_session addInput:input];

    [_session addOutput:output];

    //设置扫码支持的编码格式(如下设置条形码和二维码兼容)

    output.metadataObjectTypes=@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];

    

    AVCaptureVideoPreviewLayer * layer = [AVCaptureVideoPreviewLayer layerWithSession:_session];

    layer.videoGravity=AVLayerVideoGravityResizeAspectFill;

    layer.frame=self.view.layer.bounds;

    [self.view.layer insertSublayer:layer atIndex:0];

    //开始捕获

    [_session startRunning];


其实这里重点还是 关于设置二维码扫描范围的问题 我们设置布局的时候都是以竖屏的坐标系来进行一个设置,二维码他妹的是横屏做参考 rectOfInterest 是传的参数是 一个 比例值,这扫描的布局在整个屏幕的比例,

看看注释里面的一句话

The rectangle's origin is top left and is relative to the coordinate space of the device providing the metadata.  Specifying 

a rectOfInterest may improve detection performance for certain types of metadata. The default value of this property is the 

value CGRectMake(0, 0, 1, 1).

只能说明是比例,进过测试布局是按照横屏的,所以要坐标在换算的时候需要颠倒一下。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值