On my storyboard, I have a view controller, that has the following class associated with it:
import AVFoundation
import UIKit
class ScannerViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
var captureSession: AVCaptureSession!
var previewLayer: AVCaptureVideoPreviewLayer!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.black
captureSession = AVCaptureSession()
let videoCaptureDevice = AVCaptureDevice.defaultDevice(withMediaType: 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: DispatchQueue.main)
metadataOutput.metadataObjectTypes = [AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeCode39Code]
} 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))
present(ac, animated: true)
captureSession = nil
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if (captureSession?.isRunning == false) {
captureSession.startRunning();
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if (captureSession?.isRunning == true) {
captureSession.stopRunning();
}
}
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
captureSession.stopRunning()
let initialView: PLLogViewController = presentingViewController as! PLLogViewController
if let metadataObject = metadataObjects.first {
let readableObject = metadataObject as! AVMetadataMachineReadableCodeObject;
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
found(code: readableObject.stringValue);
initialView.setResults(code: readableObject.stringValue)
}
dismiss(animated: true)
}
func found(code: String) {
print(code)
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
}
This code basically initiates a camera view that acts as a barcode scanner. If you point your device at a barcode, it'll detect the barcode, and push the results to a text field.
This all works great. My question is - I would like to have some other elements showing on the screen. Right now, my entire screen is being overtaken by the camera, and so if you access this view, you either HAVE to scan a barcode, or you have to shut down the app.
I attempted to add other elements in the view just by dragging them from the object library, however, the camera seems to overlay those items, making them inaccessible.
There are two elements that I would like to add over top of the camera view. The first is an image, that looks like a "bracket" to give some indication of where the user should "fit" the barcode they are scanning.
The second is a button that allows the user to cancel out of the camera view.
So ultimately, my question is simply, how can I add elements over top of a camera view that is being initialized programmatically?
add a view in you view controller were you want your video preview to appear then simply add the preview layer to that layer instead of the whole view.
Like so:
// Initialize the video preview layer and add it as a sublayer to your views layer.
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
previewLayer?.frame = /*your views name here i e qrView*/.layer.bounds
/*your views name here i e qrView*/.layer.addSublayer(videoPreviewLayer!)
// instead of
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession);
previewLayer.frame = view.layer.bounds;
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
view.layer.addSublayer(previewLayer);