mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-09-22 21:30:44 +02:00
feat: add barcode scanner plugin (#536)
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import AVFoundation
|
||||
import UIKit
|
||||
|
||||
class CameraView: UIView {
|
||||
var videoPreviewLayer: AVCaptureVideoPreviewLayer?
|
||||
|
||||
func interfaceOrientationToVideoOrientation(_ orientation: UIInterfaceOrientation)
|
||||
-> AVCaptureVideoOrientation
|
||||
{
|
||||
switch orientation {
|
||||
case UIInterfaceOrientation.portrait:
|
||||
return AVCaptureVideoOrientation.portrait
|
||||
case UIInterfaceOrientation.portraitUpsideDown:
|
||||
return AVCaptureVideoOrientation.portraitUpsideDown
|
||||
case UIInterfaceOrientation.landscapeLeft:
|
||||
return AVCaptureVideoOrientation.landscapeLeft
|
||||
case UIInterfaceOrientation.landscapeRight:
|
||||
return AVCaptureVideoOrientation.landscapeRight
|
||||
default:
|
||||
return AVCaptureVideoOrientation.portraitUpsideDown
|
||||
}
|
||||
}
|
||||
|
||||
override func layoutSubviews() {
|
||||
super.layoutSubviews()
|
||||
if let sublayers = self.layer.sublayers {
|
||||
for layer in sublayers {
|
||||
layer.frame = self.bounds
|
||||
}
|
||||
}
|
||||
|
||||
if let interfaceOrientation = UIApplication.shared.windows.first(where: { $0.isKeyWindow })?
|
||||
.windowScene?.interfaceOrientation
|
||||
{
|
||||
self.videoPreviewLayer?.connection?.videoOrientation = interfaceOrientationToVideoOrientation(
|
||||
interfaceOrientation)
|
||||
}
|
||||
}
|
||||
|
||||
func addPreviewLayer(_ previewLayer: AVCaptureVideoPreviewLayer?) {
|
||||
previewLayer!.videoGravity = AVLayerVideoGravity.resizeAspectFill
|
||||
previewLayer!.frame = self.bounds
|
||||
self.layer.addSublayer(previewLayer!)
|
||||
self.videoPreviewLayer = previewLayer
|
||||
}
|
||||
|
||||
func removePreviewLayer() {
|
||||
if self.videoPreviewLayer != nil {
|
||||
self.videoPreviewLayer!.removeFromSuperlayer()
|
||||
self.videoPreviewLayer = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user