Added onError for when Android fails to initialize the camera

May fail when phone is broken or
if there are no camera with the specs (e.g. aspect ratio)
This commit is contained in:
Seph Soliman 2023-07-31 18:59:06 -07:00
parent c38594754b
commit 73bd920ca4
3 changed files with 12 additions and 0 deletions

View File

@ -121,6 +121,8 @@ Additionally, the Camera can be used for barcode scanning
| `torchMode` | `'on'`/`'off'` | Toggle flash light when camera is active. Default: `off` |
| `cameraType` | CameraType.Back/CameraType.Front | Choose what camera to use. Default: `CameraType.Back` |
| `onOrientationChange` | Function | Callback when physical device orientation changes. Returned event contains `orientation`. Ex: `onOrientationChange={(event) => console.log(event.nativeEvent.orientation)}`. Use `import { Orientation } from 'react-native-camera-kit'; if (event.nativeEvent.orientation === Orientation.PORTRAIT) { ... }` to understand the new value |
| **Android only** |
| `onError` | Function | Android only. Callback when camera fails to initialize. Ex: `onError={(e) => console.log(e.nativeEvent.errorMessage)}`. |
| **iOS only** |
| `ratioOverlay` | `'int:int'` | Show a guiding overlay in the camera preview for the selected ratio. Does not crop image as of v9.0. Example: `'16:9'` |
| `ratioOverlayColor` | Color | Any color with alpha. Default: `'#ffffff77'` |

View File

@ -326,6 +326,14 @@ class CKCamera(context: ThemedReactContext) : FrameLayout(context), LifecycleObs
preview?.setSurfaceProvider(viewFinder.surfaceProvider)
} catch (exc: Exception) {
Log.e(TAG, "Use case binding failed", exc)
val event: WritableMap = Arguments.createMap()
event.putString("errorMessage", exc.message)
currentContext.getJSModule(RCTEventEmitter::class.java).receiveEvent(
id,
"onError",
event
)
}
}

2
src/Camera.d.ts vendored
View File

@ -82,6 +82,8 @@ export interface CameraProps {
* ```
*/
onZoom?: (event: OnZoom) => void;
/** **Android only**. Triggered when camera fails to initialize */
onError?: (event: { nativeEvent: { errorMessage: number } }) => void;
// Barcode only
scanBarcode?: boolean;
showFrame?: boolean;