Last active
June 28, 2016 20:57
-
-
Save yukas/bb41b543cc6aa9b43483aeeea97a4a0e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import AVFoundation | |
import UIKit | |
class ScanViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate { | |
var captureSession: AVCaptureSession! | |
var previewLayer: AVCaptureVideoPreviewLayer! | |
var previousScannedValue: String? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
captureSession = AVCaptureSession() | |
let videoCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) | |
let videoInput: AVCaptureDeviceInput | |
do { | |
videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice) | |
} catch { | |
return | |
} | |
if (captureSession.canAddInput(videoInput)) { | |
captureSession.addInput(videoInput) | |
} else { | |
failed(); | |
return | |
} | |
let metadataOutput = AVCaptureMetadataOutput() | |
if (captureSession.canAddOutput(metadataOutput)) { | |
captureSession.addOutput(metadataOutput) | |
metadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue()) | |
metadataOutput.metadataObjectTypes = [AVMetadataObjectTypeQRCode] | |
} else { | |
failed() | |
return | |
} | |
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) | |
previewLayer.frame = view.layer.bounds | |
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill | |
view.layer.addSublayer(previewLayer) | |
captureSession.startRunning() | |
} | |
func failed() { | |
print("Failed") | |
} | |
override func viewWillAppear(animated: Bool) { | |
super.viewWillAppear(animated) | |
if (captureSession?.running == false) { | |
captureSession.startRunning(); | |
} | |
} | |
override func viewDidAppear(animated: Bool) { | |
super.viewDidAppear(animated) | |
navigationController?.setNavigationBarHidden(true, animated: true) | |
} | |
override func viewWillDisappear(animated: Bool) { | |
super.viewWillDisappear(animated) | |
if (captureSession?.running == true) { | |
captureSession.stopRunning(); | |
} | |
} | |
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) { | |
if let metadataObject = metadataObjects.first { | |
let readableObject = metadataObject as! AVMetadataMachineReadableCodeObject; | |
if readableObject.stringValue == previousScannedValue { | |
return | |
} else { | |
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate)) | |
foundCode(readableObject.stringValue); | |
previousScannedValue = readableObject.stringValue | |
} | |
} | |
} | |
func foundCode(code: String) { | |
print(code) | |
} | |
override func prefersStatusBarHidden() -> Bool { | |
return true | |
} | |
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { | |
return .Portrait | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment