如何在iOS中使用Objective C扫描AVCaptureVideoPreviewLayer特定区域中的条形码? [英] How to scan Bar code in a specific area of AVCaptureVideoPreviewLayer using Objective C in iOS?

查看:78
本文介绍了如何在iOS中使用Objective C扫描AVCaptureVideoPreviewLayer特定区域中的条形码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

解决方案

我在pj中有一个示例,它在Swift中,但是我想您可以将其转换为正确的代码,并根据需要对其进行调整,每个长度为55线

要创建拐角,也许可以做到, scanRectView 是感兴趣的区域:

  func createFrame()->CAShapeLayer {让高度:CGFloat = self.scanRectView.frame.size.height设宽度:CGFloat = self.scanRectView.frame.size.width让路径= UIBezierPath()path.move(至:CGPoint(x:5,y:50))path.addLine(至:CGPoint(x:5,y:5))path.addLine(至:CGPoint(x:50,y:5))path.move(至:CGPoint(x:高度-55,y:5))path.addLine(至:CGPoint(x:高度-5,y:5))path.addLine(至:CGPoint(x:高度-5,y:55))path.move(至:CGPoint(x:5,y:宽度-55))path.addLine(至:CGPoint(x:5,y:宽度-5))path.addLine(至:CGPoint(x:55,y:宽度-5))path.move(至:CGPoint(x:宽度-55,y:高度-5))path.addLine(至:CGPoint(x:宽度-5,y:高度-5))path.addLine(至:CGPoint(x:宽度-5,y:高度-55))让形状= CAShapeLayer()shape.path = path.cgPathshape.strokeColor = UIColor.white.cgColorshape.lineWidth = 5shape.fillColor = UIColor.clear.cgColor返回形状} 

目标C代码:

  CGFloat高度= baseScannerView.frame.size.height + 4;CGFloat宽度= 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)];[路径moveToPoint:CGPointMake(width-30,height-5)];[path addLineToPoint:CGPointMake(width-5,height-5)];[path addLineToPoint:CGPointMake(width-5,height-30)];CAShapeLayer * pathLayer = [CAShapeLayer层];pathLayer.path = path.CGPath;pathLayer.strokeColor = [[[UIColor redColor] CGColor];pathLayer.lineWidth = 5.0f;pathLayer.fillColor = nil;[self.scanRectView.layer addSublayer:pathLayer]; 

创建后,在 viewWillAppear viewDidLayoutSubView 中将其添加为 self.scanRectView.layer.addSublayer(self.createFrame())>

结果:

I am trying to implement Bar code Scanner to my iPhone App using AVFoundation in Objective C. Bar code scanning part is working fine, but the issue is currently preview is displaying in the whole screen of the device and I want scanner preview to be display in a specific area of the device and need to change brightness area of the background (please refer to the below screenshot).

How can I place specific region of the AVCaptureVideoPreviewLayer to get the Bar scanner result with the corners?

解决方案

I have one example in my pj, it's in Swift but I guess you can convert it fine, tweak the code as you needed, 55 is the length of each line

To create the corner, maybe this will do, scanRectView is the interest rect:

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 code :

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

After create, add it like self.scanRectView.layer.addSublayer(self.createFrame()) in viewWillAppear or viewDidLayoutSubView

Result:

这篇关于如何在iOS中使用Objective C扫描AVCaptureVideoPreviewLayer特定区域中的条形码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆