2021-02-19 08:28:05 -07:00
|
|
|
//
|
|
|
|
// CameraViewManager.swift
|
2021-06-21 14:42:46 -06:00
|
|
|
// mrousavy
|
2021-02-19 08:28:05 -07:00
|
|
|
//
|
|
|
|
// Created by Marc Rousavy on 09.11.20.
|
2021-06-01 05:07:57 -06:00
|
|
|
// Copyright © 2020 mrousavy. All rights reserved.
|
2021-02-19 08:28:05 -07:00
|
|
|
//
|
|
|
|
|
|
|
|
import AVFoundation
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
@objc(CameraViewManager)
|
|
|
|
final class CameraViewManager: RCTViewManager {
|
2021-05-06 06:11:55 -06:00
|
|
|
// pragma MARK: Properties
|
|
|
|
|
2021-03-26 09:28:08 -06:00
|
|
|
override var methodQueue: DispatchQueue! {
|
|
|
|
return DispatchQueue.main
|
|
|
|
}
|
2021-02-19 08:28:05 -07:00
|
|
|
|
|
|
|
override static func requiresMainQueueSetup() -> Bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-03-09 02:53:29 -07:00
|
|
|
override final func view() -> UIView! {
|
|
|
|
return CameraView()
|
2021-02-19 08:28:05 -07:00
|
|
|
}
|
|
|
|
|
2021-05-06 06:11:55 -06:00
|
|
|
// pragma MARK: React Functions
|
|
|
|
|
2023-02-13 07:22:45 -07:00
|
|
|
@objc
|
|
|
|
final func installFrameProcessorBindings() -> NSNumber {
|
2023-07-20 07:30:04 -06:00
|
|
|
#if VISION_CAMERA_ENABLE_FRAME_PROCESSORS
|
2023-07-21 09:52:30 -06:00
|
|
|
// Called on JS Thread (blocking sync method)
|
|
|
|
let result = VisionCameraInstaller.install(to: bridge)
|
|
|
|
return NSNumber(value: result)
|
2023-07-20 07:30:04 -06:00
|
|
|
#else
|
|
|
|
return false as NSNumber
|
|
|
|
#endif
|
2023-02-13 07:22:45 -07:00
|
|
|
}
|
|
|
|
|
2023-10-13 10:33:20 -06:00
|
|
|
// TODO: The startRecording() func cannot be async because RN doesn't allow
|
|
|
|
// both a callback and a Promise in a single function. Wait for TurboModules?
|
|
|
|
// This means that any errors that occur in this function have to be delegated through
|
|
|
|
// the callback, but I'd prefer for them to throw for the original function instead.
|
2021-02-19 08:28:05 -07:00
|
|
|
@objc
|
|
|
|
final func startRecording(_ node: NSNumber, options: NSDictionary, onRecordCallback: @escaping RCTResponseSenderBlock) {
|
2021-02-25 06:07:46 -07:00
|
|
|
let component = getCameraView(withTag: node)
|
2021-02-19 08:28:05 -07:00
|
|
|
component.startRecording(options: options, callback: onRecordCallback)
|
|
|
|
}
|
|
|
|
|
2022-03-22 03:44:58 -06:00
|
|
|
@objc
|
|
|
|
final func pauseRecording(_ node: NSNumber, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
|
|
let component = getCameraView(withTag: node)
|
|
|
|
component.pauseRecording(promise: Promise(resolver: resolve, rejecter: reject))
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc
|
|
|
|
final func resumeRecording(_ node: NSNumber, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
|
|
let component = getCameraView(withTag: node)
|
|
|
|
component.resumeRecording(promise: Promise(resolver: resolve, rejecter: reject))
|
|
|
|
}
|
|
|
|
|
2021-02-19 08:28:05 -07:00
|
|
|
@objc
|
|
|
|
final func stopRecording(_ node: NSNumber, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
2021-02-25 06:07:46 -07:00
|
|
|
let component = getCameraView(withTag: node)
|
2021-02-19 08:28:05 -07:00
|
|
|
component.stopRecording(promise: Promise(resolver: resolve, rejecter: reject))
|
|
|
|
}
|
2021-02-25 05:59:50 -07:00
|
|
|
|
2021-02-19 08:28:05 -07:00
|
|
|
@objc
|
|
|
|
final func takePhoto(_ node: NSNumber, options: NSDictionary, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
2021-02-25 06:07:46 -07:00
|
|
|
let component = getCameraView(withTag: node)
|
2021-02-19 08:28:05 -07:00
|
|
|
component.takePhoto(options: options, promise: Promise(resolver: resolve, rejecter: reject))
|
|
|
|
}
|
2021-02-25 05:59:50 -07:00
|
|
|
|
2021-02-19 08:28:05 -07:00
|
|
|
@objc
|
|
|
|
final func focus(_ node: NSNumber, point: NSDictionary, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
|
|
let promise = Promise(resolver: resolve, rejecter: reject)
|
|
|
|
guard let x = point["x"] as? NSNumber, let y = point["y"] as? NSNumber else {
|
2021-06-09 03:14:49 -06:00
|
|
|
promise.reject(error: .parameter(.invalid(unionName: "point", receivedValue: point.description)))
|
|
|
|
return
|
2021-02-19 08:28:05 -07:00
|
|
|
}
|
2021-02-25 06:07:46 -07:00
|
|
|
let component = getCameraView(withTag: node)
|
2021-02-19 08:28:05 -07:00
|
|
|
component.focus(point: CGPoint(x: x.doubleValue, y: y.doubleValue), promise: promise)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc
|
|
|
|
final func getCameraPermissionStatus(_ resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
|
|
withPromise(resolve: resolve, reject: reject) {
|
|
|
|
let status = AVCaptureDevice.authorizationStatus(for: .video)
|
|
|
|
return status.descriptor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc
|
|
|
|
final func getMicrophonePermissionStatus(_ resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
|
|
withPromise(resolve: resolve, reject: reject) {
|
|
|
|
let status = AVCaptureDevice.authorizationStatus(for: .audio)
|
|
|
|
return status.descriptor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc
|
|
|
|
final func requestCameraPermission(_ resolve: @escaping RCTPromiseResolveBlock, reject _: @escaping RCTPromiseRejectBlock) {
|
|
|
|
AVCaptureDevice.requestAccess(for: .video) { granted in
|
|
|
|
let result: AVAuthorizationStatus = granted ? .authorized : .denied
|
|
|
|
resolve(result.descriptor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc
|
|
|
|
final func requestMicrophonePermission(_ resolve: @escaping RCTPromiseResolveBlock, reject _: @escaping RCTPromiseRejectBlock) {
|
|
|
|
AVCaptureDevice.requestAccess(for: .audio) { granted in
|
|
|
|
let result: AVAuthorizationStatus = granted ? .authorized : .denied
|
|
|
|
resolve(result.descriptor)
|
|
|
|
}
|
|
|
|
}
|
2021-03-09 02:53:29 -07:00
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
2021-03-26 09:28:08 -06:00
|
|
|
private func getCameraView(withTag tag: NSNumber) -> CameraView {
|
|
|
|
// swiftlint:disable force_cast
|
|
|
|
return bridge.uiManager.view(forReactTag: tag) as! CameraView
|
2023-07-20 07:30:04 -06:00
|
|
|
// swiftlint:enable force_cast
|
2021-03-26 09:28:08 -06:00
|
|
|
}
|
2021-02-19 08:28:05 -07:00
|
|
|
}
|