Skip to content

Instantly share code, notes, and snippets.

@MSchmidt
Created January 9, 2017 12:06
Show Gist options
  • Save MSchmidt/38f191bfc19d07ea41567e6e0210b139 to your computer and use it in GitHub Desktop.
Save MSchmidt/38f191bfc19d07ea41567e6e0210b139 to your computer and use it in GitHub Desktop.
Swift Barcode Scanner
//
// SecondViewController.swift
// Scanmaster
//
// Created by Matthias Schmidt on 05/01/2017.
// Copyright © 2017 Pixelflush. All rights reserved.
//
import UIKit
import AVFoundation
class SecondViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
var session: AVCaptureSession!
var previewLayer: AVCaptureVideoPreviewLayer!
override func viewDidLoad() {
super.viewDidLoad()
print("test 1")
// Create a session object.
session = AVCaptureSession()
// Set the captureDevice.
let videoCaptureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
// Create input object.
let videoInput: AVCaptureDeviceInput?
do {
videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
} catch {
print("return")
return
}
// Add input to the session.
if (session.canAddInput(videoInput)) {
session.addInput(videoInput)
} else {
scanningNotPossible()
}
let metadataOutput = AVCaptureMetadataOutput()
// Add output to the session.
if (session.canAddOutput(metadataOutput)) {
session.addOutput(metadataOutput)
// Send captured data to the delegate object via a serial queue.
metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
// Set barcode type for which to scan: Code128.
metadataOutput.metadataObjectTypes = [AVMetadataObjectTypeCode128Code]
// Add previewLayer and have it show the video data.
previewLayer = AVCaptureVideoPreviewLayer(session: session);
previewLayer.frame = view.layer.bounds;
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
view.layer.addSublayer(previewLayer);
// Begin the capture session.
session.startRunning()
} else {
scanningNotPossible()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func scanningNotPossible() {
print("scanningNotPossible()")
// Let the user know that scanning isn't possible with the current device.
let alert = UIAlertController(title: "Can't Scan.", message: "Let's try a device equipped with a camera.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(alert, animated: true, completion: nil)
session = nil
}
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
// Get the first object from the metadataObjects array.
if let barcodeData = metadataObjects.first {
// Turn it into machine readable code
let barcodeReadable = barcodeData as? AVMetadataMachineReadableCodeObject;
if let readableCode = barcodeReadable {
// Send the barcode as a string to barcodeDetected()
// barcodeDetected(readableCode.stringValue);
print(readableCode.stringValue)
}
// Vibrate the device to give the user some feedback.
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
// Avoid a very buzzy device.
session.stopRunning()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment