在我的 Storyboard上,我有一个 View Controller ,它有以下关联的类:
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
}
}
这段代码基本上启动了一个充当条形码扫描仪的相机 View 。如果您将设备指向条形码,它会检测条形码,并将结果推送到文本字段。
这一切都很好。我的问题是 - 我想在屏幕上显示一些其他元素。现在,我的整个屏幕都被摄像头占据了,所以如果你访问这个 View ,你要么必须扫描条形码,要么必须关闭应用程序。
我试图通过从对象库中拖动它们来在 View 中添加其他元素,但是,相机似乎覆盖了这些项目,使它们无法访问。
我想在相机 View 的顶部添加两个元素。第一个是图像,它看起来像一个“括号”,用于指示用户应该“适合”他们正在扫描的条形码的位置。
第二个是允许用户取消相机 View 的按钮。
所以最终,我的问题很简单,如何在以编程方式初始化的相机 View 之上添加元素?
最佳答案
如果您希望显示视频预览,则在您的 View Controller 中添加一个 View ,然后只需将预览图层添加到该图层而不是整个 View 。
像这样:
//初始化视频预览层并将其作为子层添加到您的 View 层。
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);
关于ios - 在条形码扫描仪应用程序 (Swift 3) 中的相机 View 上添加对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39939683/