-
Notifications
You must be signed in to change notification settings - Fork 1
/
CamerVIewControllerHidden.swift
123 lines (97 loc) · 4.22 KB
/
CamerVIewControllerHidden.swift
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
// CamerVIewController.swift
// HiddenCam
//
// Created by Daniel on 26/11/2019.
// Copyright © 2019 Daniel. All rights reserved.
//
import UIKit
import AVFoundation
class CameraViewControllerHidden: UIViewController {
let photoButton: UIButton = UIButton()
//Camera Capture requiered properties
var captureDevice : AVCaptureDevice!
let session = AVCaptureSession()
var stillImageOutput: AVCapturePhotoOutput!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
photoButton.frame = CGRect(x: 0, y: 0, width: 200, height: 40)
photoButton.backgroundColor = UIColor.red
photoButton.layer.masksToBounds = true
photoButton.setTitle("or?", for: .normal)
photoButton.setTitleColor(UIColor.white, for: .normal)
photoButton.layer.cornerRadius = 20.0
photoButton.layer.position = CGPoint(x: self.view.frame.width/2, y:200)
photoButton.addTarget(self, action: #selector(self.takePhoto(sender:)), for: .touchUpInside)
view.addSubview(photoButton)
let text: UITextView = UITextView(frame: CGRect(x: self.view.frame.width/2 - 150, y: 100, width: 300, height: 40))
text.text = "This is just a regular ViewController. There is nothing to see here."
text.textColor = UIColor.black
view.addSubview(text)
self.setupAVCapture()
}
@objc func takePhoto(sender: UIButton){
stillImageOutput.capturePhoto(with: AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.jpeg]), delegate: self)
}
}
// MARK: - AVCaptureSession related methods
extension CameraViewControllerHidden {
func setupAVCapture(){
session.sessionPreset = AVCaptureSession.Preset.vga640x480
guard let device = AVCaptureDevice
.default(AVCaptureDevice.DeviceType.builtInWideAngleCamera,
for: .video,
position: AVCaptureDevice.Position.front) else {
return
}
captureDevice = device
beginSession()
}
func beginSession(){
var deviceInput: AVCaptureDeviceInput!
do {
deviceInput = try AVCaptureDeviceInput(device: captureDevice)
guard deviceInput != nil else {
print("error: can't get deviceInput")
return
}
if self.session.canAddInput(deviceInput){
self.session.addInput(deviceInput)
}
stillImageOutput = AVCapturePhotoOutput()
if session.canAddOutput(stillImageOutput) {
session.addOutput(self.stillImageOutput)
}
// we can also attach videoDataOutput from the previous example to get all the video frames
// but now for the example image capturing is enough
session.startRunning()
} catch let error as NSError {
deviceInput = nil
print("error: \(error.localizedDescription)")
}
}
// clean up AVCapture
func stopCamera(){
session.stopRunning()
}
}
// MARK: - AVCapturePhotoCaptureDelegate
extension CameraViewControllerHidden: AVCapturePhotoCaptureDelegate {
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
//process single image
guard let imageData = photo.fileDataRepresentation()
else { return }
let captureImageView = UIImageView(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
let image = UIImage(data: imageData)
captureImageView.image = image
//show the captured image
let centerView = UIView(frame: CGRect(x: UIScreen.main.bounds.size.width / 2 - 100,
y: UIScreen.main.bounds.size.height / 2 - 100,
width: 200,
height: 200))
centerView.backgroundColor = UIColor.red
centerView.addSubview(captureImageView)
self.view.addSubview(centerView)
}
}