Created
February 19, 2018 22:12
-
-
Save nfarshchi/7039f0cf933424fcb7361e4f70e4cebe to your computer and use it in GitHub Desktop.
Binary/Raw data from QR Code scanning with swift 4.0 by AVFoundation.
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
// | |
// QRScanVC.swift | |
// | |
// Created by Farshchi, Navid on 1/2/18. | |
// | |
import UIKit | |
import AVFoundation | |
class QRScanVC: UIViewController, AVCaptureMetadataOutputObjectsDelegate { | |
//The camera preview frame | |
@IBOutlet weak var previewFrame: UIView! | |
var videoCaptureDevice = AVCaptureDevice.default(for: .video) | |
var device = AVCaptureDevice.default(for: .video) | |
var output = AVCaptureMetadataOutput() | |
var previewLayer: AVCaptureVideoPreviewLayer? | |
var captureSession = AVCaptureSession() | |
var code: String? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.setupCamera() | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
} | |
private func setupCamera() { | |
do { | |
let input = try AVCaptureDeviceInput(device: videoCaptureDevice!) | |
if self.captureSession.canAddInput(input) { | |
self.captureSession.addInput(input) | |
} | |
self.previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) | |
if let videoPreviewLayer = self.previewLayer { | |
videoPreviewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill | |
videoPreviewLayer.frame = self.previewFrame.bounds | |
self.previewFrame.layer.addSublayer(videoPreviewLayer) | |
} | |
let metadataOutput = AVCaptureMetadataOutput() | |
if self.captureSession.canAddOutput(metadataOutput) { | |
self.captureSession.addOutput(metadataOutput) | |
metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main) | |
metadataOutput.metadataObjectTypes = [AVMetadataObject.ObjectType.upce, AVMetadataObject.ObjectType.code39, AVMetadataObject.ObjectType.code39Mod43, AVMetadataObject.ObjectType.ean13, AVMetadataObject.ObjectType.ean8, AVMetadataObject.ObjectType.code93, AVMetadataObject.ObjectType.code128, AVMetadataObject.ObjectType.pdf417, AVMetadataObject.ObjectType.qr, AVMetadataObject.ObjectType.aztec] | |
} else { | |
print("Could not add metadata output") | |
} | |
} catch { | |
print("Error while setting camera") | |
} | |
} | |
override func viewWillAppear(_ animated: Bool) { | |
super.viewWillAppear(animated) | |
// This if for start capture session again while the VC become visible | |
if (captureSession.isRunning == false) { | |
captureSession.startRunning(); | |
} | |
} | |
override func viewWillDisappear(_ animated: Bool) { | |
super.viewWillDisappear(animated) | |
// This if for stopping capture session while the VC is not visible | |
if (captureSession.isRunning == true) { | |
captureSession.stopRunning(); | |
} | |
} | |
func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) { | |
print(metadataObjects) | |
for metadataObject in metadataObjects { | |
print("type: " + metadataObject.type.rawValue) | |
let readableObject = metadataObject as! AVMetadataMachineReadableCodeObject; | |
let rawReadableObjectTemp = readableObject.value(forKeyPath: "_internal.basicDescriptor")! as! [String:Any] | |
let rawReadableObject = rawReadableObjectTemp["BarcodeRawData"] as? Data | |
if let rawBytes = rawReadableObject { | |
print(rawBytes) | |
let dataString = rawBytes.hexEncodedString() | |
print("dataString: \(dataString)") | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment