Skip to content

Commit

Permalink
Added onError for when Android fails to initialize the camera
Browse files Browse the repository at this point in the history
May fail when phone is broken or
if there are no camera with the specs (e.g. aspect ratio)
  • Loading branch information
scarlac committed Aug 1, 2023
1 parent c385947 commit 73bd920
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -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'` |
Expand Down
8 changes: 8 additions & 0 deletions android/src/main/java/com/rncamerakit/CKCamera.kt
Expand Up @@ -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
)
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/Camera.d.ts
Expand Up @@ -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;
Expand Down

0 comments on commit 73bd920

Please sign in to comment.