Skip to content

Instantly share code, notes, and snippets.

@Katafalkas
Last active February 2, 2016 19:33
Show Gist options
  • Save Katafalkas/603defb660111f1de7e8 to your computer and use it in GitHub Desktop.
Save Katafalkas/603defb660111f1de7e8 to your computer and use it in GitHub Desktop.
Implementing ZBar Barcode scanner Swift 2.0
use_frameworks!
pod 'ZBarSDK'
protocol ScanViewControllerDelegate: NSObjectProtocol {
func scanViewController(scanViewController: ScanViewController, didFindBarcode barcode: String)
}
extension ZBarSymbolSet: SequenceType {
public func generate() -> NSFastGenerator {
return NSFastGenerator(self)
}
}
class ScanViewController: ZBarReaderViewController, ZBarReaderDelegate {
weak var delegate: ScanViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
self.readerDelegate = self
self.supportedOrientationsMask = 0
self.readerView.zoom = 1.0
self.modalInPopover = false
self.showsZBarControls = false
self.scanner.setSymbology(ZBAR_UPCA, config: ZBAR_CFG_ENABLE, to: 1)
}
// MARK: - ZBarReaderDelegate
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let results: NSFastEnumeration = info[ZBarReaderControllerResults] as! NSFastEnumeration
var symbolFound : ZBarSymbol?
for symbol in results as! ZBarSymbolSet {
symbolFound = symbol as? ZBarSymbol
break
}
delegate?.scanViewController(self, didFindBarcode: String(symbolFound!.data))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment