iOS如何實(shí)現(xiàn)掃描二維碼和條形碼功能限制掃描區(qū)域

小編給大家分享一下iOS如何實(shí)現(xiàn)掃描二維碼和條形碼功能限制掃描區(qū)域,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

創(chuàng)新互聯(lián)建站主要從事成都網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)輪臺(tái),十余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):028-86922220

現(xiàn)在蘋(píng)果iOS系統(tǒng)已經(jīng)原生支持了二維碼掃描的功能,使用原生來(lái)掃描需要導(dǎo)入AVFoundation。

掃描準(zhǔn)備

一、獲取攝像設(shè)備:

device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

二、創(chuàng)建輸入流

do {
  try input = AVCaptureDeviceInput(device: device)
} catch let e as NSError {
  print(e.localizedDescription)
}

三、創(chuàng)建輸出流

output = AVCaptureMetadataOutput()
// 設(shè)置代理在主線程中刷新
output?.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)

四、初始化連接對(duì)象

session = AVCaptureSession()
// 高質(zhì)量采集率
session?.canSetSessionPreset(AVCaptureSessionPresetHigh)
session?.addOutput(output)
session?.addInput(input)

五、設(shè)置掃描區(qū)域

// 特別注意的地方:有效的掃描區(qū)域,定位是以設(shè)置的右頂點(diǎn)為原點(diǎn)。屏幕寬所在的那條線為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)

六、設(shè)置掃碼支持的編碼格式(如下設(shè)置條形碼和二維碼兼容)

output?.metadataObjectTypes = [AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code]

七、開(kāi)始捕獲

preview = AVCaptureVideoPreviewLayer(session: session)
preview?.videoGravity = AVLayerVideoGravityResizeAspectFill
preview?.frame = self.view.layer.bounds
self.view.layer.insertSublayer(preview!, at: 0)
session?.startRunning()

掃描動(dòng)畫(huà)

這里的動(dòng)畫(huà)是仿支付寶的掃描框動(dòng)畫(huà)

我們新建一個(gè)方法,專門(mén)處理我們的動(dòng)畫(huà)。

fileprivate func scanAnimation() -> CABasicAnimation {
   let scanNetAnimation = CABasicAnimation()
    // 沿Y軸運(yùn)動(dòng)
   scanNetAnimation.keyPath = "transform.translation.y"
   // 掃描框的高度,注意:這里是實(shí)際高度的相反數(shù)
   scanNetAnimation.byValue = QRCodeWidth
    // 動(dòng)畫(huà)的持續(xù)時(shí)間
   scanNetAnimation.duration = 1.5
   // 動(dòng)畫(huà)的重復(fù)次數(shù)
   scanNetAnimation.repeatCount = MAXFLOAT
   return scanNetAnimation
}

使用動(dòng)畫(huà):

我們?cè)趧?chuàng)建界面的時(shí)候,掃描框有一個(gè)UIImageView,我們需要將我們的動(dòng)畫(huà)添加到這個(gè)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)
  }
}

點(diǎn)擊掃描結(jié)果的處理

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中打開(kāi)連接。不過(guò)最好是把事件控制在自己的程序中,在iOS 9 之后,蘋(píng)果引入了 SFSafariViewController 這個(gè)類(lèi),可以用這個(gè)類(lèi)來(lái)顯示需要瀏覽的網(wǎng)頁(yè)。

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

看完了這篇文章,相信你對(duì)“iOS如何實(shí)現(xiàn)掃描二維碼和條形碼功能限制掃描區(qū)域”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

本文標(biāo)題:iOS如何實(shí)現(xiàn)掃描二維碼和條形碼功能限制掃描區(qū)域
鏈接地址:http://bm7419.com/article42/igioec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、域名注冊(cè)、服務(wù)器托管、搜索引擎優(yōu)化、做網(wǎng)站、網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)