gpt4 book ai didi

ios - 如何在iOS中使用Objective C扫描AVCaptureVideoPreviewLayer特定区域的条形码?

转载 作者:行者123 更新时间:2023-12-02 07:51:03 27 4
gpt4 key购买 nike

我正在尝试使用 Objective C 中的 AVFoundation 在我的 iPhone 应用程序中实现条形码扫描仪。条形码扫描部分工作正常,但问题是当前预览显示在设备的整个屏幕上,我希望扫描仪预览要显示在设备的特定区域,需要更改背景的亮度区域(请引用下面的截图)。

enter image description here

如何放置 AVCaptureVideoPreviewLayer 的特定区域以获得带角的条形扫描结果?

最佳答案

我的 pj 中有一个例子,它是用 Swift 编写的,但我想你可以很好地转换它,根据需要调整代码,55 是每一行的长度

要创建角点,也许这样就可以了,scanRectView是感兴趣的矩形:

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
}

objective-c 代码:

    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];

创建后,像viewWillAppearviewDidLayoutSubView中的self.scanRectView.layer.addSublayer(self.createFrame())一样添加它

结果:

enter image description here

关于ios - 如何在iOS中使用Objective C扫描AVCaptureVideoPreviewLayer特定区域的条形码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45072019/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com