This commit is contained in:
ldeluca 2014-05-20 13:54:43 -04:00
commit 2eb71f648f
3 changed files with 38 additions and 65 deletions

View File

@ -92,13 +92,6 @@ scenario, the image may not appear when the cordova activity is restored.
### Android Quirks ### 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 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 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. scenario, the image may not appear when the Cordova activity is restored.
@ -213,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 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 VIDEO: 1, // allow selection of video only, WILL ALWAYS RETURN FILE_URI
ALLMEDIA : 2 // allow selection from all media types ALLMEDIA : 2 // allow selection from all media types
}; };
- __correctOrientation__: Rotate the image to correct for the orientation of the device during capture. _(Boolean)_ - __correctOrientation__: Rotate the image to correct for the orientation of the device during capture. _(Boolean)_

View File

@ -133,55 +133,34 @@ module.exports = {
// because of asynchronous method, so let the successCallback be called in it. // because of asynchronous method, so let the successCallback be called in it.
var resizeImageBase64 = function (file) { 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 () { Windows.Storage.FileIO.readBufferAsync(file).done( function(buffer) {
var imageWidth = targetWidth, var strBase64 = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(buffer);
imageHeight = targetHeight; var imageData = "data:" + file.contentType + ";base64," + strBase64;
var canvas = document.createElement('canvas');
canvas.width = imageWidth; var image = new Image();
canvas.height = imageHeight; image.src = imageData;
var ctx = canvas.getContext("2d"); image.onload = function() {
ctx.drawImage(this, 0, 0, imageWidth, imageHeight); var imageWidth = targetWidth,
imageHeight = targetHeight;
var canvas = document.createElement('canvas');
// The resized file ready for upload canvas.width = imageWidth;
var finalFile = canvas.toDataURL(fileType); canvas.height = imageHeight;
// Remove the prefix such as "data:" + contentType + ";base64," , in order to meet the Cordova API. var ctx = canvas.getContext("2d");
var arr = finalFile.split(","); ctx.drawImage(this, 0, 0, imageWidth, imageHeight);
var newStr = finalFile.substr(arr[0].length + 1);
successCallback(newStr);
};
};
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) { if (sourceType != Camera.PictureSourceType.CAMERA) {

View File

@ -343,18 +343,8 @@ namespace WPCordovaClassLib.Cordova.Commands
try try
{ {
//use photo's actual width & height if user doesn't provide width & height // Resize photo and convert to JPEG
if (cameraOptions.TargetWidth < 0 && cameraOptions.TargetHeight < 0) imageContent = ResizePhoto(stream);
{
int streamLength = (int)stream.Length;
imageContent = new byte[streamLength + 1];
stream.Read(imageContent, 0, streamLength);
}
else
{
// resize photo
imageContent = ResizePhoto(stream);
}
} }
finally finally
{ {
@ -368,7 +358,6 @@ namespace WPCordovaClassLib.Cordova.Commands
/// Resize image /// Resize image
/// </summary> /// </summary>
/// <param name="stream">Image stream</param> /// <param name="stream">Image stream</param>
/// <param name="fileData">File data</param>
/// <returns>resized image</returns> /// <returns>resized image</returns>
private byte[] ResizePhoto(Stream stream) private byte[] ResizePhoto(Stream stream)
{ {
@ -382,10 +371,22 @@ namespace WPCordovaClassLib.Cordova.Commands
WriteableBitmap objWB = new WriteableBitmap(objBitmap); WriteableBitmap objWB = new WriteableBitmap(objBitmap);
objBitmap.UriSource = null; objBitmap.UriSource = null;
//Keep proportionally // Calculate resultant image size
double ratio = Math.Min((double)cameraOptions.TargetWidth / objWB.PixelWidth, (double)cameraOptions.TargetHeight / objWB.PixelHeight); int width, height;
int width = Convert.ToInt32(ratio * objWB.PixelWidth); if (cameraOptions.TargetWidth >= 0 && cameraOptions.TargetHeight >= 0)
int height = Convert.ToInt32(ratio * objWB.PixelHeight); {
// 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 //Hold the result stream
using (MemoryStream objBitmapStreamResized = new MemoryStream()) using (MemoryStream objBitmapStreamResized = new MemoryStream())