我正在尝试使用目标C中的AVFoundation将条形码扫描仪实现到我的iPhone应用程序。条形码扫描部分工作正常,但问题是当前预览显示在设备的整个屏幕上,我希望扫描仪预览到显示在设备的特定区域,需要更改背景的亮度区域(请参阅下面的屏幕截图)。
如何放置AVCaptureVideoPreviewLayer
的特定区域以获取带有角落的条形扫描结果?
答案 0 :(得分:1)
我的pj中有一个例子,它在Swift中,但我猜你可以转换它,根据需要调整代码,55是每行的长度
要创建角落,也许会这样做,color
是兴趣矩阵:
scanRectView
目标C代码:
func createFrame() -> CAShapeLayer {
let height: CGFloat = self.scanRectView.frame.size.height
let width: CGFloat = self.scanRectView.frame.size.width
let path = UIBezierPath()
path.move(to: CGPoint(x: 5, y: 50))
path.addLine(to: CGPoint(x: 5, y: 5))
path.addLine(to: CGPoint(x: 50, y: 5))
path.move(to: CGPoint(x: height - 55, y: 5))
path.addLine(to: CGPoint(x: height - 5, y: 5))
path.addLine(to: CGPoint(x: height - 5, y: 55))
path.move(to: CGPoint(x: 5, y: width - 55))
path.addLine(to: CGPoint(x: 5, y: width - 5))
path.addLine(to: CGPoint(x: 55, y: width - 5))
path.move(to: CGPoint(x: width - 55, y: height - 5))
path.addLine(to: CGPoint(x: width - 5, y: height - 5))
path.addLine(to: CGPoint(x: width - 5, y: height - 55))
let shape = CAShapeLayer()
shape.path = path.cgPath
shape.strokeColor = UIColor.white.cgColor
shape.lineWidth = 5
shape.fillColor = UIColor.clear.cgColor
return shape
}
创建后,在 CGFloat height = baseScannerView.frame.size.height+4;
CGFloat width = baseScannerView.frame.size.width+4;
UIBezierPath* path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(1, 25)];
[path addLineToPoint:CGPointMake(1, 1)];
[path addLineToPoint:CGPointMake(25, 1)];
[path moveToPoint:CGPointMake(width - 30, 1)];
[path addLineToPoint:CGPointMake(width - 5, 1)];
[path addLineToPoint:CGPointMake(width - 5, 25)];
[path moveToPoint:CGPointMake(1, height - 30)];
[path addLineToPoint:CGPointMake(1, height - 5)];
[path addLineToPoint:CGPointMake(25, height - 5)];
[path moveToPoint:CGPointMake(width - 30, height - 5)];
[path addLineToPoint:CGPointMake(width - 5, height - 5)];
[path addLineToPoint:CGPointMake(width - 5, height - 30)];
CAShapeLayer *pathLayer = [CAShapeLayer layer];
pathLayer.path = path.CGPath;
pathLayer.strokeColor = [[UIColor redColor] CGColor];
pathLayer.lineWidth = 5.0f;
pathLayer.fillColor = nil;
[self.scanRectView.layer addSublayer:pathLayer];
或self.scanRectView.layer.addSublayer(self.createFrame())
viewWillAppear
结果:
答案 1 :(得分:0)
你可以根据你的需要改变AVCaptureVideoPreviewLayer框架,就像这样,这是一个例子,
AVCaptureVideoPreviewLayer *_CaptureLayer;
AVCaptureSession *_Capturesession;
_Capturesession = [[AVCaptureSession alloc] init];
_CaptureLayer = [AVCaptureVideoPreviewLayer layerWithSession:
_Capturesession];
_CaptureLayer = CGRectMake(self.view.frame.origin.x,
self.view.frame.origin.y+100,
self.view.frame.size.width,
self.view.frame.size.height/2.5);
_CaptureLayer = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer: _CaptureLayer];