Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dyazincahya/93c4daa6a9928f3fe8825a85db821980 to your computer and use it in GitHub Desktop.
Save dyazincahya/93c4daa6a9928f3fe8825a85db821980 to your computer and use it in GitHub Desktop.
var enumsModule = require("ui/enums");
var cameraModule = require("camera");
var fs = require('file-system');
var bghttp = require("nativescript-background-http");
exports.takephoto = function(args) {
cameraModule.takePicture({width: 800, height: 800, keepAspectRatio: true}).then(function(picture) {
var savepath = fs.knownFolders.currentApp().path + "/saved_images";
var filename = 'img_' + new Date().getTime() + '.jpg';
var filepath = fs.path.join(savepath, filename);
var picsaved = picture.saveToFile(filepath, enumsModule.ImageFormat.jpeg);
if(picsaved) {
console.log("Saving");
var session = bghttp.session("image-upload");
var request = {
url: config.apiUrl,
method: "POST",
headers: {
"Content-Type": "application/octet-stream",
"File-Name": filename
},
description: "{ 'uploading': '" + filename + "' }"
};
var task = session.uploadFile("file://" + filepath, request);
task.on("progress", logEvent);
task.on("error", logEvent);
task.on("complete", logEvent);
function logEvent(e) {
console.log(e.eventName);
}
} else {
console.log("Failed To Save");
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment