2023-09-26 03:39:17 -06:00
|
|
|
import React from 'react'
|
|
|
|
import { requireNativeComponent, NativeSyntheticEvent, findNodeHandle, NativeMethods } from 'react-native'
|
|
|
|
import type { CameraDevice } from './CameraDevice'
|
|
|
|
import type { ErrorWithCause } from './CameraError'
|
|
|
|
import { CameraCaptureError, CameraRuntimeError, tryParseNativeCameraError, isErrorWithCause } from './CameraError'
|
|
|
|
import type { CameraProps, FrameProcessor } from './CameraProps'
|
|
|
|
import { CameraModule } from './NativeCameraModule'
|
|
|
|
import type { PhotoFile, TakePhotoOptions } from './PhotoFile'
|
|
|
|
import type { Point } from './Point'
|
|
|
|
import type { RecordVideoOptions, VideoFile } from './VideoFile'
|
|
|
|
import { VisionCameraProxy } from './FrameProcessorPlugins'
|
|
|
|
import { CameraDevices } from './CameraDevices'
|
|
|
|
import type { EmitterSubscription } from 'react-native'
|
2021-02-19 08:07:53 -07:00
|
|
|
|
|
|
|
//#region Types
|
2023-09-26 03:39:17 -06:00
|
|
|
export type CameraPermissionStatus = 'granted' | 'not-determined' | 'denied' | 'restricted'
|
|
|
|
export type CameraPermissionRequestResult = 'granted' | 'denied'
|
2021-02-19 08:07:53 -07:00
|
|
|
|
|
|
|
interface OnErrorEvent {
|
2023-09-26 03:39:17 -06:00
|
|
|
code: string
|
|
|
|
message: string
|
|
|
|
cause?: ErrorWithCause
|
2021-02-19 08:07:53 -07:00
|
|
|
}
|
feat: Sync Frame Processors (plus `runAsync` and `runAtTargetFps`) (#1472)
Before, Frame Processors ran on a separate Thread.
After, Frame Processors run fully synchronous and always at the same FPS as the Camera.
Two new functions have been introduced:
* `runAtTargetFps(fps: number, func: () => void)`: Runs the given code as often as the given `fps`, effectively throttling it's calls.
* `runAsync(frame: Frame, func: () => void)`: Runs the given function on a separate Thread for Frame Processing. A strong reference to the Frame is held as long as the function takes to execute.
You can use `runAtTargetFps` to throttle calls to a specific API (e.g. if your Camera is running at 60 FPS, but you only want to run face detection at ~25 FPS, use `runAtTargetFps(25, ...)`.)
You can use `runAsync` to run a heavy algorithm asynchronous, so that the Camera is not blocked while your algorithm runs. This is useful if your main sync processor draws something, and your async processor is doing some image analysis on the side.
You can also combine both functions.
Examples:
```js
const frameProcessor = useFrameProcessor((frame) => {
'worklet'
console.log("I'm running at 60 FPS!")
}, [])
```
```js
const frameProcessor = useFrameProcessor((frame) => {
'worklet'
console.log("I'm running at 60 FPS!")
runAtTargetFps(10, () => {
'worklet'
console.log("I'm running at 10 FPS!")
})
}, [])
```
```js
const frameProcessor = useFrameProcessor((frame) => {
'worklet'
console.log("I'm running at 60 FPS!")
runAsync(frame, () => {
'worklet'
console.log("I'm running on another Thread, I can block for longer!")
})
}, [])
```
```js
const frameProcessor = useFrameProcessor((frame) => {
'worklet'
console.log("I'm running at 60 FPS!")
runAtTargetFps(10, () => {
'worklet'
runAsync(frame, () => {
'worklet'
console.log("I'm running on another Thread at 10 FPS, I can block for longer!")
})
})
}, [])
```
2023-02-15 08:47:09 -07:00
|
|
|
type NativeCameraViewProps = Omit<CameraProps, 'device' | 'onInitialized' | 'onError' | 'frameProcessor'> & {
|
2023-09-26 03:39:17 -06:00
|
|
|
cameraId: string
|
|
|
|
enableFrameProcessor: boolean
|
|
|
|
onInitialized?: (event: NativeSyntheticEvent<void>) => void
|
|
|
|
onError?: (event: NativeSyntheticEvent<OnErrorEvent>) => void
|
|
|
|
onViewReady: () => void
|
|
|
|
}
|
|
|
|
type RefType = React.Component<NativeCameraViewProps> & Readonly<NativeMethods>
|
2021-02-19 08:07:53 -07:00
|
|
|
//#endregion
|
|
|
|
|
2021-03-23 10:15:09 -06:00
|
|
|
//#region Camera Component
|
2021-02-19 08:07:53 -07:00
|
|
|
/**
|
|
|
|
* ### A powerful `<Camera>` component.
|
2021-03-03 04:37:43 -07:00
|
|
|
*
|
2023-03-06 03:03:40 -07:00
|
|
|
* Read the [VisionCamera documentation](https://react-native-vision-camera.com/) for more information.
|
2021-03-08 10:51:53 -07:00
|
|
|
*
|
2023-09-25 04:57:03 -06:00
|
|
|
* The `<Camera>` component's most important properties are:
|
2021-03-03 04:37:43 -07:00
|
|
|
*
|
2023-09-25 04:57:03 -06:00
|
|
|
* * {@linkcode CameraProps.device | device}: Specifies the {@linkcode CameraDevice} to use. Get a {@linkcode CameraDevice} by using the {@linkcode useCameraDevice | useCameraDevice(..)} hook, or manually by using the {@linkcode CameraDevices.getAvailableCameraDevices CameraDevices.getAvailableCameraDevices()} function.
|
2021-03-12 06:19:22 -07:00
|
|
|
* * {@linkcode CameraProps.isActive | isActive}: A boolean value that specifies whether the Camera should actively stream video frames or not. This can be compared to a Video component, where `isActive` specifies whether the video is paused or not. If you fully unmount the `<Camera>` component instead of using `isActive={false}`, the Camera will take a bit longer to start again.
|
2021-03-03 04:37:43 -07:00
|
|
|
*
|
|
|
|
* @example
|
2021-03-09 04:02:10 -07:00
|
|
|
* ```tsx
|
2021-03-03 04:37:43 -07:00
|
|
|
* function App() {
|
2023-09-25 04:57:03 -06:00
|
|
|
* const device = useCameraDevice('back')
|
2021-03-03 04:37:43 -07:00
|
|
|
*
|
2023-09-25 04:57:03 -06:00
|
|
|
* if (device == null) return <NoCameraErrorView />
|
2021-03-03 04:37:43 -07:00
|
|
|
* return (
|
|
|
|
* <Camera
|
|
|
|
* style={StyleSheet.absoluteFill}
|
|
|
|
* device={device}
|
|
|
|
* isActive={true}
|
|
|
|
* />
|
|
|
|
* )
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @component
|
2021-02-19 08:07:53 -07:00
|
|
|
*/
|
2021-05-06 08:48:02 -06:00
|
|
|
export class Camera extends React.PureComponent<CameraProps> {
|
2021-07-12 07:16:03 -06:00
|
|
|
/** @internal */
|
2023-09-26 03:39:17 -06:00
|
|
|
static displayName = 'Camera'
|
2021-07-12 07:16:03 -06:00
|
|
|
/** @internal */
|
2023-09-26 03:39:17 -06:00
|
|
|
displayName = Camera.displayName
|
|
|
|
private lastFrameProcessor: FrameProcessor | undefined
|
|
|
|
private isNativeViewMounted = false
|
2021-02-19 08:07:53 -07:00
|
|
|
|
2023-09-26 03:39:17 -06:00
|
|
|
private readonly ref: React.RefObject<RefType>
|
2021-02-19 08:07:53 -07:00
|
|
|
|
2021-07-12 07:16:03 -06:00
|
|
|
/** @internal */
|
2021-02-19 08:07:53 -07:00
|
|
|
constructor(props: CameraProps) {
|
2023-09-26 03:39:17 -06:00
|
|
|
super(props)
|
|
|
|
this.onViewReady = this.onViewReady.bind(this)
|
|
|
|
this.onInitialized = this.onInitialized.bind(this)
|
|
|
|
this.onError = this.onError.bind(this)
|
|
|
|
this.ref = React.createRef<RefType>()
|
|
|
|
this.lastFrameProcessor = undefined
|
2021-02-19 08:07:53 -07:00
|
|
|
}
|
|
|
|
|
2023-07-21 09:52:30 -06:00
|
|
|
private get handle(): number {
|
2023-09-26 03:39:17 -06:00
|
|
|
const nodeHandle = findNodeHandle(this.ref.current)
|
2021-09-27 06:35:35 -06:00
|
|
|
if (nodeHandle == null || nodeHandle === -1) {
|
|
|
|
throw new CameraRuntimeError(
|
|
|
|
'system/view-not-found',
|
|
|
|
"Could not get the Camera's native view tag! Does the Camera View exist in the native view-tree?",
|
2023-09-26 03:39:17 -06:00
|
|
|
)
|
2021-09-27 06:35:35 -06:00
|
|
|
}
|
2021-02-19 08:23:54 -07:00
|
|
|
|
2023-09-26 03:39:17 -06:00
|
|
|
return nodeHandle
|
2021-02-19 08:07:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//#region View-specific functions (UIViewManager)
|
|
|
|
/**
|
|
|
|
* Take a single photo and write it's content to a temporary file.
|
|
|
|
*
|
2021-03-08 10:51:53 -07:00
|
|
|
* @throws {@linkcode CameraCaptureError} When any kind of error occured while capturing the photo. Use the {@linkcode CameraCaptureError.code | code} property to get the actual error
|
2021-03-08 10:30:23 -07:00
|
|
|
* @example
|
2021-03-09 04:02:10 -07:00
|
|
|
* ```ts
|
2021-03-08 10:30:23 -07:00
|
|
|
* const photo = await camera.current.takePhoto({
|
|
|
|
* qualityPrioritization: 'quality',
|
|
|
|
* flash: 'on',
|
|
|
|
* enableAutoRedEyeReduction: true
|
|
|
|
* })
|
|
|
|
* ```
|
2021-02-19 08:07:53 -07:00
|
|
|
*/
|
|
|
|
public async takePhoto(options?: TakePhotoOptions): Promise<PhotoFile> {
|
|
|
|
try {
|
2023-09-26 03:39:17 -06:00
|
|
|
return await CameraModule.takePhoto(this.handle, options ?? {})
|
2021-02-19 08:07:53 -07:00
|
|
|
} catch (e) {
|
2023-09-26 03:39:17 -06:00
|
|
|
throw tryParseNativeCameraError(e)
|
2021-02-19 08:07:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-29 07:27:09 -06:00
|
|
|
private calculateBitRate(bitRate: 'low' | 'normal' | 'high', codec: 'h264' | 'h265' = 'h264'): number {
|
|
|
|
const format = this.props.format
|
|
|
|
if (format == null) {
|
|
|
|
throw new CameraRuntimeError(
|
|
|
|
'parameter/invalid-combination',
|
|
|
|
`A videoBitRate of '${bitRate}' can only be used in combination with a 'format'!`,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const factor = {
|
|
|
|
low: 0.8,
|
|
|
|
normal: 1,
|
|
|
|
high: 1.2,
|
|
|
|
}[bitRate]
|
|
|
|
let result = (30 / (3840 * 2160 * 0.75)) * (format.videoWidth * format.videoHeight)
|
|
|
|
// FPS - 30 is default, 60 would be 2x, 120 would be 4x
|
|
|
|
const fps = this.props.fps ?? Math.min(format.maxFps, 30)
|
|
|
|
result = (result / 30) * fps
|
|
|
|
// H.265 (HEVC) codec is 20% more efficient
|
|
|
|
if (codec === 'h265') result = result * 0.8
|
|
|
|
// HDR (10-bit) instead of SDR (8-bit) takes up 20% more pixels
|
|
|
|
if (this.props.hdr) result = result * 1.2
|
|
|
|
// Return overall result
|
|
|
|
return result * factor
|
|
|
|
}
|
|
|
|
|
2021-02-19 08:07:53 -07:00
|
|
|
/**
|
|
|
|
* Start a new video recording.
|
|
|
|
*
|
2021-03-08 10:51:53 -07:00
|
|
|
* @throws {@linkcode CameraCaptureError} When any kind of error occured while starting the video recording. Use the {@linkcode CameraCaptureError.code | code} property to get the actual error
|
2021-02-19 08:07:53 -07:00
|
|
|
*
|
|
|
|
* @example
|
2021-03-09 04:02:10 -07:00
|
|
|
* ```ts
|
2021-02-19 08:07:53 -07:00
|
|
|
* camera.current.startRecording({
|
|
|
|
* onRecordingFinished: (video) => console.log(video),
|
|
|
|
* onRecordingError: (error) => console.error(error),
|
|
|
|
* })
|
|
|
|
* setTimeout(() => {
|
|
|
|
* camera.current.stopRecording()
|
|
|
|
* }, 5000)
|
2021-03-03 04:37:43 -07:00
|
|
|
* ```
|
2021-02-19 08:07:53 -07:00
|
|
|
*/
|
|
|
|
public startRecording(options: RecordVideoOptions): void {
|
2023-09-26 03:39:17 -06:00
|
|
|
const { onRecordingError, onRecordingFinished, ...passThroughOptions } = options
|
2021-02-19 08:23:54 -07:00
|
|
|
if (typeof onRecordingError !== 'function' || typeof onRecordingFinished !== 'function')
|
2023-09-26 03:39:17 -06:00
|
|
|
throw new CameraRuntimeError('parameter/invalid-parameter', 'The onRecordingError or onRecordingFinished functions were not set!')
|
2021-02-19 08:23:54 -07:00
|
|
|
|
2023-09-29 07:27:09 -06:00
|
|
|
const videoBitRate = passThroughOptions.videoBitRate
|
|
|
|
if (typeof videoBitRate === 'string') passThroughOptions.videoBitRate = this.calculateBitRate(videoBitRate, options.videoCodec)
|
|
|
|
|
2021-02-19 08:23:54 -07:00
|
|
|
const onRecordCallback = (video?: VideoFile, error?: CameraCaptureError): void => {
|
2023-09-26 03:39:17 -06:00
|
|
|
if (error != null) return onRecordingError(error)
|
|
|
|
if (video != null) return onRecordingFinished(video)
|
|
|
|
}
|
2021-02-19 08:07:53 -07:00
|
|
|
try {
|
2023-09-21 03:20:33 -06:00
|
|
|
// TODO: Use TurboModules to make this awaitable.
|
2023-09-26 03:39:17 -06:00
|
|
|
CameraModule.startRecording(this.handle, passThroughOptions, onRecordCallback)
|
2021-02-19 08:07:53 -07:00
|
|
|
} catch (e) {
|
2023-09-26 03:39:17 -06:00
|
|
|
throw tryParseNativeCameraError(e)
|
2021-02-19 08:07:53 -07:00
|
|
|
}
|
|
|
|
}
|
2022-03-22 03:44:58 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pauses the current video recording.
|
|
|
|
*
|
|
|
|
* @throws {@linkcode CameraCaptureError} When any kind of error occured while pausing the video recording. Use the {@linkcode CameraCaptureError.code | code} property to get the actual error
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* // Start
|
|
|
|
* await camera.current.startRecording()
|
|
|
|
* await timeout(1000)
|
|
|
|
* // Pause
|
|
|
|
* await camera.current.pauseRecording()
|
|
|
|
* await timeout(500)
|
|
|
|
* // Resume
|
|
|
|
* await camera.current.resumeRecording()
|
|
|
|
* await timeout(2000)
|
|
|
|
* // Stop
|
|
|
|
* const video = await camera.current.stopRecording()
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
public async pauseRecording(): Promise<void> {
|
|
|
|
try {
|
2023-09-26 03:39:17 -06:00
|
|
|
return await CameraModule.pauseRecording(this.handle)
|
2022-03-22 03:44:58 -06:00
|
|
|
} catch (e) {
|
2023-09-26 03:39:17 -06:00
|
|
|
throw tryParseNativeCameraError(e)
|
2022-03-22 03:44:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resumes a currently paused video recording.
|
|
|
|
*
|
|
|
|
* @throws {@linkcode CameraCaptureError} When any kind of error occured while resuming the video recording. Use the {@linkcode CameraCaptureError.code | code} property to get the actual error
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* // Start
|
|
|
|
* await camera.current.startRecording()
|
|
|
|
* await timeout(1000)
|
|
|
|
* // Pause
|
|
|
|
* await camera.current.pauseRecording()
|
|
|
|
* await timeout(500)
|
|
|
|
* // Resume
|
|
|
|
* await camera.current.resumeRecording()
|
|
|
|
* await timeout(2000)
|
|
|
|
* // Stop
|
|
|
|
* const video = await camera.current.stopRecording()
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
public async resumeRecording(): Promise<void> {
|
|
|
|
try {
|
2023-09-26 03:39:17 -06:00
|
|
|
return await CameraModule.resumeRecording(this.handle)
|
2022-03-22 03:44:58 -06:00
|
|
|
} catch (e) {
|
2023-09-26 03:39:17 -06:00
|
|
|
throw tryParseNativeCameraError(e)
|
2022-03-22 03:44:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-19 08:07:53 -07:00
|
|
|
/**
|
|
|
|
* Stop the current video recording.
|
|
|
|
*
|
2021-03-08 10:51:53 -07:00
|
|
|
* @throws {@linkcode CameraCaptureError} When any kind of error occured while stopping the video recording. Use the {@linkcode CameraCaptureError.code | code} property to get the actual error
|
2021-03-08 10:16:45 -07:00
|
|
|
*
|
2021-02-19 08:07:53 -07:00
|
|
|
* @example
|
2021-03-09 04:02:10 -07:00
|
|
|
* ```ts
|
2021-02-19 08:07:53 -07:00
|
|
|
* await camera.current.startRecording()
|
|
|
|
* setTimeout(async () => {
|
|
|
|
* const video = await camera.current.stopRecording()
|
|
|
|
* }, 5000)
|
2021-03-03 04:37:43 -07:00
|
|
|
* ```
|
2021-02-19 08:07:53 -07:00
|
|
|
*/
|
|
|
|
public async stopRecording(): Promise<void> {
|
|
|
|
try {
|
2023-09-26 03:39:17 -06:00
|
|
|
return await CameraModule.stopRecording(this.handle)
|
2021-02-19 08:07:53 -07:00
|
|
|
} catch (e) {
|
2023-09-26 03:39:17 -06:00
|
|
|
throw tryParseNativeCameraError(e)
|
2021-02-19 08:07:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Focus the camera to a specific point in the coordinate system.
|
2023-09-21 03:20:33 -06:00
|
|
|
* @param {Point} point The point to focus to. This should be relative
|
|
|
|
* to the Camera view's coordinate system and is expressed in points.
|
2021-02-19 08:07:53 -07:00
|
|
|
* * `(0, 0)` means **top left**.
|
|
|
|
* * `(CameraView.width, CameraView.height)` means **bottom right**.
|
|
|
|
*
|
|
|
|
* Make sure the value doesn't exceed the CameraView's dimensions.
|
2021-03-08 10:16:45 -07:00
|
|
|
*
|
2021-03-08 10:51:53 -07:00
|
|
|
* @throws {@linkcode CameraRuntimeError} When any kind of error occured while focussing. Use the {@linkcode CameraRuntimeError.code | code} property to get the actual error
|
2021-03-08 10:30:23 -07:00
|
|
|
* @example
|
2021-03-09 04:02:10 -07:00
|
|
|
* ```ts
|
2021-03-08 10:30:23 -07:00
|
|
|
* await camera.current.focus({
|
|
|
|
* x: tapEvent.x,
|
|
|
|
* y: tapEvent.y
|
|
|
|
* })
|
|
|
|
* ```
|
2021-02-19 08:07:53 -07:00
|
|
|
*/
|
|
|
|
public async focus(point: Point): Promise<void> {
|
|
|
|
try {
|
2023-09-26 03:39:17 -06:00
|
|
|
return await CameraModule.focus(this.handle, point)
|
2021-02-19 08:07:53 -07:00
|
|
|
} catch (e) {
|
2023-09-26 03:39:17 -06:00
|
|
|
throw tryParseNativeCameraError(e)
|
2021-02-19 08:07:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
//#region Static Functions (NativeModule)
|
|
|
|
/**
|
|
|
|
* Get a list of all available camera devices on the current phone.
|
2021-03-08 10:16:45 -07:00
|
|
|
*
|
2023-09-25 04:57:03 -06:00
|
|
|
* If you use Hooks, use the `useCameraDevices(..)` hook instead.
|
2023-09-21 03:20:33 -06:00
|
|
|
*
|
|
|
|
* * For Camera Devices attached to the phone, it is safe to assume that this will never change.
|
|
|
|
* * For external Camera Devices (USB cameras, Mac continuity cameras, etc.) the available Camera Devices could change over time when the external Camera device gets plugged in or plugged out, so use {@link addCameraDevicesChangedListener | addCameraDevicesChangedListener(...)} to listen for such changes.
|
|
|
|
*
|
2021-03-08 10:30:23 -07:00
|
|
|
* @example
|
2021-03-09 04:02:10 -07:00
|
|
|
* ```ts
|
2023-09-21 03:20:33 -06:00
|
|
|
* const devices = Camera.getAvailableCameraDevices()
|
|
|
|
* const backCameras = devices.filter((d) => d.position === "back")
|
|
|
|
* const frontCameras = devices.filter((d) => d.position === "front")
|
2021-03-08 10:30:23 -07:00
|
|
|
* ```
|
2021-02-19 08:07:53 -07:00
|
|
|
*/
|
2023-09-21 03:20:33 -06:00
|
|
|
public static getAvailableCameraDevices(): CameraDevice[] {
|
2023-09-26 03:39:17 -06:00
|
|
|
return CameraDevices.getAvailableCameraDevices()
|
2023-09-21 03:20:33 -06:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Adds a listener that gets called everytime the Camera Devices change, for example
|
|
|
|
* when an external Camera Device (USB or continuity Camera) gets plugged in or plugged out.
|
|
|
|
*
|
|
|
|
* If you use Hooks, use the `useCameraDevices()` hook instead.
|
|
|
|
*/
|
|
|
|
public static addCameraDevicesChangedListener(listener: (newDevices: CameraDevice[]) => void): EmitterSubscription {
|
2023-09-26 03:39:17 -06:00
|
|
|
return CameraDevices.addCameraDevicesChangedListener(listener)
|
2021-02-19 08:07:53 -07:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Gets the current Camera Permission Status. Check this before mounting the Camera to ensure
|
|
|
|
* the user has permitted the app to use the camera.
|
|
|
|
*
|
2021-03-08 10:51:53 -07:00
|
|
|
* To actually prompt the user for camera permission, use {@linkcode Camera.requestCameraPermission | requestCameraPermission()}.
|
2021-03-08 10:16:45 -07:00
|
|
|
*
|
2021-03-08 10:51:53 -07:00
|
|
|
* @throws {@linkcode CameraRuntimeError} When any kind of error occured while getting the current permission status. Use the {@linkcode CameraRuntimeError.code | code} property to get the actual error
|
2021-02-19 08:07:53 -07:00
|
|
|
*/
|
|
|
|
public static async getCameraPermissionStatus(): Promise<CameraPermissionStatus> {
|
|
|
|
try {
|
2023-09-26 03:39:17 -06:00
|
|
|
return await CameraModule.getCameraPermissionStatus()
|
2021-02-19 08:07:53 -07:00
|
|
|
} catch (e) {
|
2023-09-26 03:39:17 -06:00
|
|
|
throw tryParseNativeCameraError(e)
|
2021-02-19 08:07:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Gets the current Microphone-Recording Permission Status. Check this before mounting the Camera to ensure
|
|
|
|
* the user has permitted the app to use the microphone.
|
|
|
|
*
|
2021-03-08 10:51:53 -07:00
|
|
|
* To actually prompt the user for microphone permission, use {@linkcode Camera.requestMicrophonePermission | requestMicrophonePermission()}.
|
2021-03-08 10:16:45 -07:00
|
|
|
*
|
2021-03-08 10:51:53 -07:00
|
|
|
* @throws {@linkcode CameraRuntimeError} When any kind of error occured while getting the current permission status. Use the {@linkcode CameraRuntimeError.code | code} property to get the actual error
|
2021-02-19 08:07:53 -07:00
|
|
|
*/
|
|
|
|
public static async getMicrophonePermissionStatus(): Promise<CameraPermissionStatus> {
|
|
|
|
try {
|
2023-09-26 03:39:17 -06:00
|
|
|
return await CameraModule.getMicrophonePermissionStatus()
|
2021-02-19 08:07:53 -07:00
|
|
|
} catch (e) {
|
2023-09-26 03:39:17 -06:00
|
|
|
throw tryParseNativeCameraError(e)
|
2021-02-19 08:07:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Shows a "request permission" alert to the user, and resolves with the new camera permission status.
|
|
|
|
*
|
|
|
|
* If the user has previously blocked the app from using the camera, the alert will not be shown
|
|
|
|
* and `"denied"` will be returned.
|
2021-03-08 10:16:45 -07:00
|
|
|
*
|
2021-03-08 10:51:53 -07:00
|
|
|
* @throws {@linkcode CameraRuntimeError} When any kind of error occured while requesting permission. Use the {@linkcode CameraRuntimeError.code | code} property to get the actual error
|
2021-02-19 08:07:53 -07:00
|
|
|
*/
|
|
|
|
public static async requestCameraPermission(): Promise<CameraPermissionRequestResult> {
|
|
|
|
try {
|
2023-09-26 03:39:17 -06:00
|
|
|
return await CameraModule.requestCameraPermission()
|
2021-02-19 08:07:53 -07:00
|
|
|
} catch (e) {
|
2023-09-26 03:39:17 -06:00
|
|
|
throw tryParseNativeCameraError(e)
|
2021-02-19 08:07:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Shows a "request permission" alert to the user, and resolves with the new microphone permission status.
|
|
|
|
*
|
|
|
|
* If the user has previously blocked the app from using the microphone, the alert will not be shown
|
|
|
|
* and `"denied"` will be returned.
|
2021-03-08 10:16:45 -07:00
|
|
|
*
|
2021-03-08 10:51:53 -07:00
|
|
|
* @throws {@linkcode CameraRuntimeError} When any kind of error occured while requesting permission. Use the {@linkcode CameraRuntimeError.code | code} property to get the actual error
|
2021-02-19 08:07:53 -07:00
|
|
|
*/
|
|
|
|
public static async requestMicrophonePermission(): Promise<CameraPermissionRequestResult> {
|
|
|
|
try {
|
2023-09-26 03:39:17 -06:00
|
|
|
return await CameraModule.requestMicrophonePermission()
|
2021-02-19 08:07:53 -07:00
|
|
|
} catch (e) {
|
2023-09-26 03:39:17 -06:00
|
|
|
throw tryParseNativeCameraError(e)
|
2021-02-19 08:07:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
//#region Events (Wrapped to maintain reference equality)
|
2021-03-12 05:21:46 -07:00
|
|
|
private onError(event: NativeSyntheticEvent<OnErrorEvent>): void {
|
2021-02-19 08:07:53 -07:00
|
|
|
if (this.props.onError != null) {
|
2023-09-26 03:39:17 -06:00
|
|
|
const error = event.nativeEvent
|
|
|
|
const cause = isErrorWithCause(error.cause) ? error.cause : undefined
|
2021-02-19 08:07:53 -07:00
|
|
|
this.props.onError(
|
|
|
|
// @ts-expect-error We're casting from unknown bridge types to TS unions, I expect it to hopefully work
|
2021-02-19 08:23:54 -07:00
|
|
|
new CameraRuntimeError(error.code, error.message, cause),
|
2023-09-26 03:39:17 -06:00
|
|
|
)
|
2021-02-19 08:07:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-19 08:23:54 -07:00
|
|
|
private onInitialized(): void {
|
2023-09-26 03:39:17 -06:00
|
|
|
this.props.onInitialized?.()
|
2021-02-19 08:07:53 -07:00
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
2021-06-28 10:18:35 -06:00
|
|
|
//#region Lifecycle
|
2023-07-20 07:30:04 -06:00
|
|
|
private setFrameProcessor(frameProcessor: FrameProcessor): void {
|
2023-09-26 03:39:17 -06:00
|
|
|
VisionCameraProxy.setFrameProcessor(this.handle, frameProcessor)
|
2021-06-28 10:18:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
private unsetFrameProcessor(): void {
|
2023-09-26 03:39:17 -06:00
|
|
|
VisionCameraProxy.removeFrameProcessor(this.handle)
|
2021-06-28 10:18:35 -06:00
|
|
|
}
|
|
|
|
|
2021-10-11 10:27:23 -06:00
|
|
|
private onViewReady(): void {
|
2023-09-26 03:39:17 -06:00
|
|
|
this.isNativeViewMounted = true
|
2021-10-11 10:27:23 -06:00
|
|
|
if (this.props.frameProcessor != null) {
|
|
|
|
// user passed a `frameProcessor` but we didn't set it yet because the native view was not mounted yet. set it now.
|
2023-09-26 03:39:17 -06:00
|
|
|
this.setFrameProcessor(this.props.frameProcessor)
|
|
|
|
this.lastFrameProcessor = this.props.frameProcessor
|
2021-10-11 10:27:23 -06:00
|
|
|
}
|
2021-05-06 06:11:55 -06:00
|
|
|
}
|
|
|
|
|
2021-07-12 07:16:03 -06:00
|
|
|
/** @internal */
|
2021-05-06 06:11:55 -06:00
|
|
|
componentDidUpdate(): void {
|
2023-09-26 03:39:17 -06:00
|
|
|
if (!this.isNativeViewMounted) return
|
|
|
|
const frameProcessor = this.props.frameProcessor
|
2021-07-12 07:16:03 -06:00
|
|
|
if (frameProcessor !== this.lastFrameProcessor) {
|
2021-06-28 10:18:35 -06:00
|
|
|
// frameProcessor argument identity changed. Update native to reflect the change.
|
2023-09-26 03:39:17 -06:00
|
|
|
if (frameProcessor != null) this.setFrameProcessor(frameProcessor)
|
|
|
|
else this.unsetFrameProcessor()
|
2021-06-28 10:18:35 -06:00
|
|
|
|
2023-09-26 03:39:17 -06:00
|
|
|
this.lastFrameProcessor = frameProcessor
|
2021-07-12 07:16:03 -06:00
|
|
|
}
|
|
|
|
}
|
2021-06-28 10:18:35 -06:00
|
|
|
//#endregion
|
2021-03-12 05:21:46 -07:00
|
|
|
|
2021-07-12 07:16:03 -06:00
|
|
|
/** @internal */
|
2021-05-06 06:11:55 -06:00
|
|
|
public render(): React.ReactNode {
|
2021-02-19 08:07:53 -07:00
|
|
|
// We remove the big `device` object from the props because we only need to pass `cameraId` to native.
|
2023-09-26 03:39:17 -06:00
|
|
|
const { device, frameProcessor, ...props } = this.props
|
2023-02-23 06:57:57 -07:00
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
|
|
if (device == null) {
|
|
|
|
throw new Error(
|
|
|
|
'Camera: `device` is null! Select a valid Camera device. See: https://mrousavy.com/react-native-vision-camera/docs/guides/devices',
|
2023-09-26 03:39:17 -06:00
|
|
|
)
|
2023-02-23 06:57:57 -07:00
|
|
|
}
|
|
|
|
|
2023-09-26 03:39:17 -06:00
|
|
|
const shouldEnableBufferCompression = props.video === true && frameProcessor == null
|
2023-09-21 09:18:54 -06:00
|
|
|
|
2021-06-27 04:37:54 -06:00
|
|
|
return (
|
|
|
|
<NativeCameraView
|
|
|
|
{...props}
|
|
|
|
cameraId={device.id}
|
|
|
|
ref={this.ref}
|
2021-10-11 10:27:23 -06:00
|
|
|
onViewReady={this.onViewReady}
|
2021-06-27 04:37:54 -06:00
|
|
|
onInitialized={this.onInitialized}
|
|
|
|
onError={this.onError}
|
2021-07-12 07:16:03 -06:00
|
|
|
enableFrameProcessor={frameProcessor != null}
|
2023-09-21 09:18:54 -06:00
|
|
|
enableBufferCompression={props.enableBufferCompression ?? shouldEnableBufferCompression}
|
2021-06-27 04:37:54 -06:00
|
|
|
/>
|
2023-09-26 03:39:17 -06:00
|
|
|
)
|
2021-02-19 08:07:53 -07:00
|
|
|
}
|
|
|
|
}
|
2021-03-23 10:15:09 -06:00
|
|
|
//#endregion
|
2021-02-19 08:07:53 -07:00
|
|
|
|
|
|
|
// requireNativeComponent automatically resolves 'CameraView' to 'CameraViewManager'
|
2021-03-12 05:21:46 -07:00
|
|
|
const NativeCameraView = requireNativeComponent<NativeCameraViewProps>(
|
2021-02-19 08:23:54 -07:00
|
|
|
'CameraView',
|
2021-02-19 08:07:53 -07:00
|
|
|
// @ts-expect-error because the type declarations are kinda wrong, no?
|
2021-02-19 08:23:54 -07:00
|
|
|
Camera,
|
2023-09-26 03:39:17 -06:00
|
|
|
)
|