Use WinJS functionality to resize image instead of File plugin functionality

This commit is contained in:
Vladimir Kotikov 2014-06-11 12:49:58 +04:00
parent 1cf38cd775
commit ffd46c4ef5

View File

@ -69,14 +69,14 @@ module.exports = {
} else { } else {
tempPhotoFileName = "camera_cordova_temp_return.jpg"; tempPhotoFileName = "camera_cordova_temp_return.jpg";
} }
var imgObj = new Image();
var success = function (fileEntry) { var storageFolder = Windows.Storage.ApplicationData.current.localFolder;
var successCB = function (filePhoto) { file.copyAsync(storageFolder, file.name, Windows.Storage.NameCollisionOption.replaceExisting).then(function (storageFile) {
var fileType = file.contentType, Windows.Storage.FileIO.readBufferAsync(storageFile).then(function(buffer) {
reader = new FileReader(); var strBase64 = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(buffer);
reader.onloadend = function () { var imageData = "data:" + file.contentType + ";base64," + strBase64;
var image = new Image(); var image = new Image();
image.src = reader.result; image.src = imageData;
image.onload = function() { image.onload = function() {
var imageWidth = targetWidth, var imageWidth = targetWidth,
imageHeight = targetHeight; imageHeight = targetHeight;
@ -85,48 +85,24 @@ module.exports = {
canvas.width = imageWidth; canvas.width = imageWidth;
canvas.height = imageHeight; canvas.height = imageHeight;
var ctx = canvas.getContext("2d"); canvas.getContext("2d").drawImage(this, 0, 0, imageWidth, imageHeight);
ctx.drawImage(this, 0, 0, imageWidth, imageHeight);
// The resized file ready for upload var fileContent = canvas.toDataURL(file.contentType).split(',')[1];
var _blob = canvas.msToBlob();
var _stream = _blob.msDetachStream();
var storageFolder = Windows.Storage.ApplicationData.current.localFolder; var storageFolder = Windows.Storage.ApplicationData.current.localFolder;
storageFolder.createFileAsync(tempPhotoFileName, Windows.Storage.CreationCollisionOption.generateUniqueName).done(function (file) {
file.openAsync(Windows.Storage.FileAccessMode.readWrite).done(function (fileStream) { storageFolder.createFileAsync(tempPhotoFileName, Windows.Storage.CreationCollisionOption.generateUniqueName).done(function (storagefile) {
Windows.Storage.Streams.RandomAccessStream.copyAndCloseAsync(_stream, fileStream).done(function () { var content = Windows.Security.Cryptography.CryptographicBuffer.decodeFromBase64String(fileContent);
var _imageUrl = URL.createObjectURL(file); Windows.Storage.FileIO.writeBufferAsync(storagefile, content).then(function () {
successCallback(_imageUrl); successCallback("ms-appdata:///local/" + storagefile.name);
}, function () { }, function () {
errorCallback("Resize picture error."); errorCallback("Resize picture error.");
}); });
}, function () { });
errorCallback("Resize picture error."); };
}); });
}, function () { }, function () {
errorCallback("Resize picture error."); errorCallback("Can't access localStorage folder");
});
};
};
reader.readAsDataURL(filePhoto);
};
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, storageFile.path));
}, function () {
fail(FileError.INVALID_MODIFICATION_ERR);
}, function () {
errorCallback("Folder not access.");
}); });
}; };