iOS原生二维码扫描扫描区域

2019-05-10  本文已影响0人  往事不许提

扫描区域默认是全屏扫描的,可以通过设置AVCaptureMetadataOutput的rectOfInterest属性来设置扫描区域:rectOfInterest是CGRect类型,但是默认值为CGRectMake(0,0,1,1),其中的值是比例;
用一个方法来计算这个比例关系:

- (CGRect)rectOfInterestsByScanViewRect:(CGRect)rect {
      CGFloat width = CGRectGetWidth(self.view.frame);
      CGFloat height = CGRectGetHeight(self.view.frame);
      CGFloat x = rect.origin.y/height;
      CGFloat y = rect.origin.x/width;
      CGFloat w = CGRectGetHeight(rect)/height;
      CGFloat h = CGRectGetWidth(rect)/width;
      return CGRectMake(x,y,w,h);
}

即:
屏幕为(x,y,w,h),扫描区域为(x1,y1,w1,h1)则rectOfInterest应该设置为CGRectMake(y1/y, x1/x , h1/h, w1/w),原因是坐标原点在右上角,和常规在左上角不同;

上一篇下一篇

猜你喜欢

热点阅读