【问题标题】:How to scan Bar code in a specific area of AVCaptureVideoPreviewLayer using Objective C in iOS?如何在 iOS 中使用 Objective C 扫描 AVCaptureVideoPreviewLayer 特定区域的条码?
【发布时间】:2017-12-17 17:28:25
【问题描述】:

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

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

【问题讨论】:

标签: ios objective-c avfoundation barcode-scanner avcapturesession


【解决方案1】:

我的 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
}

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

创建后,将其添加为self.scanRectView.layer.addSublayer(self.createFrame()) 中的viewWillAppearviewDidLayoutSubView

结果:

【讨论】:

  • 让我把它转换成Objective C,我会尽快通知你的。谢谢亲爱的:)
  • 嗨@Tj3n,我也用尊重的坐标转换了这个。现在我正在尝试更改 AVCaptureMetadataOutput 的 rectOfInterest。但无法更改扫描仪背景的亮度区域。有什么想法吗?
  • 创建亮度较低的 view2,然后使用 view2.layer.mask = rectOfInterestView.layerview2.layer.masksToBounds = YES 用扫描视图遮盖背景
  • 好的,但是view2不会像扫描仪视图那样出现,请看这个链接imgur.com/a/qb8jT
  • hmm..哪部分出了问题?我认为它看起来像预期的那样?
【解决方案2】:

您可以根据自己的需要更改 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];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-24
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多