在我的故事板上,我有一个视图控制器,它有以下与之关联的类:
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
}
}
此代码基本上启动了充当条形码扫描仪的摄像机视图。如果您将设备指向条形码,它将检测条形码,并将结果推送到文本字段。
这一切都很棒。我的问题是 - 我想在屏幕上显示一些其他元素。现在,我的整个屏幕都被相机超越了,所以如果你访问这个视图,你要么必须扫描条形码,要么你必须关闭应用程序。
我试图在视图中添加其他元素,只需将它们从对象库拖放即可,然而,相机似乎覆盖了这些项目,使其无法访问。
我想在相机视图的顶部添加两个元素。第一个是图像,看起来像一个“括号”,用于指示用户应该“适合”他们正在扫描的条形码。
第二个是允许用户取消摄像机视图的按钮。
所以最终,我的问题很简单,如何在以编程方式初始化的摄像机视图顶部添加元素?
答案 0 :(得分:0)
在视图控制器中添加视图如果要显示视频预览,则只需将预览图层添加到该图层而不是整个视图。
像这样:
//初始化视频预览图层并将其作为子图层添加到视图图层。
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!)
//而不是
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession);
previewLayer.frame = view.layer.bounds;
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
view.layer.addSublayer(previewLayer);