diff --git a/README.md b/README.md index e4e7d09ab..f780c5232 100644 --- a/README.md +++ b/README.md @@ -346,6 +346,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 10b59f9c5..108927312 100644 --- a/android/src/main/java/com/lwansbrough/RCTCamera/RCTCameraModule.java +++ b/android/src/main/java/com/lwansbrough/RCTCamera/RCTCameraModule.java @@ -658,6 +658,18 @@ public void stopCapture(final Promise promise) { } } + @ReactMethod + public void stopPreview(ReadableMap options) { + Camera camera = RCTCamera.getInstance().acquireCameraInstance(options.getInt("type")); + camera.stopPreview(); + } + + @ReactMethod + public void startPreview(ReadableMap options) { + Camera camera = RCTCamera.getInstance().acquireCameraInstance(options.getInt("type")); + 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 f1957e9e1..8e1d059b4 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 231008055..ea6d3555a 100644 --- a/ios/RCTCameraManager.m +++ b/ios/RCTCameraManager.m @@ -374,6 +374,28 @@ - (id)init { } } +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];