mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-05-05 15:37:45 +02:00
MESSENGER-4093 scan permalinks as qr
This commit is contained in:
committed by
Frank Rotermund
parent
1bff461abb
commit
eb618b395d
@@ -16,8 +16,6 @@
|
||||
*/
|
||||
|
||||
import SwiftUI
|
||||
import UIKit
|
||||
import AVFoundation
|
||||
|
||||
struct AuthenticationServerSelectionQRCodeScanner: View {
|
||||
@Environment(\.presentationMode) var presentationMode
|
||||
@@ -44,102 +42,3 @@ struct AuthenticationServerSelectionQRCodeScanner: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ScannerView: UIViewControllerRepresentable {
|
||||
@Binding var qrCode: String
|
||||
@Binding var scanCompleted: Bool
|
||||
|
||||
class Coordinator: NSObject, AVCaptureMetadataOutputObjectsDelegate {
|
||||
var parent: ScannerView
|
||||
|
||||
init(_ parent: ScannerView) {
|
||||
self.parent = parent
|
||||
}
|
||||
|
||||
func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
|
||||
|
||||
if let metadataObject = metadataObjects.first {
|
||||
guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject else { return }
|
||||
guard let stringValue = readableObject.stringValue else { return }
|
||||
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
|
||||
DispatchQueue.main.async {
|
||||
self.parent.qrCode = stringValue
|
||||
self.parent.scanCompleted = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func makeCoordinator() -> Coordinator {
|
||||
Coordinator(self)
|
||||
}
|
||||
|
||||
func makeUIViewController(context: Context) -> some ScannerViewController {
|
||||
let controller = ScannerViewController()
|
||||
controller.delegate = context.coordinator
|
||||
return controller
|
||||
}
|
||||
|
||||
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class ScannerViewController: UIViewController {
|
||||
var captureSession: AVCaptureSession?
|
||||
var previewLayer: AVCaptureVideoPreviewLayer!
|
||||
let metadataOutput = AVCaptureMetadataOutput()
|
||||
weak var delegate: AVCaptureMetadataOutputObjectsDelegate?
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
|
||||
view.backgroundColor = UIColor.black
|
||||
captureSession = AVCaptureSession()
|
||||
|
||||
guard let videoCaptureDevice = AVCaptureDevice.default(for: .video), let videoInput = try? AVCaptureDeviceInput(device: videoCaptureDevice) else {
|
||||
captureSession = nil
|
||||
return
|
||||
}
|
||||
|
||||
if let captureSession = captureSession {
|
||||
guard captureSession.canAddInput(videoInput) && captureSession.canAddOutput(metadataOutput) else {
|
||||
self.captureSession = nil
|
||||
return
|
||||
}
|
||||
|
||||
captureSession.addInput(videoInput)
|
||||
captureSession.addOutput(metadataOutput)
|
||||
|
||||
metadataOutput.setMetadataObjectsDelegate(delegate, queue: .main)
|
||||
metadataOutput.metadataObjectTypes = [.qr]
|
||||
|
||||
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
|
||||
previewLayer.frame = view.layer.bounds
|
||||
previewLayer.videoGravity = .resizeAspectFill
|
||||
view.layer.addSublayer(previewLayer)
|
||||
}
|
||||
}
|
||||
|
||||
override func viewWillAppear(_ animated: Bool) {
|
||||
super.viewWillAppear(animated)
|
||||
|
||||
let dispatchQueue = DispatchQueue(label: "AVCapturesession.startRunning", qos: .background)
|
||||
dispatchQueue.async{
|
||||
if (self.captureSession?.isRunning == false) {
|
||||
self.captureSession?.startRunning()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override func viewWillDisappear(_ animated: Bool) {
|
||||
super.viewWillDisappear(animated)
|
||||
|
||||
let dispatchQueue = DispatchQueue(label: "AVCapturesession.stopRunning", qos: .background)
|
||||
dispatchQueue.async{
|
||||
if (self.captureSession?.isRunning == true) {
|
||||
self.captureSession?.stopRunning()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user