我正在尝试阅读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)
}
}
我挣扎于qr码的不一致读取,对我来说有效的是使qr码尽可能成比例。(如果从另一个iphone/ipad屏幕上阅读)如果这对你没有帮助,http://www.appcoda.com/qr-code-reader-swift/<——帮了我欢呼声