1

我正在尝试阅读 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)  
}  
} 
4

1 回答 1

1

我为 qr 码的不一致阅读而苦苦挣扎,对我有用的是让 qr 码尽可能成比例。(如果从另一个 iphone/ipad 屏幕阅读)如果这对您没有帮助,http://www.appcoda.com/qr-code-reader-swift/ <-- 帮我欢呼

于 2015-09-28T22:21:49.333 回答