Skip to content

Commit

Permalink
Added camera functionality
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
eibarolle authored Jan 27, 2023
1 parent 2180752 commit 91792d5
Showing 1 changed file with 111 additions and 46 deletions.
157 changes: 111 additions & 46 deletions Shared/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.")
}
}
}

0 comments on commit 91792d5

Please sign in to comment.