From 87a5030771b031962a2d3db14337ee3f38438a13 Mon Sep 17 00:00:00 2001 From: Vladimir Kotikov Date: Fri, 2 May 2014 12:46:15 +0400 Subject: [PATCH 1/4] CB-6613 Use WinJS functionality to get base64-encoded content of image instead of File plugin functionality --- src/windows8/CameraProxy.js | 59 ++++++++++++------------------------- 1 file changed, 19 insertions(+), 40 deletions(-) diff --git a/src/windows8/CameraProxy.js b/src/windows8/CameraProxy.js index d46553b..9f21c44 100644 --- a/src/windows8/CameraProxy.js +++ b/src/windows8/CameraProxy.js @@ -133,55 +133,34 @@ module.exports = { // because of asynchronous method, so let the successCallback be called in it. var resizeImageBase64 = function (file) { - var imgObj = new Image(); - var success = function (fileEntry) { - var successCB = function (filePhoto) { - var fileType = file.contentType, - reader = new FileReader(); - reader.onloadend = function () { - var image = new Image(); - image.src = reader.result; - image.onload = function () { - var imageWidth = targetWidth, - imageHeight = targetHeight; - var canvas = document.createElement('canvas'); + Windows.Storage.FileIO.readBufferAsync(file).done( function(buffer) { + var strBase64 = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(buffer); + var imageData = "data:" + file.contentType + ";base64," + strBase64; - canvas.width = imageWidth; - canvas.height = imageHeight; + var image = new Image(); + image.src = imageData; - var ctx = canvas.getContext("2d"); - ctx.drawImage(this, 0, 0, imageWidth, imageHeight); + image.onload = function() { + var imageWidth = targetWidth, + imageHeight = targetHeight; + var canvas = document.createElement('canvas'); - // The resized file ready for upload - var finalFile = canvas.toDataURL(fileType); + canvas.width = imageWidth; + canvas.height = imageHeight; - // 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); - }; - }; + var ctx = canvas.getContext("2d"); + ctx.drawImage(this, 0, 0, imageWidth, imageHeight); - reader.readAsDataURL(filePhoto); + // 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); }; - var failCB = function () { - errorCallback("File not found."); - }; - fileEntry.file(successCB, failCB); - }; - - var storageFolder = Windows.Storage.ApplicationData.current.localFolder; - file.copyAsync(storageFolder, file.name, Windows.Storage.NameCollisionOption.replaceExisting).then(function (storageFile) { - success(new FileEntry(storageFile.name, "ms-appdata:///local/" + storageFile.name)); - }, function () { - fail(FileError.INVALID_MODIFICATION_ERR); - }, function () { - errorCallback("Folder not access."); }); - - }; if (sourceType != Camera.PictureSourceType.CAMERA) { From 9e0d9439714b54540685f7daa23f713ca17d28f6 Mon Sep 17 00:00:00 2001 From: Vladimir Kotikov Date: Fri, 2 May 2014 10:51:32 +0400 Subject: [PATCH 2/4] CB-6612 camera.getPicture now always returns encoded JPEG image --- src/wp/Camera.cs | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/wp/Camera.cs b/src/wp/Camera.cs index b76929b..30bb5e8 100644 --- a/src/wp/Camera.cs +++ b/src/wp/Camera.cs @@ -343,18 +343,8 @@ namespace WPCordovaClassLib.Cordova.Commands try { - //use photo's actual width & height if user doesn't provide width & height - if (cameraOptions.TargetWidth < 0 && cameraOptions.TargetHeight < 0) - { - int streamLength = (int)stream.Length; - imageContent = new byte[streamLength + 1]; - stream.Read(imageContent, 0, streamLength); - } - else - { - // resize photo - imageContent = ResizePhoto(stream); - } + // Resize photo and convert to JPEG + imageContent = ResizePhoto(stream); } finally { @@ -368,7 +358,6 @@ namespace WPCordovaClassLib.Cordova.Commands /// Resize image /// /// Image stream - /// File data /// resized image private byte[] ResizePhoto(Stream stream) { @@ -382,10 +371,22 @@ namespace WPCordovaClassLib.Cordova.Commands WriteableBitmap objWB = new WriteableBitmap(objBitmap); objBitmap.UriSource = null; - //Keep proportionally - double ratio = Math.Min((double)cameraOptions.TargetWidth / objWB.PixelWidth, (double)cameraOptions.TargetHeight / objWB.PixelHeight); - int width = Convert.ToInt32(ratio * objWB.PixelWidth); - int height = Convert.ToInt32(ratio * objWB.PixelHeight); + // Calculate resultant image size + int width, height; + if (cameraOptions.TargetWidth >= 0 && cameraOptions.TargetHeight >= 0) + { + // Keep proportionally + double ratio = Math.Min( + (double)cameraOptions.TargetWidth / objWB.PixelWidth, + (double)cameraOptions.TargetHeight / objWB.PixelHeight); + width = Convert.ToInt32(ratio * objWB.PixelWidth); + height = Convert.ToInt32(ratio * objWB.PixelHeight); + } + else + { + width = objWB.PixelWidth; + height = objWB.PixelHeight; + } //Hold the result stream using (MemoryStream objBitmapStreamResized = new MemoryStream()) From 30a75e3fa7d139ce9961b84066293770746d2549 Mon Sep 17 00:00:00 2001 From: mbillau Date: Thu, 8 May 2014 09:00:37 -0400 Subject: [PATCH 3/4] Removed invalid note from CB-5398 --- doc/index.md | 7 ------- 1 file changed, 7 deletions(-) diff --git a/doc/index.md b/doc/index.md index edd881f..9b38348 100644 --- a/doc/index.md +++ b/doc/index.md @@ -92,13 +92,6 @@ scenario, the image may not appear when the cordova activity is restored. ### Android Quirks -*Android 4.4 only*: Android 4.4 introduced a new [Storage Access Framework](https://developer.android.com/guide/topics/providers/document-provider.html) that makes it -easier for users to browse and open documents across all of their preferred document storage providers. -Cordova has not yet been fully integrated with this new Storage Access Framework. Because of this, the `getPicture()` -method will not correctly return pictures when the user selects from the "Recent", "Drive", "Images", or "External -Storage" folders when the `destinationType` is `FILE_URI`. However, the user will be able to correctly select any pictures -if they go through the "Gallery" app first. Potential workarounds for this issue are documented on [this StackOverflow question](http://stackoverflow.com/questions/19834842/android-gallery-on-kitkat-returns-different-uri-for-intent-action-get-content/20177611). Please see [CB-5398](https://issues.apache.org/jira/browse/CB-5398) to track this issue. - Android uses intents to launch the camera activity on the device to capture images, and on phones with low memory, the Cordova activity may be killed. In this scenario, the image may not appear when the Cordova activity is restored. From 199732845e9ba2db063180fc269e3ecc964b1cfb Mon Sep 17 00:00:00 2001 From: Shashwat Date: Tue, 13 May 2014 16:25:57 +0530 Subject: [PATCH 4/4] Little fix in code formatting github: close #28 --- doc/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/index.md b/doc/index.md index 9b38348..e892620 100644 --- a/doc/index.md +++ b/doc/index.md @@ -206,7 +206,7 @@ Optional parameters to customize the camera settings. PICTURE: 0, // allow selection of still pictures only. DEFAULT. Will return format specified via DestinationType VIDEO: 1, // allow selection of video only, WILL ALWAYS RETURN FILE_URI ALLMEDIA : 2 // allow selection from all media types -}; + }; - __correctOrientation__: Rotate the image to correct for the orientation of the device during capture. _(Boolean)_