Apple原装扫描二维码指定区域

以前记录了用Apple原装的AVFoundationo类库扫描二维码:http://www.cnblogs.com/foxting/p/4733226.html,此篇记录下如何设置扫描区域。html

 AVCaptureMetadataOutput有一个属性rectOfInterest,设置你的区域便可。swift

先把Video区域设置成全屏的:self.layer?.frame = self.view.boundside

看下rectOfInterest的注释会知道它的区域大小是先设置top再设置left,有效范围为(0,0,1,1)this

@discussion
        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.spa

键入如下代码rest

let x = (self.view.bounds.size.width - 268) / 2
let beginningX = x / self.view.bounds.width
let beginningY = 106 / self.view.bounds.height
let width = 268 / self.view.bounds.width
let height = 268 / self.view.bounds.height
let interestedRect = CGRectMake(beginningY, beginningX, height, width)

let output = AVCaptureMetadataOutput()
output.rectOfInterest = interestedRect

 实现的效果仍是能够的,此设置仍是蛮方便的。上图咯:orm

相关文章
相关标签/搜索