From 672958eb211ed06043f405d38bbd3ab5cb92dc15 Mon Sep 17 00:00:00 2001 From: Hazem Saleh Date: Thu, 9 May 2019 17:57:28 -0400 Subject: [PATCH] =?UTF-8?q?GH-420=C2=A0(all)=20DATA=5FURL=20is=20improperl?= =?UTF-8?q?y=20prefixed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- appium-tests/helpers/cameraHelper.js | 4 +--- src/android/CameraLauncher.java | 27 ++++++++++++++++++--------- src/browser/CameraProxy.js | 5 +---- src/ios/CDVCamera.m | 10 ++++++++-- src/osx/CDVCamera.m | 7 +++++-- src/windows/CameraProxy.js | 5 +---- 6 files changed, 34 insertions(+), 24 deletions(-) diff --git a/appium-tests/helpers/cameraHelper.js b/appium-tests/helpers/cameraHelper.js index 72f7a2700..8e7ba9f62 100644 --- a/appium-tests/helpers/cameraHelper.js +++ b/appium-tests/helpers/cameraHelper.js @@ -158,9 +158,7 @@ module.exports.checkPicture = function (pid, options, skipContentCheck, cb) { } try { - window.atob(result); - // if we got here it is a base64 string (DATA_URL) - result = "data:" + mimeType + ";base64," + result; + window.atob(result.substring(result.indexOf(",") + 1)); } catch (e) { // not DATA_URL if (options.destinationType === Camera.DestinationType.DATA_URL) { diff --git a/src/android/CameraLauncher.java b/src/android/CameraLauncher.java index 6cdf0c5f7..85238cde1 100644 --- a/src/android/CameraLauncher.java +++ b/src/android/CameraLauncher.java @@ -303,7 +303,7 @@ public void takePicture(int returnType, int encodingType) this.imageUri = new CordovaUri(FileProvider.getUriForFile(cordova.getActivity(), applicationId + ".provider", photo)); - intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri.getCorrectUri()); + intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri.getCorrectUri()); //We can write to this URI, this will hopefully allow us to write files to get to the next step intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); @@ -389,7 +389,7 @@ public void getImage(int srcType, int returnType, int encodingType) { } File photo = createCaptureFile(JPEG); croppedUri = Uri.fromFile(photo); - intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, croppedUri); + intent.putExtra(MediaStore.EXTRA_OUTPUT, croppedUri); } else { intent.setAction(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); @@ -912,14 +912,14 @@ private void writeUncompressedImage(Uri src, Uri dest) throws FileNotFoundExcept */ private Uri getUriFromMediaStore() { ContentValues values = new ContentValues(); - values.put(android.provider.MediaStore.Images.Media.MIME_TYPE, JPEG_MIME_TYPE); + values.put(MediaStore.Images.Media.MIME_TYPE, JPEG_MIME_TYPE); Uri uri; try { - uri = this.cordova.getActivity().getContentResolver().insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); + uri = this.cordova.getActivity().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); } catch (RuntimeException e) { LOG.d(LOG_TAG, "Can't write to external media storage."); try { - uri = this.cordova.getActivity().getContentResolver().insert(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, values); + uri = this.cordova.getActivity().getContentResolver().insert(MediaStore.Images.Media.INTERNAL_CONTENT_URI, values); } catch (RuntimeException ex) { LOG.d(LOG_TAG, "Can't write to internal media storage."); return null; @@ -1245,9 +1245,9 @@ private void checkForDuplicateImage(int type) { */ private Uri whichContentStore() { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { - return android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI; + return MediaStore.Images.Media.EXTERNAL_CONTENT_URI; } else { - return android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI; + return MediaStore.Images.Media.INTERNAL_CONTENT_URI; } } @@ -1261,14 +1261,23 @@ public void processPicture(Bitmap bitmap, int encodingType) { CompressFormat compressFormat = encodingType == JPEG ? CompressFormat.JPEG : CompressFormat.PNG; + String imageMimeType = encodingType == JPEG ? + JPEG_MIME_TYPE : + PNG_MIME_TYPE; try { if (bitmap.compress(compressFormat, mQuality, jpeg_data)) { byte[] code = jpeg_data.toByteArray(); byte[] output = Base64.encode(code, Base64.NO_WRAP); String js_out = new String(output); - this.callbackContext.success(js_out); + String base64Prefix = "data:" + imageMimeType + ";base64,"; + String finalJsOut = base64Prefix + js_out; + + this.callbackContext.success(finalJsOut); + js_out = null; + base64Prefix = null; + finalJsOut = null; output = null; code = null; } @@ -1299,7 +1308,7 @@ private void scanForGallery(Uri newImage) { public void onMediaScannerConnected() { try { this.conn.scanFile(this.scanMe.toString(), "image/*"); - } catch (java.lang.IllegalStateException e) { + } catch (IllegalStateException e) { LOG.e(LOG_TAG, "Can't scan file in MediaScanner after taking picture"); } diff --git a/src/browser/CameraProxy.js b/src/browser/CameraProxy.js index ff81257a6..b59514f4d 100644 --- a/src/browser/CameraProxy.js +++ b/src/browser/CameraProxy.js @@ -36,10 +36,8 @@ function takePicture (success, error, opts) { var reader = new FileReader(); /* eslint no-undef : 0 */ reader.onload = function (readerEvent) { input.parentNode.removeChild(input); - var imageData = readerEvent.target.result; - - return success(imageData.substr(imageData.indexOf(',') + 1)); + return success(imageData); }; reader.readAsDataURL(inputEvent.target.files[0]); @@ -79,7 +77,6 @@ function capture (success, errorCallback, opts) { // convert image stored in canvas to base64 encoded image var imageData = canvas.toDataURL('image/png'); - imageData = imageData.replace('data:image/png;base64,', ''); // stop video stream, remove video and button. // Note that MediaStream.stop() is deprecated as of Chrome 47. diff --git a/src/ios/CDVCamera.m b/src/ios/CDVCamera.m index bf02ca2e6..91dda2786 100644 --- a/src/ios/CDVCamera.m +++ b/src/ios/CDVCamera.m @@ -493,7 +493,10 @@ - (void)resultForImage:(CDVPictureOptions*)options info:(NSDictionary*)info comp image = [self retrieveImage:info options:options]; NSData* data = [self processImage:image info:info options:options]; if (data) { - result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:toBase64(data)]; + NSString* mimeType = options.encodingType == EncodingTypePNG? @"image/png" : @"image/jpeg"; + NSString* finalDataURL = [NSString stringWithFormat:@"%@%@%@%@", @"data:", mimeType, @";base64,", toBase64(data)]; + + result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:finalDataURL]; } } break; @@ -701,7 +704,10 @@ - (void)imagePickerControllerReturnImageResult break; case DestinationTypeDataUrl: { - result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:toBase64(self.data)]; + NSString* mimeType = self.pickerController.pictureOptions.encodingType == EncodingTypePNG ? @"image/png" : @"image/jpeg"; + NSString* finalDataURL = [NSString stringWithFormat:@"%@%@%@%@", @"data:", mimeType, @";base64,", toBase64(self.data)]; + + result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:finalDataURL]; } break; case DestinationTypeNativeUri: diff --git a/src/osx/CDVCamera.m b/src/osx/CDVCamera.m index 9693eefb8..b4785752f 100644 --- a/src/osx/CDVCamera.m +++ b/src/osx/CDVCamera.m @@ -180,7 +180,10 @@ - (void)returnImage:(NSImage *)image command:(CDVInvokedUrlCommand *)command opt NSData *processedImageData = [self processImage:image options:pictureOptions]; if (pictureOptions.destinationType == DestinationTypeDataUrl) { - CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[processedImageData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]]; + NSString* mimeType = pictureOptions.encodingType == EncodingTypeJPEG? @"image/jpeg" : @"image/png"; + NSString* finalDataURL = [NSString stringWithFormat:@"%@%@%@%@", @"data:", mimeType, @";base64,", [processedImageData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]]; + + CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:finalDataURL]; [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; } else { NSString *tempFilePath = [self uniqueImageName:pictureOptions]; @@ -255,4 +258,4 @@ - (NSString *)uniqueImageName:(CDVPictureOptions *)pictureOptions { return uniqueFileName; } -@end \ No newline at end of file +@end diff --git a/src/windows/CameraProxy.js b/src/windows/CameraProxy.js index 7ede906f9..56948c35d 100644 --- a/src/windows/CameraProxy.js +++ b/src/windows/CameraProxy.js @@ -153,10 +153,7 @@ function resizeImageBase64 (successCallback, errorCallback, file, targetWidth, t // The resized file ready for upload var finalFile = canvas.toDataURL(file.contentType); - // Remove the prefix such as "data:" + contentType + ";base64," , in order to meet the Cordova API. - var arr = finalFile.split(','); - var newStr = finalFile.substr(arr[0].length + 1); - successCallback(newStr); + successCallback(finalFile); }; }, function (err) { errorCallback(err); }); }