限定二维码扫描范围
2017-06-19 17:14
120 查看
使用iOS原生的二维码扫描,如何限定扫描范围,你可能已经知道,通过设定AVCaptureMetadataOutput的rectOfInterest来限定范围,对,就是这个属性。
The value of this property is a CGRect that determines the receiver’s rectangle of interest for each frame of video. 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). Metadata objects whose bounds do not intersect with the rectOfInterest will not be returned.
这是官方对这个属性的说明,是不是有点晕,这个rect和平常理解的那个rect有点区别,不过不用担心自己不会计算,AVCaptureVideoPreviewLayer的metadataOutputRectOfInterestForRect方法可以帮你计算出来。
AVCaptureMetadataOutput rectOfInterest is expressed as a CGRect where {0,0} represents the top left of the picture area, and {1,1} represents the bottom right on an unrotated picture. This convenience method converts a rectangle in the coordinate space of the receiver to a rectangle of interest in the coordinate space of an AVCaptureMetadataOutput whose AVCaptureDevice is providing input to the receiver. The conversion takes frame size and videoGravity into consideration.
但是这个方法用的时机要对。
在
在收到
最后尝试在
所以正确的使用方式是
The value of this property is a CGRect that determines the receiver’s rectangle of interest for each frame of video. 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). Metadata objects whose bounds do not intersect with the rectOfInterest will not be returned.
这是官方对这个属性的说明,是不是有点晕,这个rect和平常理解的那个rect有点区别,不过不用担心自己不会计算,AVCaptureVideoPreviewLayer的metadataOutputRectOfInterestForRect方法可以帮你计算出来。
AVCaptureMetadataOutput rectOfInterest is expressed as a CGRect where {0,0} represents the top left of the picture area, and {1,1} represents the bottom right on an unrotated picture. This convenience method converts a rectangle in the coordinate space of the receiver to a rectangle of interest in the coordinate space of an AVCaptureMetadataOutput whose AVCaptureDevice is providing input to the receiver. The conversion takes frame size and videoGravity into consideration.
但是这个方法用的时机要对。
在
AVCaptureSession startRunning之前用,结果始终是
{0,0,0,0}。
在收到
AVCaptureInputPortFormatDescriptionDidChangeNotification之后调用,我试了一下,在iOS8-iOS10上都没有问题,但是在iOS7结果始终是
{0,0,0,0}。
最后尝试在
AVCaptureSession startRunning之后调用,成功,iOS7-iOS10都没有问题。
所以正确的使用方式是
[session startRunning]; CGRect scopeRect = CGRectMake(x, y, width, height);//要限定的范围 output.rectOfInterest = [previewLayer metadataOutputRectOfInterestForRect:scopeRect];
相关文章推荐
- HTTP 500 错误-没有在限定的时间内用 DCOM 注册
- const限定修饰符
- HTTP 500 错误-没有在限定的时间内用 DCOM 注册
- delphi限定数字输入
- 用StructLayout特性限定声明结构或类
- warning C4482: 使用了非标准扩展: 限定名中使用了枚举
- 限定字符长度,截断中文字符
- 简单继承(inheritance)的实现及访问限定
- C++ const 限定符 指针的使用方法
- property 限定符
- 2960 S-Nim 取石子的个数有限定集合尼姆博弈
- Const限定修饰符
- 《GOF设计模式》—迭代器 (ITERATOR)—Delphi源码示例:避免限定于一种特定的列表实现(一般迭代)
- [C#2] 3-局部类型、属性访问器保护级别、命名空间别名限定符
- [JavaScript]限定输入的格式为多个数字用逗号隔开
- 限定登录失败次数,超过指定次数就限制登录一段时间
- XSD限定例子详解
- 限定年龄的用人观应该改一改了
- c++ 3/1---const 限定类的成员函数
- 《第十二周任务一》理解基类中成员函数的访问限定符和派生类的继承方式