File size: 1,798 Bytes
2d8be8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 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
    }
  }
}