From 91792d54dfe5041e47af41e2dc6d2f245c2adee2 Mon Sep 17 00:00:00 2001 From: Ernest Ibarolle <63222761+AdAstraCA@users.noreply.github.com> Date: Fri, 27 Jan 2023 01:44:05 -0800 Subject: [PATCH] Added camera functionality I added code that can help the app take photos. However, it can be a bit inconsistent, which I think is because of how the code is formatted. However, I can debug that later, since now that the camera is done, I will focus on the Arduino issues. --- Shared/ContentView.swift | 157 +++++++++++++++++++++++++++------------ 1 file changed, 111 insertions(+), 46 deletions(-) diff --git a/Shared/ContentView.swift b/Shared/ContentView.swift index d637d20..e49d80c 100644 --- a/Shared/ContentView.swift +++ b/Shared/ContentView.swift @@ -3,7 +3,7 @@ // Shared // // Created by Delta on 24/5/22. -// Edited by Astra on 1/8/23. +// Edited by Astra on 1/27/23. // import AVFoundation @@ -48,7 +48,57 @@ struct CameraView: View{ } }.onAppear { camera.Check() - }.frame(width: geometry.size.width, height: geometry.size.height / 1.4, alignment: .center).clipped() + }.frame(width: geometry.size.width, height: geometry.size.height / 1.6, alignment: .center).clipped() + HStack{ + + // if taken showing save and again take button... + + if camera.isTaken{ + Spacer() + + Button(action: camera.reTake, label: { + + Image(systemName: "arrow.triangle.2.circlepath.camera") + .foregroundColor(.black) + .padding() + .background(Color.white) + .clipShape(Circle()) + }) + .padding(.trailing,10) + + Spacer() + + Button(action: {if !camera.isSaved{camera.savePic()}}, label: { + Text(camera.isSaved ? "Saved" : "Save") + .foregroundColor(.black) + .fontWeight(.semibold) + .padding(.vertical,10) + .padding(.horizontal,20) + .background(Color.white) + .clipShape(Capsule()) + }) + .padding(.leading) + + 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) ScrollView(.horizontal, showsIndicators: false) { HStack { ForEach(filtersPreview,id: \.filter) { filterPreview in @@ -86,45 +136,16 @@ struct CameraView: View{ } } -struct TakePictureButton: View { - var isTaken: Bool - var body: some View { - VStack{ - HStack{ - if isTaken { - Button(action: { - playSound(sound: "mixkit-camera-shutter-click-1133", type: "wav") - }, label: { - - }) - }else{ - Button(action: { - playSound(sound: "mixkit-camera-shutter-click-1133", type: "wav") - }, label: - { - ZStack{ - Circle() - .fill(Color.white) - .frame(width: 65, height: 65, alignment: .center) - Circle() - .stroke(Color.white, lineWidth: 2) - .frame(width: 75, height: 75, alignment: .center) - } - }) - } - } - } - } -} - //camera model -class CameraModel: ObservableObject{ +class CameraModel: NSObject, ObservableObject, AVCapturePhotoCaptureDelegate{ @Published var isTaken = false @Published var session = AVCaptureSession() @Published var alert = false + @Published var isSaved = false @Published var output = AVCapturePhotoOutput() @Published var preview: AVCaptureVideoPreviewLayer! + @Published var picData = Data(count: 0) func Check(){ switch AVCaptureDevice.authorizationStatus(for: .video){ @@ -175,6 +196,62 @@ class CameraModel: ObservableObject{ print(error.localizedDescription) } } + + //take and retake photos + + func takePic(){ + + self.output.capturePhoto(with: AVCapturePhotoSettings(), delegate: self) + + DispatchQueue.global(qos: .background).async { + + self.session.stopRunning() + + DispatchQueue.main.async { + withAnimation{self.isTaken.toggle()} + } + } + } + + func reTake(){ + + DispatchQueue.global(qos: .background).async { + + self.session.startRunning() + + DispatchQueue.main.async { + withAnimation{self.isTaken.toggle()} + //clearing ... + self.isSaved = false + self.picData = Data(count: 0) + } + } + } + + 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(){ + + guard let image = UIImage(data: self.picData) else{return} + + // saving Image... + UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) + + self.isSaved = true + + print("saved Successfully....") + } } // setting view for preview @@ -202,15 +279,3 @@ struct CameraPreview: UIViewRepresentable { } } - -func playSound(sound: String, type: String) { - var audioPlayer: AVAudioPlayer? - if let path = Bundle.main.path(forResource: sound, ofType: type) { - do { - audioPlayer = try AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: path) as URL) - audioPlayer?.play() - } catch { - print("Could not find and play the sound file.") - } - } -}