Created
February 16, 2016 21:50
-
-
Save Bashta/b37c8391b9750d2e47fe 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 ScannerViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate { | |
var captureSession: AVCaptureSession! | |
var previewLayer: AVCaptureVideoPreviewLayer! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.backgroundColor = UIColor.blackColor() | |
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 = [AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypePDF417Code] | |
} else { | |
failed() | |
return | |
} | |
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession); | |
previewLayer.frame = view.layer.bounds; | |
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; | |
view.layer.addSublayer(previewLayer); | |
captureSession.startRunning(); | |
} | |
func failed() { | |
let ac = UIAlertController(title: "Scanning not supported", message: "Your device does not support scanning a code from an item. Please use a device with a camera.", preferredStyle: .Alert) | |
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) | |
presentViewController(ac, animated: true, completion: nil) | |
captureSession = nil | |
} | |
override func viewWillAppear(animated: Bool) { | |
super.viewWillAppear(animated) | |
if (captureSession?.running == false) { | |
captureSession.startRunning(); | |
} | |
} | |
override func viewWillDisappear(animated: Bool) { | |
super.viewWillDisappear(animated) | |
if (captureSession?.running == true) { | |
captureSession.stopRunning(); | |
} | |
} | |
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) { | |
captureSession.stopRunning() | |
if let metadataObject = metadataObjects.first { | |
let readableObject = metadataObject as! AVMetadataMachineReadableCodeObject; | |
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate)) | |
foundCode(readableObject.stringValue); | |
} | |
dismissViewControllerAnimated(true, completion: nil) | |
} | |
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