(function () {
"use strict";
function $(query) {
return document.querySelector(query);
}
function toDateTime(date) {
var dateTime = new Date(Date.parse(date));
if (dateTime.getDate()) {
var month = dateTime.getMonth() + 1;
var day = dateTime.getDate();
var year = dateTime.getFullYear();
var hours = dateTime.getHours();
var minutes = dateTime.getMinutes();
var ampm = hours < 12 ? "am" : "pm";
return "{0}/{1}/{2} {3}:{4} {5}".format(month, day, year, hours < 10 ? "0" + hours :
hours, minutes < 10 ? "0" + minutes : minutes, ampm);
}
else
return date;
}
String.prototype.format = function () {
var str = this;
for (var i = 0; i < arguments.length; i++) {
var reg = new RegExp("\\{" + i + "\\}", "gm");
str = str.replace(reg, arguments[i]);
}
return str;
};
var ERR_OPERATION_UNSUPPORTED = -2003292287; // The operation is unsupported
WinJS.UI.Pages.define("/pages/home/home.html", {
// This function is called whenever a user navigates to this page. It
// populates the page elements with the app's data.
ready: function (element, options) {
$("#btnBrowse").addEventListener("click", this.openFilePicker.bind(this), false);
$("#btnResize").addEventListener("click", this.resizeImage.bind(this), false);
},
selectedImage: null,
openFilePicker: function () {
var that = this;
// Create the picker object and set options
var fp = Windows.Storage.Pickers.FileOpenPicker();
// Users expect to have a filtered view of their folders depending on the scenario.
// For example, when choosing a documents folder, restrict the filetypes to documents for your application.
fp.fileTypeFilter.replaceAll([".png", ".bmp", ".jpg", ".jpeg"]);
fp.suggestedStartLocation = Windows.Storage.KnownFolders.picturesLibrary;
fp.commitButtonText = "Select";
fp.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail;
// Open the picker for the user to pick a file
fp.pickSingleFileAsync().done(function (file) {
if (file != null) {
that.selectedImage = file;
$("#previewImage").src = URL.createObjectURL(file, { oneTimeOnly: true });
file.properties.getImagePropertiesAsync().then(function (imgProp) {
$("#imageDetails").innerHTML = "Width: {0}<br/>Height: {1}<br/>Date Taken: {2}<br/>Camera Model: {3}".format(
imgProp.width, imgProp.height, toDateTime(imgProp.dateTaken), imgProp.cameraModel);
$("#width").value = imgProp.width;
$("#height").value = imgProp.height;
$("#btnResize").disabled = false;
});
}
else {
$("#width").value = 0;
$("#height").value = 0;
$("#btnResize").disabled = true;
}
});
},
resizeImage: function () {
var that = this;
var width = $("#width").value != "" ? parseInt($("#width").value) : 0;
var height = $("#height").value != "" ? parseInt($("#height").value) : 0;
if (width === 0 || height === 0) {
var msg = new Windows.UI.Popups.MessageDialog("Please enter width and height values");
msg.showAsync().done("");
return;
}
var fs = new Windows.Storage.Pickers.FileSavePicker();
fs.suggestedStartLocation = Windows.Storage.KnownFolders.picturesLibrary;
fs.defaultFileExtension = this.selectedImage.fileType;
fs.fileTypeChoices.insert(this.selectedImage.fileType, [this.selectedImage.fileType]);
fs.suggestedFileName = "Resize Demo-" + this.selectedImage.name;
fs.pickSaveFileAsync().done(function (targetFile) {
//copy selected image to new file
that.selectedImage.copyAndReplaceAsync(targetFile).then(function () {
//call resize function on the new file.
that._resize(width, height, targetFile).done(function () {
var msg = new Windows.UI.Popups.MessageDialog("File Resize Completed!");
msg.showAsync().done("");
});
});
});
},
_scale: function (decoder, memStream, maxWidth, maxHeight) {
return new WinJS.Promise(function (complete, error) {
var originalHeight = decoder.pixelHeight;
var originalWidth = decoder.pixelWidth;
if (originalHeight <= maxHeight || originalWidth <= maxWidth)
complete();
else {
Windows.Graphics.Imaging.BitmapEncoder.createForTranscodingAsync(memStream, decoder).then(function (_encoder) {
var encoder = _encoder;
var nPercent = 0;
var nPercentW = 0;
var nPercentH = 0;
nPercentW = (maxWidth / originalWidth);
nPercentH = (maxHeight / originalHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
var destWidth = (originalWidth * nPercent);
var destHeight = (originalHeight * nPercent);
encoder.bitmapTransform.scaledWidth = destWidth;
encoder.bitmapTransform.scaledHeight = destHeight;
// Attempt to generate a new thumbnail to reflect any rotation operation.
encoder.isThumbnailGenerated = true;
encoder.flushAsync().done(function () {
complete();
});
})
}
})
},
_resize: function (maxWidth, maxHeight, imgFile) {
var originalWidth;
var originalHeight;
var encoder;
var fileStream;
var count = 0;
var that = this;
return new WinJS.Promise(function (complete) {
var memStream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
imgFile.openAsync(Windows.Storage.FileAccessMode.readWrite).then(function (stream) {
fileStream = stream;
return Windows.Graphics.Imaging.BitmapDecoder.createAsync(fileStream);
}).then(function (decoder) {
that._scale(decoder, memStream, maxWidth, maxHeight).then(function () {
// Overwrite the contents of the file with the updated image stream.
memStream.seek(0);
fileStream.seek(0);
fileStream.size = 0;
return Windows.Storage.Streams.RandomAccessStream.copyAsync(memStream, fileStream);
})
.done(function () {
// Finally, close each stream to release any locks.
memStream && memStream.close();
fileStream && fileStream.close();
});
}
);
});
}
});
})();