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 に答える