1

I'm trying to read PDF-417 and QR code and my code works but when there is less light or blurred image does not work. So I am looking for information about how may improve reading code.

I tried changing the settings on the camera: AVCaptureSession Increased the resolution settings and the AVCaptureDevice Increased videoZoomFactor but apparently I'm not doing it correctly.

I hope you can help me.

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)  
}  
} 
corocraft
  • 150
  • 3
  • 13

1 Answers1

1

I struggled with unconsistent reading of qr-codes, what worked for me was to get the qr-code as proportional as possible. (If reading from another iphone/ipad screen) If that doesnt help you, the http://www.appcoda.com/qr-code-reader-swift/ <-- helped me out cheers

Stav1
  • 132
  • 2
  • 11