使用AVFoundation swift改进阅读QR和PDF417代码

时间:2015-08-30 03:52:58

标签: swift avfoundation qr-code pdf417

我正在尝试阅读PDF-417和QR码以及我的代码可以正常工作,但是当光线较少或模糊图像不起作用时。所以我正在寻找有关如何改进阅读代码的信息。

我尝试更改相机上的设置:AVCaptureSession增加了分辨率设置和AVCaptureDevice增加了videoZoomFactor,但显然我做得不对。

我希望你能帮助我。

import UIKit  
import AVFoundation  
class ViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate { 



let session         : AVCaptureSession = AVCaptureSession()  
var previewLayer    : AVCaptureVideoPreviewLayer!  
var highlightView   : UIView = UIView()  

override func viewDidLoad() {  
    super.viewDidLoad()  

    /  
    self.highlightView.autoresizingMask =   UIViewAutoresizing.FlexibleTopMargin |  
        UIViewAutoresizing.FlexibleBottomMargin |  
        UIViewAutoresizing.FlexibleLeftMargin |  
        UIViewAutoresizing.FlexibleRightMargin  

    /  
    self.highlightView.layer.borderColor = UIColor.greenColor().CGColor  
    self.highlightView.layer.borderWidth = 3  

    /  
    self.view.addSubview(self.highlightView)  

    /  
    let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)  

    /  
    /  
    var error : NSError? = nil  


    let input : AVCaptureDeviceInput? = AVCaptureDeviceInput.deviceInputWithDevice(device, error: &error) as? AVCaptureDeviceInput  

    /  
    if input != nil {  
        session.addInput(input)  
    }  
    else {  
        /  
        println(error)  
    }  

    let output = AVCaptureMetadataOutput()  
    output.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())  
    session.addOutput(output)  
    output.metadataObjectTypes = output.availableMetadataObjectTypes  


    previewLayer = AVCaptureVideoPreviewLayer.layerWithSession(session) as! AVCaptureVideoPreviewLayer  
    previewLayer.frame = self.view.bounds  
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill  
    self.view.layer.addSublayer(previewLayer)  

    /  
    session.startRunning()  

}  

/  
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {  

    var highlightViewRect = CGRectZero  

    var barCodeObject : AVMetadataMachineReadableCodeObject!  

    var detectionString : String!  

    let barCodeTypes = [  
        AVMetadataObjectTypePDF417Code,  
        AVMetadataObjectTypeQRCode  
    ]  

    /  
    for metadata in metadataObjects {  

        for barcodeType in barCodeTypes {  

            if metadata.type == barcodeType  
            {  
                barCodeObject = self.previewLayer.transformedMetadataObjectForMetadataObject(metadata as! AVMetadataMachineReadableCodeObject) as! AVMetadataMachineReadableCodeObject  

                highlightViewRect = barCodeObject.bounds  

                detectionString = (metadata as! AVMetadataMachineReadableCodeObject).stringValue  

                self.session.stopRunning()  

                self.alert(detectionString)  
                break  
            }  

        }  
    }  

    println(detectionString)  
    self.highlightView.frame = highlightViewRect  
    self.view.bringSubviewToFront(self.highlightView)  

}  

func alert(Code: String)  
{  
    let actionSheet:UIAlertController = UIAlertController(title: "Barcode", message: "\(Code)", preferredStyle: UIAlertControllerStyle.Alert)  
    let firstAlertAction:UIAlertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler:  
    {  
        (alertAction:UIAlertAction!) in  
            self.session.startRunning()  
    })  
    actionSheet.addAction(firstAlertAction)  
    self.presentViewController(actionSheet, animated: true, completion: nil)  
}  
} 

1 个答案:

答案 0 :(得分:1)

我在不断阅读qr-codes方面苦苦挣扎,对我来说有用的是让qr-code尽可能成比例。 (如果从另一个iphone / ipad屏幕上阅读)如果这对你没有帮助,那么http://www.appcoda.com/qr-code-reader-swift/< - 帮助了我 欢呼声