使用原生api扫描二维码设置扫描区域

使用原生API扫描二维码

在上面的文章中说的关于设置扫描区域,当时写的是手动计算的区域设置的

其实AVCaptureVideoPreviewLayer- (CGRect)metadataOutputRectOfInterestForRect:(CGRect)rectInLayerCoordinates是可以正常使用的,只不过不是直接调用这个方法,需要在通知回调中进行设置

1
2
3
4
5
6
7
  CGRect outRect = CGRectMake(10, 60, 200, 200);
[[NSNotificationCenter defaultCenter] addObserverForName:AVCaptureInputPortFormatDescriptionDidChangeNotification
object:nil
queue:[NSOperationQueue currentQueue]
usingBlock: ^(NSNotification *_Nonnull note) {
captureOutput.rectOfInterest = [captureLayer metadataOutputRectOfInterestForRect:outRect];
}];