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'
|
2023-11-09 03:57:05 -07:00
|
|
|
import { Code, CodeScanner, CodeScannerFrame } from './CodeScanner'
|
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
|
|
|
|
2023-10-04 04:53:52 -06:00
|
|
|
interface OnCodeScannedEvent {
|
|
|
|
codes: Code[]
|
2023-11-09 03:57:05 -07:00
|
|
|
frame: CodeScannerFrame
|
2023-10-04 04:53:52 -06:00
|
|
|
}
|
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
|
|
|
}
|
2023-10-04 04:53:52 -06:00
|
|
|
type NativeCameraViewProps = Omit<CameraProps, 'device' | 'onInitialized' | 'onError' | 'frameProcessor' | 'codeScanner'> & {
|
2023-09-26 03:39:17 -06:00
|
|
|
cameraId: string
|
|
|
|
enableFrameProcessor: boolean
|
2023-10-04 04:53:52 -06:00
|
|
|
codeScannerOptions?: Omit<CodeScanner, 'onCodeScanned'>
|
2023-09-26 03:39:17 -06:00
|
|
|
onInitialized?: (event: NativeSyntheticEvent<void>) => void
|
|
|
|
onError?: (event: NativeSyntheticEvent<OnErrorEvent>) => void
|
2023-10-04 04:53:52 -06:00
|
|
|
onCodeScanned?: (event: NativeSyntheticEvent<OnCodeScannedEvent>) => void
|
2023-12-09 11:09:55 -07:00
|
|
|
onStarted?: (event: NativeSyntheticEvent<void>) => void
|
|
|
|
onStopped?: (event: NativeSyntheticEvent<void>) => void
|
2023-09-26 03:39:17 -06:00
|
|
|
onViewReady: () => void
|
|
|
|
}
|
2023-11-27 09:20:26 -07:00
|
|
|
type NativeRecordVideoOptions = Omit<RecordVideoOptions, 'onRecordingError' | 'onRecordingFinished' | 'videoBitRate'> & {
|
|
|
|
videoBitRateOverride?: number
|
|
|
|
videoBitRateMultiplier?: number
|
|
|
|
}
|
2023-09-26 03:39:17 -06:00
|
|
|
type RefType = React.Component<NativeCameraViewProps> & Readonly<NativeMethods>
|
2023-12-04 03:43:17 -07:00
|
|
|
interface CameraState {
|
|
|
|
isRecordingWithFlash: boolean
|
|
|
|
}
|
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
|
|
|
*/
|
2023-12-04 03:43:17 -07:00
|
|
|
export class Camera extends React.PureComponent<CameraProps, CameraState> {
|
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)
|
2023-12-09 11:09:55 -07:00
|
|
|
this.onStarted = this.onStarted.bind(this)
|
|
|
|
this.onStopped = this.onStopped.bind(this)
|
2023-09-26 03:39:17 -06:00
|
|
|
this.onError = this.onError.bind(this)
|
2023-10-04 04:53:52 -06:00
|
|
|
this.onCodeScanned = this.onCodeScanned.bind(this)
|
2023-09-26 03:39:17 -06:00
|
|
|
this.ref = React.createRef<RefType>()
|
|
|
|
this.lastFrameProcessor = undefined
|
2023-12-04 03:43:17 -07:00
|
|
|
this.state = {
|
|
|
|
isRecordingWithFlash: false,
|
|
|
|
}
|
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-11-27 09:20:26 -07:00
|
|
|
private getBitRateMultiplier(bitRate: RecordVideoOptions['videoBitRate']): number {
|
2023-11-27 09:29:25 -07:00
|
|
|
if (typeof bitRate === 'number' || bitRate == null) return 1
|
2023-11-27 09:20:26 -07:00
|
|
|
switch (bitRate) {
|
2023-11-27 09:29:25 -07:00
|
|
|
case 'extra-low':
|
|
|
|
return 0.6
|
2023-11-27 09:20:26 -07:00
|
|
|
case 'low':
|
|
|
|
return 0.8
|
2023-11-27 09:29:25 -07:00
|
|
|
case 'normal':
|
|
|
|
return 1
|
2023-11-27 09:20:26 -07:00
|
|
|
case 'high':
|
|
|
|
return 1.2
|
2023-11-27 09:29:25 -07:00
|
|
|
case 'extra-high':
|
|
|
|
return 1.4
|
2023-09-29 07:27:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-27 09:20:26 -07:00
|
|
|
const { onRecordingError, onRecordingFinished, videoBitRate, ...passThruOptions } = 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-12-04 03:43:17 -07:00
|
|
|
if (options.flash === 'on') {
|
|
|
|
// Enable torch for video recording
|
|
|
|
this.setState({
|
|
|
|
isRecordingWithFlash: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-11-27 09:20:26 -07:00
|
|
|
const nativeOptions: NativeRecordVideoOptions = passThruOptions
|
2023-11-27 09:29:25 -07:00
|
|
|
if (typeof videoBitRate === 'number') {
|
2023-11-27 09:20:26 -07:00
|
|
|
// If the user passed an absolute number as a bit-rate, we just use this as a full override.
|
|
|
|
nativeOptions.videoBitRateOverride = videoBitRate
|
2023-11-27 09:29:25 -07:00
|
|
|
} else if (typeof videoBitRate === 'string' && videoBitRate !== 'normal') {
|
|
|
|
// If the user passed 'low'/'normal'/'high', we need to apply this as a multiplier to the native bitrate instead of absolutely setting it
|
|
|
|
nativeOptions.videoBitRateMultiplier = this.getBitRateMultiplier(videoBitRate)
|
2023-11-27 09:20:26 -07:00
|
|
|
}
|
2023-09-29 07:27:09 -06:00
|
|
|
|
2021-02-19 08:23:54 -07:00
|
|
|
const onRecordCallback = (video?: VideoFile, error?: CameraCaptureError): void => {
|
2023-12-04 03:43:17 -07:00
|
|
|
if (this.state.isRecordingWithFlash) {
|
|
|
|
// disable torch again if it was enabled
|
|
|
|
this.setState({
|
|
|
|
isRecordingWithFlash: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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-11-27 09:20:26 -07:00
|
|
|
CameraModule.startRecording(this.handle, nativeOptions, 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-02-19 08:07:53 -07:00
|
|
|
*/
|
2023-12-19 06:22:04 -07:00
|
|
|
public static getCameraPermissionStatus(): CameraPermissionStatus {
|
|
|
|
return CameraModule.getCameraPermissionStatus()
|
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-02-19 08:07:53 -07:00
|
|
|
*/
|
2023-12-19 06:22:04 -07:00
|
|
|
public static getMicrophonePermissionStatus(): CameraPermissionStatus {
|
|
|
|
return CameraModule.getMicrophonePermissionStatus()
|
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 {
|
2023-11-18 04:16:52 -07:00
|
|
|
const error = event.nativeEvent
|
|
|
|
const cause = isErrorWithCause(error.cause) ? error.cause : undefined
|
|
|
|
// @ts-expect-error We're casting from unknown bridge types to TS unions, I expect it to hopefully work
|
|
|
|
const cameraError = new CameraRuntimeError(error.code, error.message, cause)
|
|
|
|
|
2021-02-19 08:07:53 -07:00
|
|
|
if (this.props.onError != null) {
|
2023-11-18 04:16:52 -07:00
|
|
|
this.props.onError(cameraError)
|
|
|
|
} else {
|
|
|
|
// User didn't pass an `onError` handler, so just log it to console
|
|
|
|
console.error(`Camera.onError(${cameraError.code}): ${cameraError.message}`, cameraError)
|
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
|
|
|
}
|
2023-12-09 11:09:55 -07:00
|
|
|
|
|
|
|
private onStarted(): void {
|
|
|
|
this.props.onStarted?.()
|
|
|
|
}
|
|
|
|
|
|
|
|
private onStopped(): void {
|
|
|
|
this.props.onStopped?.()
|
|
|
|
}
|
2021-02-19 08:07:53 -07:00
|
|
|
//#endregion
|
|
|
|
|
2023-10-04 04:53:52 -06:00
|
|
|
private onCodeScanned(event: NativeSyntheticEvent<OnCodeScannedEvent>): void {
|
|
|
|
const codeScanner = this.props.codeScanner
|
|
|
|
if (codeScanner == null) return
|
|
|
|
|
2023-11-09 03:57:05 -07:00
|
|
|
codeScanner.onCodeScanned(event.nativeEvent.codes, event.nativeEvent.frame)
|
2023-10-04 04:53:52 -06:00
|
|
|
}
|
|
|
|
|
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-10-04 04:53:52 -06:00
|
|
|
const { device, frameProcessor, codeScanner, ...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-12-04 03:43:17 -07:00
|
|
|
const torch = this.state.isRecordingWithFlash ? 'on' : props.torch
|
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}
|
2023-12-04 03:43:17 -07:00
|
|
|
torch={torch}
|
2021-10-11 10:27:23 -06:00
|
|
|
onViewReady={this.onViewReady}
|
2021-06-27 04:37:54 -06:00
|
|
|
onInitialized={this.onInitialized}
|
2023-10-04 04:53:52 -06:00
|
|
|
onCodeScanned={this.onCodeScanned}
|
2023-12-09 11:09:55 -07:00
|
|
|
onStarted={this.onStarted}
|
|
|
|
onStopped={this.onStopped}
|
2021-06-27 04:37:54 -06:00
|
|
|
onError={this.onError}
|
2023-10-04 04:53:52 -06:00
|
|
|
codeScannerOptions={codeScanner}
|
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
|
|
|
)
|