CB-8706, CB-8707 saveToPhotoAlbum improvements and some refactoring

- On Windows we should use filePicker instead of programmatically
accessing user libs. This way the app doesnt have to depend on extra
capabilities.

- Some refactoring to help on navigating code easier

This closes #78
This commit is contained in:
Vladimir Kotikov 2015-04-07 21:48:00 +03:00
commit 3927735d09
2 changed files with 464 additions and 400 deletions

View File

@ -241,16 +241,7 @@
<!-- windows --> <!-- windows -->
<platform name="windows"> <platform name="windows">
<config-file target="package.windows.appxmanifest" parent="/Package/Capabilities"> <config-file target="package.appxmanifest" parent="/Package/Capabilities">
<Capability Name="picturesLibrary" />
<DeviceCapability Name="webcam" />
</config-file>
<config-file target="package.windows80.appxmanifest" parent="/Package/Capabilities">
<Capability Name="picturesLibrary" />
<DeviceCapability Name="webcam" />
</config-file>
<config-file target="package.phone.appxmanifest" parent="/Package/Capabilities">
<Capability Name="picturesLibrary" />
<DeviceCapability Name="webcam" /> <DeviceCapability Name="webcam" />
</config-file> </config-file>
<js-module src="www/CameraPopoverHandle.js" name="CameraPopoverHandle"> <js-module src="www/CameraPopoverHandle.js" name="CameraPopoverHandle">

View File

@ -19,11 +19,11 @@
* *
*/ */
/*global Windows:true, URL:true */ /*jshint unused:true, undef:true, browser:true */
/*global Windows:true, URL:true, module:true, require:true */
var cordova = require('cordova'), var Camera = require('./Camera');
Camera = require('./Camera');
module.exports = { module.exports = {
@ -43,18 +43,19 @@ module.exports = {
// 11 cameraDirection:0 // 11 cameraDirection:0
takePicture: function (successCallback, errorCallback, args) { takePicture: function (successCallback, errorCallback, args) {
var encodingType = args[5];
var targetWidth = args[3];
var targetHeight = args[4];
var sourceType = args[2]; var sourceType = args[2];
var destinationType = args[1];
var mediaType = args[6];
var allowCrop = !!args[7];
var saveToPhotoAlbum = args[9];
var cameraDirection = args[11];
// resize method :) if (sourceType != Camera.PictureSourceType.CAMERA) {
var resizeImage = function (file) { takePictureFromFile(successCallback, errorCallback, args);
} else {
takePictureFromCamera(successCallback, errorCallback, args);
}
}
};
// Resize method
function resizeImage(successCallback, errorCallback, file, targetWidth, targetHeight, encodingType) {
var tempPhotoFileName = ""; var tempPhotoFileName = "";
if (encodingType == Camera.EncodingType.PNG) { if (encodingType == Camera.EncodingType.PNG) {
tempPhotoFileName = "camera_cordova_temp_return.png"; tempPhotoFileName = "camera_cordova_temp_return.png";
@ -63,8 +64,9 @@ module.exports = {
} }
var storageFolder = Windows.Storage.ApplicationData.current.localFolder; var storageFolder = Windows.Storage.ApplicationData.current.localFolder;
file.copyAsync(storageFolder, file.name, Windows.Storage.NameCollisionOption.replaceExisting).then(function (storageFile) { file.copyAsync(storageFolder, file.name, Windows.Storage.NameCollisionOption.replaceExisting)
Windows.Storage.FileIO.readBufferAsync(storageFile).then(function(buffer) { .then(function (storageFile) { return Windows.Storage.FileIO.readBufferAsync(storageFile); })
.then(function(buffer) {
var strBase64 = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(buffer); var strBase64 = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(buffer);
var imageData = "data:" + file.contentType + ";base64," + strBase64; var imageData = "data:" + file.contentType + ";base64," + strBase64;
var image = new Image(); var image = new Image();
@ -73,6 +75,7 @@ module.exports = {
var imageWidth = targetWidth, var imageWidth = targetWidth,
imageHeight = targetHeight; imageHeight = targetHeight;
var canvas = document.createElement('canvas'); var canvas = document.createElement('canvas');
var storageFileName;
canvas.width = imageWidth; canvas.width = imageWidth;
canvas.height = imageHeight; canvas.height = imageHeight;
@ -83,25 +86,25 @@ module.exports = {
var storageFolder = Windows.Storage.ApplicationData.current.localFolder; var storageFolder = Windows.Storage.ApplicationData.current.localFolder;
storageFolder.createFileAsync(tempPhotoFileName, Windows.Storage.CreationCollisionOption.generateUniqueName).done(function (storagefile) { storageFolder.createFileAsync(tempPhotoFileName, Windows.Storage.CreationCollisionOption.generateUniqueName)
.then(function (storagefile) {
var content = Windows.Security.Cryptography.CryptographicBuffer.decodeFromBase64String(fileContent); var content = Windows.Security.Cryptography.CryptographicBuffer.decodeFromBase64String(fileContent);
Windows.Storage.FileIO.writeBufferAsync(storagefile, content).then(function () { storageFileName = storagefile.name;
successCallback("ms-appdata:///local/" + storagefile.name); return Windows.Storage.FileIO.writeBufferAsync(storagefile, content);
}, function () { })
errorCallback("Resize picture error."); .done(function () {
}); successCallback("ms-appdata:///local/" + storageFileName);
}); }, errorCallback);
}; };
}); })
}, function () { .done(null, function(err) {
errorCallback("Can't access localStorage folder"); errorCallback(err);
}); }
);
}; }
// because of asynchronous method, so let the successCallback be called in it.
var resizeImageBase64 = function (file) {
// Because of asynchronous method, so let the successCallback be called in it.
function resizeImageBase64(successCallback, errorCallback, file, targetWidth, targetHeight) {
Windows.Storage.FileIO.readBufferAsync(file).done( function(buffer) { Windows.Storage.FileIO.readBufferAsync(file).done( function(buffer) {
var strBase64 = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(buffer); var strBase64 = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(buffer);
var imageData = "data:" + file.contentType + ";base64," + strBase64; var imageData = "data:" + file.contentType + ";base64," + strBase64;
@ -128,11 +131,10 @@ module.exports = {
var newStr = finalFile.substr(arr[0].length + 1); var newStr = finalFile.substr(arr[0].length + 1);
successCallback(newStr); successCallback(newStr);
}; };
}); }, function(err) { errorCallback(err); });
}; }
if (sourceType != Camera.PictureSourceType.CAMERA) {
function takePictureFromFile(successCallback, errorCallback, mediaType, destinationType, targetWidth, targetHeight, encodingType) {
// TODO: Add WP8.1 support // TODO: Add WP8.1 support
// WP8.1 doesn't allow to use of pickSingleFileAsync method // WP8.1 doesn't allow to use of pickSingleFileAsync method
// see http://msdn.microsoft.com/en-us/library/windows/apps/br207852.aspx for details // see http://msdn.microsoft.com/en-us/library/windows/apps/br207852.aspx for details
@ -156,15 +158,17 @@ module.exports = {
} }
fileOpenPicker.pickSingleFileAsync().done(function (file) { fileOpenPicker.pickSingleFileAsync().done(function (file) {
if (file) { if (!file) {
errorCallback("User didn't choose a file.");
return;
}
if (destinationType == Camera.DestinationType.FILE_URI || destinationType == Camera.DestinationType.NATIVE_URI) { if (destinationType == Camera.DestinationType.FILE_URI || destinationType == Camera.DestinationType.NATIVE_URI) {
if (targetHeight > 0 && targetWidth > 0) { if (targetHeight > 0 && targetWidth > 0) {
resizeImage(file); resizeImage(successCallback, errorCallback, file, targetWidth, targetHeight, encodingType);
} }
else { else {
var storageFolder = Windows.Storage.ApplicationData.current.localFolder; var storageFolder = Windows.Storage.ApplicationData.current.localFolder;
file.copyAsync(storageFolder, file.name, Windows.Storage.NameCollisionOption.replaceExisting).then(function (storageFile) { file.copyAsync(storageFolder, file.name, Windows.Storage.NameCollisionOption.replaceExisting).done(function (storageFile) {
successCallback(URL.createObjectURL(storageFile)); successCallback(URL.createObjectURL(storageFile));
}, function () { }, function () {
errorCallback("Can't access localStorage folder."); errorCallback("Can't access localStorage folder.");
@ -174,39 +178,44 @@ module.exports = {
} }
else { else {
if (targetHeight > 0 && targetWidth > 0) { if (targetHeight > 0 && targetWidth > 0) {
resizeImageBase64(file); resizeImageBase64(successCallback, errorCallback, file, targetWidth, targetHeight);
} else { } else {
Windows.Storage.FileIO.readBufferAsync(file).done(function (buffer) { Windows.Storage.FileIO.readBufferAsync(file).done(function (buffer) {
var strBase64 = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(buffer); var strBase64 = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(buffer);
successCallback(strBase64); successCallback(strBase64);
}); }, errorCallback);
} }
}
} else {
errorCallback("User didn't choose a file.");
} }
}, function () { }, function () {
errorCallback("User didn't choose a file."); errorCallback("User didn't choose a file.");
}); });
}
} else { function takePictureFromCamera(successCallback, errorCallback, args) {
var CaptureNS = Windows.Media.Capture;
// Check if necessary API available // Check if necessary API available
if (!CaptureNS.CameraCaptureUI) { if (!Windows.Media.Capture.CameraCaptureUI) {
takePictureFromCameraWP(successCallback, errorCallback, args);
} else {
takePictureFromCameraWindows(successCallback, errorCallback, args);
}
}
function takePictureFromCameraWP(successCallback, errorCallback, args) {
// We are running on WP8.1 which lacks CameraCaptureUI class // We are running on WP8.1 which lacks CameraCaptureUI class
// so we need to use MediaCapture class instead and implement custom UI for camera // so we need to use MediaCapture class instead and implement custom UI for camera
var destinationType = args[1],
var capturePreview = null, targetWidth = args[3],
targetHeight = args[4],
encodingType = args[5],
saveToPhotoAlbum = args[9],
cameraDirection = args[11],
capturePreview = null,
captureCancelButton = null, captureCancelButton = null,
capture = null, capture = null,
captureSettings = null; captureSettings = null,
CaptureNS = Windows.Media.Capture;
var createCameraUI = function () { var createCameraUI = function () {
// Create fullscreen preview // Create fullscreen preview
capturePreview = document.createElement("video"); capturePreview = document.createElement("video");
@ -226,13 +235,17 @@ module.exports = {
}; };
var startCameraPreview = function () { var startCameraPreview = function () {
// Search for available camera devices // Search for available camera devices
// This is necessary to detect which camera (front or back) we should use // This is necessary to detect which camera (front or back) we should use
var expectedPanel = cameraDirection === 1 ? Windows.Devices.Enumeration.Panel.front : Windows.Devices.Enumeration.Panel.back; var expectedPanel = cameraDirection === 1 ? Windows.Devices.Enumeration.Panel.front : Windows.Devices.Enumeration.Panel.back;
Windows.Devices.Enumeration.DeviceInformation.findAllAsync(Windows.Devices.Enumeration.DeviceClass.videoCapture) Windows.Devices.Enumeration.DeviceInformation.findAllAsync(Windows.Devices.Enumeration.DeviceClass.videoCapture)
.done(function (devices) { .done(function (devices) {
if (devices.length > 0) { if (devices.length <= 0) {
destroyCameraPreview();
errorCallback('Camera not found');
return;
}
devices.forEach(function(currDev) { devices.forEach(function(currDev) {
if (currDev.enclosureLocation.panel && currDev.enclosureLocation.panel == expectedPanel) { if (currDev.enclosureLocation.panel && currDev.enclosureLocation.panel == expectedPanel) {
captureSettings.videoDeviceId = currDev.id; captureSettings.videoDeviceId = currDev.id;
@ -262,11 +275,7 @@ module.exports = {
destroyCameraPreview(); destroyCameraPreview();
errorCallback('Camera intitialization error ' + err); errorCallback('Camera intitialization error ' + err);
}); });
} else { }, errorCallback);
destroyCameraPreview();
errorCallback('Camera not found');
}
});
}; };
var destroyCameraPreview = function () { var destroyCameraPreview = function () {
@ -288,7 +297,8 @@ module.exports = {
var encodingProperties, var encodingProperties,
fileName, fileName,
generateUniqueCollisionOption = Windows.Storage.CreationCollisionOption.generateUniqueName, generateUniqueCollisionOption = Windows.Storage.CreationCollisionOption.generateUniqueName,
tempFolder = Windows.Storage.ApplicationData.current.temporaryFolder; tempFolder = Windows.Storage.ApplicationData.current.temporaryFolder,
capturedFile;
if (encodingType == Camera.EncodingType.PNG) { if (encodingType == Camera.EncodingType.PNG) {
fileName = 'photo.png'; fileName = 'photo.png';
@ -298,16 +308,19 @@ module.exports = {
encodingProperties = Windows.Media.MediaProperties.ImageEncodingProperties.createJpeg(); encodingProperties = Windows.Media.MediaProperties.ImageEncodingProperties.createJpeg();
} }
tempFolder.createFileAsync(fileName, generateUniqueCollisionOption).done(function(capturedFile) { tempFolder.createFileAsync(fileName, generateUniqueCollisionOption)
capture.capturePhotoToStorageFileAsync(encodingProperties, capturedFile).done(function() { .then(function(tempCapturedFile) {
capturedFile = tempCapturedFile;
return capture.capturePhotoToStorageFileAsync(encodingProperties, capturedFile);
})
.done(function() {
destroyCameraPreview(); destroyCameraPreview();
// success callback for capture operation // success callback for capture operation
var success = function(capturedfile) { var success = function(capturedfile) {
if (destinationType == Camera.DestinationType.FILE_URI || destinationType == Camera.DestinationType.NATIVE_URI) { if (destinationType == Camera.DestinationType.FILE_URI || destinationType == Camera.DestinationType.NATIVE_URI) {
if (targetHeight > 0 && targetWidth > 0) { if (targetHeight > 0 && targetWidth > 0) {
resizeImage(capturedfile); resizeImage(successCallback, errorCallback, capturedFile, targetWidth, targetHeight, encodingType);
} else { } else {
capturedfile.copyAsync(Windows.Storage.ApplicationData.current.localFolder, capturedfile.name, generateUniqueCollisionOption).done(function(copiedfile) { capturedfile.copyAsync(Windows.Storage.ApplicationData.current.localFolder, capturedfile.name, generateUniqueCollisionOption).done(function(copiedfile) {
successCallback("ms-appdata:///local/" + copiedfile.name); successCallback("ms-appdata:///local/" + copiedfile.name);
@ -315,35 +328,66 @@ module.exports = {
} }
} else { } else {
if (targetHeight > 0 && targetWidth > 0) { if (targetHeight > 0 && targetWidth > 0) {
resizeImageBase64(capturedfile); resizeImageBase64(successCallback, errorCallback, capturedfile, targetWidth, targetHeight);
} else { } else {
Windows.Storage.FileIO.readBufferAsync(capturedfile).done(function(buffer) { Windows.Storage.FileIO.readBufferAsync(capturedfile).done(function(buffer) {
var strBase64 = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(buffer); var strBase64 = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(buffer);
capturedfile.deleteAsync().done(function() { capturedfile.deleteAsync().done(function() {
successCallback(strBase64); successCallback(strBase64);
}, function(err) { }, function(err) {
console.error(err); errorCallback(err);
successCallback(strBase64);
}); });
}, errorCallback); }, errorCallback);
} }
} }
}; };
if (saveToPhotoAlbum) { if (!saveToPhotoAlbum) {
capturedFile.copyAsync(Windows.Storage.KnownFolders.picturesLibrary, capturedFile.name, generateUniqueCollisionOption)
.done(function() {
success(capturedFile);
}, errorCallback);
} else {
success(capturedFile); success(capturedFile);
return;
} }
/*
Need to add and remove an event listener to catch activation state
Using FileSavePicker will suspend the app and it's required to catch the pickSaveFileContinuation
https://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn631755.aspx
*/
var cameraActivationHandler = function(eventArgs) {
if (eventArgs.kind === Windows.ApplicationModel.Activation.ActivationKind.pickSaveFileContinuation) {
var file = eventArgs.file;
if (file) {
// Prevent updates to the remote version of the file until we're done
Windows.Storage.CachedFileManager.deferUpdates(file);
capturedFile.moveAndReplaceAsync(file)
.then(function() {
// Let Windows know that we're finished changing the file so
// the other app can update the remote version of the file.
return Windows.Storage.CachedFileManager.completeUpdatesAsync(file);
})
.done(function(updateStatus) {
if (updateStatus === Windows.Storage.Provider.FileUpdateStatus.complete) {
success(capturedFile);
} else {
errorCallback();
}
}, errorCallback);
} else {
errorCallback();
}
Windows.UI.WebUI.WebUIApplication.removeEventListener("activated", cameraActivationHandler);
}
};
}, function(err) { var savePicker = new Windows.Storage.Pickers.FileSavePicker();
destroyCameraPreview(); savePicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.documentsLibrary;
errorCallback(err); if (encodingType === Camera.EncodingType.PNG) {
}); savePicker.fileTypeChoices.insert("PNG", [".png"]);
} else {
savePicker.fileTypeChoices.insert("JPEG", [".jpg"]);
}
savePicker.suggestedFileName = fileName;
Windows.UI.WebUI.WebUIApplication.addEventListener("activated", cameraActivationHandler);
savePicker.pickSaveFileAndContinue();
}, function(err) { }, function(err) {
destroyCameraPreview(); destroyCameraPreview();
errorCallback(err); errorCallback(err);
@ -356,10 +400,16 @@ module.exports = {
} catch (ex) { } catch (ex) {
errorCallback(ex); errorCallback(ex);
} }
}
} else { function takePictureFromCameraWindows(successCallback, errorCallback, args) {
var destinationType = args[1],
var cameraCaptureUI = new Windows.Media.Capture.CameraCaptureUI(); targetWidth = args[3],
targetHeight = args[4],
encodingType = args[5],
allowCrop = !!args[7],
saveToPhotoAlbum = args[9],
cameraCaptureUI = new Windows.Media.Capture.CameraCaptureUI();
cameraCaptureUI.photoSettings.allowCropping = allowCrop; cameraCaptureUI.photoSettings.allowCropping = allowCrop;
if (encodingType == Camera.EncodingType.PNG) { if (encodingType == Camera.EncodingType.PNG) {
@ -383,59 +433,82 @@ module.exports = {
cameraCaptureUI.photoSettings.maxResolution = Windows.Media.Capture.CameraCaptureUIMaxPhotoResolution.highestAvailable; cameraCaptureUI.photoSettings.maxResolution = Windows.Media.Capture.CameraCaptureUIMaxPhotoResolution.highestAvailable;
} }
cameraCaptureUI.captureFileAsync(Windows.Media.Capture.CameraCaptureUIMode.photo).then(function(picture) { cameraCaptureUI.captureFileAsync(Windows.Media.Capture.CameraCaptureUIMode.photo).done(function(picture) {
if (picture) { if (!picture) {
errorCallback("User didn't capture a photo.");
return;
}
// save to photo album successCallback // save to photo album successCallback
var success = function() { var success = function() {
if (destinationType == Camera.DestinationType.FILE_URI) { if (destinationType == Camera.DestinationType.FILE_URI) {
if (targetHeight > 0 && targetWidth > 0) { if (targetHeight > 0 && targetWidth > 0) {
resizeImage(picture); resizeImage(successCallback, errorCallback, picture, targetWidth, targetHeight, encodingType);
} else { } else {
var storageFolder = Windows.Storage.ApplicationData.current.localFolder; var storageFolder = Windows.Storage.ApplicationData.current.localFolder;
picture.copyAsync(storageFolder, picture.name, Windows.Storage.NameCollisionOption.replaceExisting).then(function(storageFile) { picture.copyAsync(storageFolder, picture.name, Windows.Storage.NameCollisionOption.replaceExisting).done(function(storageFile) {
successCallback("ms-appdata:///local/" + storageFile.name); successCallback("ms-appdata:///local/" + storageFile.name);
}, function() { }, errorCallback);
errorCallback("Can't access localStorage folder.");
});
} }
} else { } else {
if (targetHeight > 0 && targetWidth > 0) { if (targetHeight > 0 && targetWidth > 0) {
resizeImageBase64(picture); resizeImageBase64(successCallback, errorCallback, picture, targetWidth, targetHeight);
} else { } else {
Windows.Storage.FileIO.readBufferAsync(picture).done(function(buffer) { Windows.Storage.FileIO.readBufferAsync(picture).done(function(buffer) {
var strBase64 = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(buffer); var strBase64 = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(buffer);
successCallback(strBase64); successCallback(strBase64);
}); }, errorCallback);
} }
} }
}; };
// save to photo album errorCallback // save to photo album errorCallback
var fail = function() { var fail = function() {
//errorCallback("FileError, code:" + fileError.code); //errorCallback("FileError, code:" + fileError.code);
errorCallback("Save fail."); errorCallback("Save fail.");
}; };
if (saveToPhotoAlbum) { if (!saveToPhotoAlbum) {
Windows.Storage.StorageFile.getFileFromPathAsync(picture.path).then(function(storageFile) {
storageFile.copyAsync(Windows.Storage.KnownFolders.picturesLibrary, picture.name, Windows.Storage.NameCollisionOption.generateUniqueName).then(function(storageFile) {
success(); success();
}, function() { return;
}
var savePicker = new Windows.Storage.Pickers.FileSavePicker();
savePicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.documentsLibrary;
if (encodingType === Camera.EncodingType.PNG) {
savePicker.fileTypeChoices.insert("PNG", [".png"]);
savePicker.suggestedFileName = "photo.png";
} else {
savePicker.fileTypeChoices.insert("JPEG", [".jpg"]);
savePicker.suggestedFileName = "photo.jpg";
}
savePicker.pickSaveFileAsync()
.done(function(file) {
if (file) {
// Prevent updates to the remote version of the file until we're done
Windows.Storage.CachedFileManager.deferUpdates(file);
picture.moveAndReplaceAsync(file)
.then(function() {
// Let Windows know that we're finished changing the file so
// the other app can update the remote version of the file.
return Windows.Storage.CachedFileManager.completeUpdatesAsync(file);
})
.done(function(updateStatus) {
if (updateStatus === Windows.Storage.Provider.FileUpdateStatus.complete) {
success();
} else {
fail(); fail();
});
});
} else {
success();
} }
}, fail);
} else { } else {
errorCallback("User didn't capture a photo."); fail();
} }
}, fail);
}, function() { }, function() {
errorCallback("Fail to capture a photo."); errorCallback("Fail to capture a photo.");
}); });
} }
}
}
};
require("cordova/exec/proxy").add("Camera",module.exports); require("cordova/exec/proxy").add("Camera",module.exports);