From 41b780352fa2bd81a10e44e2cbca4c2155fb84c4 Mon Sep 17 00:00:00 2001 From: Nitya Santosh Date: Wed, 6 Dec 2023 10:01:42 +0530 Subject: [PATCH 1/2] Implement stop() to close CAMERA or PHOTOLIBRARY --- src/android/CameraLauncher.java | 13 +++++++++++++ src/ios/CDVCamera.h | 1 + src/ios/CDVCamera.m | 10 ++++++++++ www/Camera.js | 7 +++++++ 4 files changed, 31 insertions(+) diff --git a/src/android/CameraLauncher.java b/src/android/CameraLauncher.java index ef730d643..b91e358f5 100644 --- a/src/android/CameraLauncher.java +++ b/src/android/CameraLauncher.java @@ -213,6 +213,9 @@ else if ((this.srcType == PHOTOLIBRARY) || (this.srcType == SAVEDPHOTOALBUM)) { r.setKeepCallback(true); callbackContext.sendPluginResult(r); + return true; + } if (action.equals("stop")) { + this.stopCamera(); return true; } return false; @@ -352,6 +355,16 @@ public void takePicture(int returnType, int encodingType) // LOG.d(LOG_TAG, "ERROR: You must use the CordovaInterface for this to work correctly. Please implement it in your activity"); } + public void stopCamera() { + LOG.d(LOG_TAG,"Stopping Camera"); + try { + this.cordova.getActivity().finishActivity((CAMERA + 1) * 16 + returnType + 1); + this.cordova.getActivity().finishActivity((PHOTOLIBRARY + 1) * 16 + returnType + 1); + } catch(Exception e) { + LOG.e(LOG_TAG, "Error in closing Camera/Picker"); + } + } + /** * Create a file in the applications temporary directory based upon the supplied encoding. * diff --git a/src/ios/CDVCamera.h b/src/ios/CDVCamera.h index 647ffab19..046b6aed5 100644 --- a/src/ios/CDVCamera.h +++ b/src/ios/CDVCamera.h @@ -102,6 +102,7 @@ typedef NSUInteger CDVMediaType; */ - (void)takePicture:(CDVInvokedUrlCommand*)command; - (void)cleanup:(CDVInvokedUrlCommand*)command; +- (void)stop:(CDVInvokedUrlCommand*)command; - (void)repositionPopover:(CDVInvokedUrlCommand*)command; - (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info; diff --git a/src/ios/CDVCamera.m b/src/ios/CDVCamera.m index 6793da45b..e0f59d311 100644 --- a/src/ios/CDVCamera.m +++ b/src/ios/CDVCamera.m @@ -205,6 +205,16 @@ - (void)takePicture:(CDVInvokedUrlCommand*)command }]; } +- (void)stop:(CDVInvokedUrlCommand*)command { + if (self.pickerController) { + NSLog(@"Closing Camera/Photo Library"); + [self.pickerController dismissViewControllerAnimated:YES completion:^{ + self.hasPendingOperation = NO; + self.pickerController = nil; + }]; + } +} + - (void)showCameraPicker:(NSString*)callbackId withOptions:(CDVPictureOptions *) pictureOptions { // Perform UI operations on the main thread diff --git a/www/Camera.js b/www/Camera.js index 5c0627a33..a00f2cf7b 100644 --- a/www/Camera.js +++ b/www/Camera.js @@ -151,6 +151,13 @@ cameraExport.getPicture = function (successCallback, errorCallback, options) { // return new CameraPopoverHandle(); }; +/** +* Closes the Camera on calling this method +*/ +cameraExport.stop = function (successCallback, errorCallback) { + exec(successCallback, errorCallback, 'Camera', 'stop', []); +}; + /** * Removes intermediate image files that are kept in temporary storage * after calling [`camera.getPicture`]{@link module:camera.getPicture}. Applies only when the value of From 108001f04a225fcf6c68f05b6e7a82d17f764498 Mon Sep 17 00:00:00 2001 From: Nitya Santosh Date: Mon, 11 Dec 2023 11:37:25 +0530 Subject: [PATCH 2/2] updated stop() implementation --- src/android/CameraLauncher.java | 4 ++-- www/Camera.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/android/CameraLauncher.java b/src/android/CameraLauncher.java index b91e358f5..a6a2d0ed1 100644 --- a/src/android/CameraLauncher.java +++ b/src/android/CameraLauncher.java @@ -215,7 +215,7 @@ else if ((this.srcType == PHOTOLIBRARY) || (this.srcType == SAVEDPHOTOALBUM)) { return true; } if (action.equals("stop")) { - this.stopCamera(); + this.stopCamera(destType); return true; } return false; @@ -355,7 +355,7 @@ public void takePicture(int returnType, int encodingType) // LOG.d(LOG_TAG, "ERROR: You must use the CordovaInterface for this to work correctly. Please implement it in your activity"); } - public void stopCamera() { + public void stopCamera(int returnType) { LOG.d(LOG_TAG,"Stopping Camera"); try { this.cordova.getActivity().finishActivity((CAMERA + 1) * 16 + returnType + 1); diff --git a/www/Camera.js b/www/Camera.js index a00f2cf7b..7fd37b601 100644 --- a/www/Camera.js +++ b/www/Camera.js @@ -155,7 +155,7 @@ cameraExport.getPicture = function (successCallback, errorCallback, options) { * Closes the Camera on calling this method */ cameraExport.stop = function (successCallback, errorCallback) { - exec(successCallback, errorCallback, 'Camera', 'stop', []); + exec(successCallback, errorCallback, 'Camera', 'stop', []); }; /**