64 lines
1.5 KiB
Swift
64 lines
1.5 KiB
Swift
import CoreImage
|
|
import AVFoundation
|
|
import UIKit
|
|
|
|
class ContentViewModel: ObservableObject {
|
|
|
|
@Published var error: Error?
|
|
@Published var frame: CGImage?
|
|
@Published var image: UIImage?
|
|
|
|
private var cameraManager: CameraManager {
|
|
.shared
|
|
}
|
|
private var frameManager: FrameManager {
|
|
.shared
|
|
}
|
|
|
|
init() {
|
|
setupSubscriptions()
|
|
}
|
|
|
|
func setupSubscriptions() {
|
|
frameManager.image = nil
|
|
frameManager.current = nil
|
|
|
|
cameraManager.$error
|
|
.receive(on: RunLoop.main)
|
|
.map { $0 }
|
|
.assign(to: &$error)
|
|
|
|
frameManager.$current
|
|
.receive(on: RunLoop.main)
|
|
.compactMap { buffer in
|
|
guard let image = CGImage.create(from: buffer) else {
|
|
return nil
|
|
}
|
|
|
|
let ciImage = CIImage(cgImage: image)
|
|
return FrameManager.shared.context.createCGImage(ciImage, from: ciImage.extent)
|
|
}
|
|
.assign(to: &$frame)
|
|
|
|
frameManager.$image
|
|
.receive(on: RunLoop.main)
|
|
.assign(to: &$image)
|
|
}
|
|
|
|
func endCapture() {
|
|
cameraManager.stopVideoCaptureSession()
|
|
}
|
|
|
|
func startCapture() {
|
|
cameraManager.startVideoCapture()
|
|
}
|
|
|
|
func captureImage() {
|
|
cameraManager.capturePhoto(delegate: frameManager)
|
|
}
|
|
|
|
func continuouslyFocusOnMiddle() {
|
|
cameraManager.continuouslyFocusOnMiddle()
|
|
}
|
|
}
|