2 QR кодын тану
class ReaderViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
@IBOutlet weak var cameraContainerView: UIView!
@IBOutlet weak var cameraContainerHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var startStopButton: UIButton!
@IBOutlet weak var outputTextView: UITextView!
var captureSession: AVCaptureSession!
var previewLayer: AVCaptureVideoPreviewLayer!
var qrCodeBounds:UIView = {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
view.layer.borderColor = UIColor.green.cgColor
view.layer.borderWidth = 3
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.black
self.captureSession = AVCaptureSession()
guard let videoCaptureDevice = AVCaptureDevice.default(for: .video) else { return }
let videoInput: AVCaptureDeviceInput
do {
videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
} catch {
return
}
Ә қосымшасының жалғасы
if (self.captureSession.canAddInput(videoInput)) {
self.captureSession.addInput(videoInput)
} else {
self.failed()
return
}
let metadataOutput = AVCaptureMetadataOutput()
if (self.captureSession.canAddOutput(metadataOutput)) {
self.captureSession.addOutput(metadataOutput)
metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
metadataOutput.metadataObjectTypes = [.qr] // Also have things like Face, body, cats
} else {
self.failed()
return
}
self.previewLayer = AVCaptureVideoPreviewLayer(session: self.captureSession)
self.previewLayer.frame = view.layer.bounds
self.previewLayer.videoGravity = .resizeAspectFill
self.cameraContainerView.layer.addSublayer(self.previewLayer)
self.qrCodeBounds.alpha = 0
self.cameraContainerView.addSubview(self.qrCodeBounds)
self.captureSession.startRunning()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if (self.captureSession?.isRunning == false) {
self.captureSession?.startRunning()
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if (self.captureSession?.isRunning == true) {
self.captureSession?.stopRunning()
}
}
Достарыңызбен бөлісу: |