-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnotherCameraView.swift
242 lines (199 loc) · 7.64 KB
/
AnotherCameraView.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//
// AnotherCameraView.swift
// SheetVision
//
// Created by Juan Carlos Martínez Sevilla on 15/5/23.
//
//
// ScannerView.swift
// Scan-Ocr
//
// Created by Haaris Iqubal on 5/21/21.
//
import VisionKit
import SwiftUI
import AVFoundation
struct AnotherCameraView: View {
@StateObject var camera = CameraModel()
var body: some View {
ZStack {
CameraPreview(camera: camera).ignoresSafeArea(.all, edges: .all)
VStack {
if camera.isTaken{
HStack {
Spacer()
Button(action: camera.reTake, label: {
Image(systemName: "arrow.triangle.2.circlepath.camera")
.foregroundColor(.black)
.padding()
.background(.white)
.clipShape(Circle())
})
.padding(.trailing, 10)
}
}
Spacer()
HStack {
if camera.isTaken {
Button(action: {if !camera.isSaved{camera.savePic()}}, label: {
Text(camera.isSaved ? "Saved" : "Save")
.foregroundColor(.black)
.fontWeight(.semibold)
.padding(.vertical, 10)
.padding(.horizontal, 20)
.background(.white)
.clipShape(Capsule())
}).padding()
Spacer()
}
else {
Button(action: camera.takePic, label: {
ZStack {
Circle()
.fill(Color.white)
.frame(width: 65, height: 65)
Circle()
.stroke(Color.white, lineWidth: 2)
.frame(width: 75, height: 75)
}
})
}
}
.frame(height: 75)
}
}
.onAppear(perform: {
camera.Check()
})
}
}
// Camera
class CameraModel : NSObject, ObservableObject, AVCapturePhotoCaptureDelegate {
@Published var isTaken = false
@Published var session = AVCaptureSession()
@Published var alert = false
@Published var output = AVCapturePhotoOutput()
@Published var preview : AVCaptureVideoPreviewLayer!
@Published var isSaved = false
@Published var picData = Data(count: 0)
func Check() {
switch AVCaptureDevice.authorizationStatus(for: .video) {
case .authorized:
setUp()
return
case .notDetermined:
AVCaptureDevice.requestAccess(for: .video) {
(status) in
if status {
self.setUp()
}
}
case .denied:
self.alert.toggle()
return
default:
return
}
}
func setUp() {
do {
self.session.beginConfiguration()
let device = AVCaptureDevice.default(for: .video)
let input = try AVCaptureDeviceInput(device: device!)
if self.session.canAddInput(input) {
self.session.addInput(input)
}
if self.session.canAddOutput(self.output) {
self.session.addOutput(self.output)
}
self.session.commitConfiguration()
} catch {
print(error.localizedDescription)
}
}
func takePic() {
DispatchQueue.global(qos: .background).async {
self.output.capturePhoto(with: AVCapturePhotoSettings(), delegate: self)
self.session.stopRunning()
DispatchQueue.main.async {
withAnimation{
self.isTaken.toggle()
}
}
}
}
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
if error != nil {
return
}
print("pic taken")
guard let imageData = photo.fileDataRepresentation() else {return}
self.picData = imageData
}
func savePic() {
let image = UIImage(data: self.picData)!
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
print("saved")
self.isSaved = true
}
func reTake() {
DispatchQueue.global(qos: .background).async {
self.session.startRunning()
DispatchQueue.main.async {
withAnimation{
self.isTaken.toggle()
self.isSaved = false
}
}
}
}
}
struct AnotherCameraView_Previews: PreviewProvider {
static var previews: some View {
AnotherCameraView()
}
}
struct CameraPreview: UIViewRepresentable {
@ObservedObject var camera: CameraModel
func makeUIView(context: Context) -> UIView {
let currentOrientation = UIDevice.current.orientation
camera.preview = AVCaptureVideoPreviewLayer(session: camera.session)
let view = UIView(frame: UIScreen.main.bounds)
camera.preview.frame = UIView(frame: UIScreen.main.bounds).frame
self.camera.preview.videoGravity = .resizeAspectFill
updateUIForDeviceOrientation(currentOrientation, view)
view.layer.addSublayer(camera.preview)
camera.session.startRunning()
return view
}
func updateUIView(_ uiView: UIView, context: Context) {
// Register for device orientation change notifications
NotificationCenter.default.addObserver(forName: UIDevice.orientationDidChangeNotification, object: nil, queue: .main) { _ in
// Handle device orientation change here
let currentOrientation = UIDevice.current.orientation
// Update the UI based on the device orientation
updateUIForDeviceOrientation(currentOrientation, uiView)
self.camera.preview.frame = UIView(frame: UIScreen.main.bounds).frame
}
}
private func updateUIForDeviceOrientation(_ orientation: UIDeviceOrientation, _ uiView: UIView) {
// Update the UI based on the device orientation
switch orientation {
case .portrait:
// Handle portrait orientation
uiView.layer.transform = CATransform3DMakeRotation(0, 0, 0, 1)
case .landscapeLeft:
// Handle landscape left orientation
uiView.layer.transform = CATransform3DMakeRotation(-.pi / 2, 0, 0, 1)
case .landscapeRight:
// Handle landscape right orientation
uiView.layer.transform = CATransform3DMakeRotation(.pi / 2, 0, 0, 1)
case .portraitUpsideDown:
// Handle landscape right orientation
uiView.layer.transform = CATransform3DMakeRotation(.pi, 0, 0, 1)
default:
// Handle other orientations if needed
break
}
}
}