From 73bd920ca46bdaa16c8254820728c50c900c682e Mon Sep 17 00:00:00 2001 From: Seph Soliman Date: Mon, 31 Jul 2023 18:59:06 -0700 Subject: [PATCH] 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) --- README.md | 2 ++ android/src/main/java/com/rncamerakit/CKCamera.kt | 8 ++++++++ src/Camera.d.ts | 2 ++ 3 files changed, 12 insertions(+) diff --git a/README.md b/README.md index e8d0bf68c..61114a64e 100644 --- a/README.md +++ b/README.md @@ -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'` | diff --git a/android/src/main/java/com/rncamerakit/CKCamera.kt b/android/src/main/java/com/rncamerakit/CKCamera.kt index b3a2c3c65..6238473f1 100644 --- a/android/src/main/java/com/rncamerakit/CKCamera.kt +++ b/android/src/main/java/com/rncamerakit/CKCamera.kt @@ -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 + ) } } diff --git a/src/Camera.d.ts b/src/Camera.d.ts index 62b041009..a5de7d9cf 100644 --- a/src/Camera.d.ts +++ b/src/Camera.d.ts @@ -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;