当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS 原生实现扫描二维码和条形码功能限制扫描区域

iOS 原生实现扫描二维码和条形码功能限制扫描区域

2019年07月24日  | 移动技术网移动技术  | 我要评论

现在苹果ios系统已经原生支持了二维码扫描的功能,使用原生来扫描需要导入avfoundation。

扫描准备

一、获取摄像设备:

device = avcapturedevice.defaultdevice(withmediatype: avmediatypevideo)

二、创建输入流

do {
  try input = avcapturedeviceinput(device: device)
} catch let e as nserror {
  print(e.localizeddescription)
}

三、创建输出流

output = avcapturemetadataoutput()
// 设置代理在主线程中刷新
output?.setmetadataobjectsdelegate(self, queue: dispatchqueue.main)

四、初始化连接对象

session = avcapturesession()
// 高质量采集率
session?.cansetsessionpreset(avcapturesessionpresethigh)
session?.addoutput(output)
session?.addinput(input)

五、设置扫描区域

// 特别注意的地方:有效的扫描区域,定位是以设置的右顶点为原点。屏幕宽所在的那条线为y轴,屏幕高所在的线为x轴
let x = ((screenheight - qrcodewidth - topviewheight) / 2.0) / screenheight
let y = ((screenwidth - qrcodewidth) / 2.0) / screenwidth
let width = qrcodewidth / screenheight
let height = qrcodewidth / screenwidth
output?.rectofinterest = cgrect(x: x, y: y, width: width, height: height)

六、设置扫码支持的编码格式(如下设置条形码和二维码兼容)

output?.metadataobjecttypes = [avmetadataobjecttypeqrcode, avmetadataobjecttypeean13code, avmetadataobjecttypeean8code, avmetadataobjecttypecode128code]

七、开始捕获

preview = avcapturevideopreviewlayer(session: session)
preview?.videogravity = avlayervideogravityresizeaspectfill
preview?.frame = self.view.layer.bounds
self.view.layer.insertsublayer(preview!, at: 0)
session?.startrunning()

扫描动画

这里的动画是仿支付宝的扫描框动画

我们新建一个方法,专门处理我们的动画。

fileprivate func scananimation() -> cabasicanimation {
   let scannetanimation = cabasicanimation()
    // 沿y轴运动
   scannetanimation.keypath = "transform.translation.y"
   // 扫描框的高度,注意:这里是实际高度的相反数
   scannetanimation.byvalue = qrcodewidth
    // 动画的持续时间
   scannetanimation.duration = 1.5
   // 动画的重复次数
   scannetanimation.repeatcount = maxfloat
   return scannetanimation
}

使用动画:

我们在创建界面的时候,扫描框有一个uiimageview,我们需要将我们的动画添加到这个imageview上面。

scanimageview?.layer.add(scananimation(), forkey: nil)

扫描之后的处理

func captureoutput(_ captureoutput: avcaptureoutput!, didoutputmetadataobjects metadataobjects: [any]!, from connection: avcaptureconnection!) {
  if metadataobjects.count > 0 {
    session?.stoprunning()
    let metadataobject = metadataobjects[0] as anyobject
    let stringvalue: string = metadataobject.stringvalue
    let vc = qrcoderesultviewcontroller.instantiate()
    vc.resultstr = stringvalue
    self.navigationcontroller?.pushviewcontroller(vc, animated: true)
  }
}

点击扫描结果的处理

func webview(_ webview: uiwebview, shouldstartloadwith request: urlrequest, navigationtype: uiwebviewnavigationtype) -> bool {
  let requesturl = request.url
  if requesturl?.scheme == "http" || requesturl?.scheme == "https" || requesturl?.scheme == "mailto" && navigationtype == .linkclicked {
//    uiapplication.shared.open(requesturl!, options: [:], completionhandler: nil)
    let svc = sfsafariviewcontroller(url: requesturl!)
    self.present(svc, animated: true, completion: nil)
  }
  return true
}

我们可以用

open func open(_ url: url, options: [string : any] = [:], completionhandler completion: ((bool) -> swift.void)? = nil)

在safari中打开连接。不过最好是把事件控制在自己的程序中,在ios 9 之后,苹果引入了 sfsafariviewcontroller 这个类,可以用这个类来显示需要浏览的网页。

let svc = sfsafariviewcontroller(url: requesturl!)
self.present(svc, animated: true, completion: nil)

以上所述是小编给大家介绍的ios 原生实现扫描二维码和条形码功能限制扫描区域,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网