Swift 二維碼掃描


代碼如下:

 

 

import UIKit

import AVFoundation

import WebKit

class ViewController: UIViewController,AVCaptureMetadataOutputObjectsDelegate {

 

    var session:AVCaptureSession!

    var screenWidth : CGFloat!

    var screenHeight:CGFloat!

   

  override func viewDidLoad() {

        super.viewDidLoad()

        screenWidth = self.view.bounds.width

        screenHeight = self.view.bounds.height

        setView()

        setCamera()

    }

 

 

//設置除了掃描區以外的視圖

    func setView(){

        let leftView = UIView(frame: CGRect(x: 0, y: 0, width: screenWidth/2-100, height: screenHeight))

        leftView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.6)

        self.view.addSubview(leftView)

        

        let rightView = UIView(frame: CGRect(x: screenWidth/2+100, y: 0, width: screenWidth-(screenWidth/2+100), height: screenHeight))

        rightView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.6)

        self.view.addSubview(rightView)

        

        let topView = UIView(frame: CGRect(x: screenWidth/2-100, y: 0, width: 200, height: 100))

        topView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.6)

        self.view.addSubview(topView)

        

        let bottomView = UIView(frame: CGRect(x: screenWidth/2-100, y: 300, width: 200, height: screenHeight-300))

        bottomView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.6)

        self.view.addSubview(bottomView)

        

    }

 

 

 

//設置相機

    func setCamera(){

        //獲取攝像設備

        guard let device = AVCaptureDevice.default(for: AVMediaType.video) else {

            return

        }

        do {

            //創建輸入流

            let input =  try AVCaptureDeviceInput(device: device)

            //創建輸出流

            let output = AVCaptureMetadataOutput()

            

            //設置會話

            session = AVCaptureSession()

            

            //連接輸入輸出

            if session.canAddInput(input){

                session.addInput(input)

            }

            if session.canAddOutput(output){

                

                session.addOutput(output)

                

                //設置輸出流代理,從接收端收到的所有元數據都會被傳送到delegate方法,所有delegate方法均在queue中執行

                output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)

                //設置掃描二維碼類型

                output.metadataObjectTypes = [ AVMetadataObject.ObjectType.qr]

                //掃描區域

                //rectOfInterest 屬性中x和y互換,width和height互換。

                output.rectOfInterest = CGRect(x: 100/screenHeight, y: (screenWidth/2-100)/screenWidth, width: 200/screenHeight, height: 200/screenWidth)

            }

            

            

            //捕捉圖層

            let previewLayer = AVCaptureVideoPreviewLayer(session: session)

            previewLayer.videoGravity = .resizeAspectFill

            previewLayer.frame = self.view.layer.bounds

            self.view.layer.insertSublayer(previewLayer, at: 0)

            

            //持續對焦

            if device.isFocusModeSupported(.continuousAutoFocus){

                try  input.device.lockForConfiguration()

                input.device.focusMode = .continuousAutoFocus

                input.device.unlockForConfiguration()

            }

            session.startRunning()

            

        } catch  {

            

        }

        

    }

 

 

//掃描完成的代理

    func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {

        session?.stopRunning()

        if let metadataObject = metadataObjects.first {

            let readableObject = metadataObject as! AVMetadataMachineReadableCodeObject

           

            let str = readableObject.stringValue!

            let url = URL(string: str)

            //用網頁打開掃描的信息

            if #available(iOS 10, *){

                UIApplication.shared.open(url!, options: [:], completionHandler: nil)

            }else{

                UIApplication.shared.openURL(url!)

            }

        }

        

    }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM