你好,歡迎來到IOS教程網

 Ios教程網 >> IOS使用技巧 >> IOS7技巧 >> ios開發之Swift使用AVFoundation實現條形碼掃描(附:拉近鏡頭改善讀取)

ios開發之Swift使用AVFoundation實現條形碼掃描(附:拉近鏡頭改善讀取)

編輯:IOS7技巧
本文章我們來看一篇ios開發之Swift使用AVFoundation實現條形碼掃描(附:拉近鏡頭改善讀取)的例子,希望能夠讓各位ios開發者了解更多的Swift開發教程。

1,條形碼(一維碼)的掃描讀取
原來寫過一篇文章,介紹如何使用攝像頭掃描讀取二維碼:Swift - 二維碼QRCode的讀取(從圖片讀取 ,或通過攝像頭掃描)要通過攝像頭讀取條形碼,只需要將原來二維碼讀取代碼中 metadataObjectTypes 做如下修改即可:

self.output.metadataObjectTypes = [AVMetadataObjectTypeEAN13Code,
        AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code,
        AVMetadataObjectTypeCode39Code,AVMetadataObjectTypeCode93Code]
2,拉近鏡頭,改善條形碼讀取效果

有網友反應,如果條碼太小的時候(比如iwatch上支付寶的生成的小條碼)就會識別不出來。 解決辦法是:像支付寶、QQ一樣,通過代碼拉近鏡頭焦距,放大內容區域讓機器更好的識別。   下面左圖是原始大小,右圖將畫面放大了1.5倍:   原文:Swift - 使用AVFoundation實現條形碼掃描(附:拉近鏡頭改善讀取)   原文:Swift - 使用AVFoundation實現條形碼掃描(附:拉近鏡頭改善讀取)


import UIKit
import AVFoundation
 
class ViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate,
UIAlertViewDelegate{
     
    var scanRectView:UIView!
    var device:AVCaptureDevice!
    var input:AVCaptureDeviceInput!
    var output:AVCaptureMetadataOutput!
    var session:AVCaptureSession!
    var preview:AVCaptureVideoPreviewLayer!
     
    override func viewDidLoad() {
        super.viewDidLoad()
         
        fromCamera()
    }
     
    //通過攝像頭掃描
    func fromCamera() {
        do{
            self.device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
             
            self.input = try AVCaptureDeviceInput(device: device)
             
            self.output = AVCaptureMetadataOutput()
            output.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
             
            self.session = AVCaptureSession()
            if UIScreen.mainScreen().bounds.size.height<500 {
                self.session.sessionPreset = AVCaptureSessionPreset640x480
            }else{
                self.session.sessionPreset = AVCaptureSessionPresetHigh
            }
             
            self.session.addInput(self.input)
            self.session.addOutput(self.output)
             
            self.output.metadataObjectTypes = [AVMetadataObjectTypeEAN13Code,
                AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code,
                AVMetadataObjectTypeCode39Code,AVMetadataObjectTypeCode93Code]
             
            //計算中間可探測區域
            let windowSize:CGSize = UIScreen.mainScreen().bounds.size;
            let scanSize:CGSize = CGSizeMake(windowSize.width*3/4,
                windowSize.width*3/4);
            var scanRect:CGRect = CGRectMake((windowSize.width-scanSize.width)/2,
                (windowSize.height-scanSize.height)/2, scanSize.width, scanSize.height);
            //計算rectOfInterest 注意x,y交換位置
            scanRect = CGRectMake(scanRect.origin.y/windowSize.height,
                scanRect.origin.x/windowSize.width,
                scanRect.size.height/windowSize.height,
                scanRect.size.width/windowSize.width);
            //設置可探測區域
            self.output.rectOfInterest = scanRect
             
            self.preview = AVCaptureVideoPreviewLayer(session:self.session)
            self.preview.videoGravity = AVLayerVideoGravityResizeAspectFill
            self.preview.frame = UIScreen.mainScreen().bounds
            self.view.layer.insertSublayer(self.preview, atIndex:0)
             
            //添加中間的探測區域綠框
            self.scanRectView = UIView();
            self.view.addSubview(self.scanRectView)
            self.scanRectView.frame = CGRectMake(0, 0, scanSize.width, scanSize.height);
            self.scanRectView.center = CGPointMake(
                CGRectGetMidX(UIScreen.mainScreen().bounds),
                CGRectGetMidY(UIScreen.mainScreen().bounds));
            self.scanRectView.layer.borderColor = UIColor.greenColor().CGColor
            self.scanRectView.layer.borderWidth = 1;
             
            //開始捕獲
            self.session.startRunning()
             
            //放大
            do {
                try self.device!.lockForConfiguration()
            } catch _ {
                NSLog("Error: lockForConfiguration.");
            }
            self.device!.videoZoomFactor = 1.5
            self.device!.unlockForConfiguration()
             
        }catch _ as NSError{
            //打印錯誤消息
            let errorAlert = UIAlertView(title: "提醒",
                message: "請在iPhone的\"設置-隱私-相機\"選項中,允許本程序訪問您的相機",
                delegate: self,
                cancelButtonTitle: "確定")
            errorAlert.show()
        }
    }
     
    //攝像頭捕獲
    func captureOutput(captureOutput: AVCaptureOutput!,
        didOutputMetadataObjects metadataObjects: [AnyObject]!,
        fromConnection connection: AVCaptureConnection!) {
             
            var stringValue:String?
            if metadataObjects.count > 0 {
                let metadataObject = metadataObjects[0]
                    as! AVMetadataMachineReadableCodeObject
                stringValue = metadataObject.stringValue
                 
                if stringValue != nil{
                    self.session.stopRunning()
                }
            }
            self.session.stopRunning()
            //輸出結果
            let alertView = UIAlertView(title: "二維碼", message: stringValue,
                delegate: self, cancelButtonTitle: "確定")
            alertView.show()
    }
     
    //消息框確認後消失
    func alertView(alertView: UIAlertView, willDismissWithButtonIndex buttonIndex: Int) {
        //繼續掃描
        self.session.startRunning()
    }
     
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved