diff --git a/README.md b/README.md index 20de34e..6a689e3 100644 --- a/README.md +++ b/README.md @@ -375,6 +375,14 @@ Returns whether or not the camera has flash capabilities. Ends the current capture session for video captures. Only applies when the current `captureMode` is `video`. +#### `stopPreview()` + +Stops the camera preview from running, and natively will make the current capture session pause. + +#### `startPreview()` + +Starts the camera preview again if previously stopped. + ## Component static methods #### `iOS` `Camera.checkDeviceAuthorizationStatus(): Promise` diff --git a/android/src/main/java/com/lwansbrough/RCTCamera/RCTCameraModule.java b/android/src/main/java/com/lwansbrough/RCTCamera/RCTCameraModule.java index 13b797a..6f79201 100644 --- a/android/src/main/java/com/lwansbrough/RCTCamera/RCTCameraModule.java +++ b/android/src/main/java/com/lwansbrough/RCTCamera/RCTCameraModule.java @@ -659,6 +659,28 @@ public class RCTCameraModule extends ReactContextBaseJavaModule } } + @ReactMethod + public void stopPreview(ReadableMap options) { + RCTCamera instance = RCTCamera.getInstance(); + if (instance == null) return; + + Camera camera = instance.acquireCameraInstance(options.getInt("type")); + if (camera == null) return; + + camera.stopPreview(); + } + + @ReactMethod + public void startPreview(ReadableMap options) { + RCTCamera instance = RCTCamera.getInstance(); + if (instance == null) return; + + Camera camera = instance.acquireCameraInstance(options.getInt("type")); + if (camera == null) return; + + camera.startPreview(); + } + @ReactMethod public void hasFlash(ReadableMap options, final Promise promise) { Camera camera = RCTCamera.getInstance().acquireCameraInstance(options.getInt("type")); diff --git a/index.js b/index.js index f1957e9..8e1d059 100644 --- a/index.js +++ b/index.js @@ -236,6 +236,28 @@ export default class Camera extends Component { return CameraManager.capture(options); } + startPreview() { + if (Platform.OS === 'android') { + const props = convertNativeProps(this.props); + CameraManager.startPreview({ + type: props.type + }); + } else { + CameraManager.startPreview(); + } + } + + stopPreview() { + if (Platform.OS === 'android') { + const props = convertNativeProps(this.props); + CameraManager.stopPreview({ + type: props.type + }); + } else { + CameraManager.stopPreview(); + } + } + stopCapture() { if (this.state.isRecording) { this.setState({ isRecording: false }); diff --git a/ios/RCTCameraManager.m b/ios/RCTCameraManager.m index 600d2bc..b68f295 100644 --- a/ios/RCTCameraManager.m +++ b/ios/RCTCameraManager.m @@ -379,6 +379,28 @@ RCT_EXPORT_METHOD(capture:(NSDictionary *)options } } +RCT_EXPORT_METHOD(stopPreview) { +#if TARGET_IPHONE_SIMULATOR + return; +#endif + dispatch_async(self.sessionQueue, ^{ + if ([self.session isRunning]) { + [self.session stopRunning]; + } + }); +} + +RCT_EXPORT_METHOD(startPreview) { +#if TARGET_IPHONE_SIMULATOR + return; +#endif + dispatch_async(self.sessionQueue, ^{ + if (![self.session isRunning]) { + [self.session startRunning]; + } + }); +} + RCT_EXPORT_METHOD(stopCapture) { if (self.movieFileOutput.recording) { [self.movieFileOutput stopRecording];