[SelfCamera]update SelfCamera(tizen_2.1)
authorgs86.lee <gs86.lee@samsung.com>
Fri, 12 Apr 2013 06:30:12 +0000 (15:30 +0900)
committergs86.lee <gs86.lee@samsung.com>
Fri, 12 Apr 2013 07:12:33 +0000 (16:12 +0900)
Change-Id: Iaa96433b3fa5ac4b9fce714cef11c38f6c08b74b

AUTHORS
NOTICE [new file with mode: 0644]
config.xml
css/style.css
images/transparent.png [new file with mode: 0644]
index.html
js/app.systemIO.js
js/main.js

diff --git a/AUTHORS b/AUTHORS
index cd174ca..17fdfef 100644 (file)
--- a/AUTHORS
+++ b/AUTHORS
@@ -4,3 +4,4 @@ Piotr Wronski <p.wronski at samsung dot com>
 Dariusz Paziewski <d.paziewski at samsung dot com>
 Tomasz Paciorek <t.paciorek at samsung dot com>
 Aniela Rudy-Gawecka <a.rudy-gawec at samsung dot com>
+Jakub Siewierski <j.siewierski at samsung dot com>
diff --git a/NOTICE b/NOTICE
new file mode 100644 (file)
index 0000000..85044e4
--- /dev/null
+++ b/NOTICE
@@ -0,0 +1,4 @@
+Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
+Except as noted, this software is licensed under Flora License, Version 1.
+Please, see the LICENSE.Flora file for Flora License terms and conditions.
+
index 563e9a5..baf1d1a 100755 (executable)
@@ -1,14 +1,15 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<widget xmlns="http://www.w3.org/ns/widgets" xmlns:tizen="http://tizen.org/ns/widgets" id="http://sample-web-application.tizen.org/SelfCamera" version="2.2.2" viewmodes="maximized">
-    <access origin="*" subdomains="true"/>
-    <tizen:application id="0almfh8eer" required_version="1.0"/>
-    <tizen:privilege name="http://tizen.org/privilege/application.read"/>
-    <tizen:privilege name="http://tizen.org/privilege/application.launch"/>
-    <tizen:privilege name="http://tizen.org/privilege/filesystem.read"/>
-    <tizen:privilege name="http://tizen.org/privilege/filesystem.write"/>
-    <tizen:privilege name="http://tizen.org/privilege/tizen"/>
-    <content src="index.html"/>
-    <icon src="icon.png"/>
-    <name>SelfCamera</name>
-    <tizen:setting screen-orientation="portrait" context-menu="disable"/>
+<widget xmlns="http://www.w3.org/ns/widgets" xmlns:tizen="http://tizen.org/ns/widgets" id="http://sample-web-application.tizen.org/SelfCamera" version="2.1.0" viewmodes="maximized">
+       <access origin="*" subdomains="true"/>
+       <tizen:application id="QvgnlNAjTV.SelfCamera" package="QvgnlNAjTV" required_version="2.1"/>
+       <content src="index.html"/>
+       <icon src="icon.png"/>
+       <name>SelfCamera</name>
+       <tizen:privilege name="http://tizen.org/privilege/application.read"/>
+       <tizen:privilege name="http://tizen.org/privilege/application.launch"/>
+       <tizen:privilege name="http://tizen.org/privilege/filesystem.read"/>
+       <tizen:privilege name="http://tizen.org/privilege/filesystem.write"/>
+       <tizen:privilege name="http://tizen.org/privilege/content.read"/>
+       <tizen:privilege name="http://tizen.org/privilege/content.write"/>
+       <tizen:setting screen-orientation="portrait" context-menu="disable" background-support="enable" encryption="disable" install-location="auto"/>
 </widget>
index 51cf66d..5a61a91 100644 (file)
@@ -8,7 +8,10 @@ body {
        margin: 0px auto;
        background-color: black;
 }
-
+#center {
+       position:absolute;
+       left: 50%;
+}
 #background {
        height: 100%;
        position: relative;
diff --git a/images/transparent.png b/images/transparent.png
new file mode 100644 (file)
index 0000000..803fd6a
Binary files /dev/null and b/images/transparent.png differ
index dc1ff0f..50cd122 100644 (file)
        <title>Self camera</title>
 
        <link rel="stylesheet" type="text/css" href="css/style.css" />
-       <script src="/usr/share/tizen-web-ui-fw/latest/js/jquery.js"></script>
+       <script src="/usr/share/tizen-web-ui-fw/latest/js/jquery.min.js"></script>
        <script src="js/app.systemIO.js"></script>
        <script src="js/main.js"></script>
 </head>
 
 <body>
        <div class="crop">
-               <video id="background" autoplay="autoplay"></video>
+               <div id="center">
+                       <video id="background" autoplay="autoplay"></video>
+               </div>
        </div>
 
        <div id="countdown"></div>
index 5b6a6b1..400b636 100644 (file)
@@ -18,12 +18,11 @@ function SystemIO() {
                 * @param {string} fileName
                 */
                createFile: function SystemIO_createFile(directoryHandle, fileName) {
-                       console.log('SystemIO_createFile', directoryHandle, fileName);
 
                        try {
                                return directoryHandle.createFile(fileName);
                        } catch (e) {
-                               console.error('SystemIO_createFile error:', e);
+                               console.error('SystemIO_createFile error: ' + e.message);
                                return false;
                        }
                },
@@ -37,11 +36,9 @@ function SystemIO() {
                 * @param {string} content encoding
                 */
                writeFile: function SystemIO_writeFile(fileHandle, fileContent, onSuccess, onError, contentEncoding) {
-                       console.log('SystemIO_writeFile', fileHandle, fileContent.length, contentEncoding);
                        onError = onError || function () {};
 
                        fileHandle.openStream('w', function (fileStream) {
-                               console.log('SystemIO_writeFile:_onOpenStreamSuccess', fileStream);
                                if (contentEncoding === 'base64') {
                                        fileStream.writeBase64(fileContent);
                                } else {
@@ -66,27 +63,25 @@ function SystemIO() {
                 * @param {string} mode
                 */
                openDir: function SystemIO_openDir(directoryPath, onSuccess, onError, openMode) {
-                       console.log('SystemIO_openDir', directoryPath, openMode);
                        openMode = openMode || 'rw';
                        onSuccess = onSuccess || function () {};
 
                        try {
                                tizen.filesystem.resolve(directoryPath, onSuccess, onError, openMode);
                        } catch (e) {
-                               console.log('SystemIO_openDir error:' + e.message);
+                               console.error('SystemIO_openDir error:' + e.message);
                        }
                },
 
                /**
                 * Get list of files
                 *
-                * @param {string} directory path
-                * @param {function} on success callback
-                * @param {function} on error callback
+                * @param {string} directoryPath directory path
+                * @param {function} onSuccess on success callback
+                * @param {function} onError on error callback
                 * @param {string} fileMask
                 */
                dir: function SystemIO_dir(directoryPath, onSuccess, onError, fileMask) {
-                       console.log('SystemIO_dir', directoryPath, fileMask);
                        fileMask = fileMask || '';
                        onSuccess = onSuccess || function () {};
 
@@ -104,13 +99,13 @@ function SystemIO() {
                        }
 
                        function onOpenDirError(e) {
-                               console.log('onOpenDirError', e.message);
+                               console.error('onOpenDirError: ' + e.message);
                        }
 
                        try {
                                this.openDir(directoryPath, onOpenDir, onOpenDirError, 'r');
                        } catch (e) {
-                               console.log('SystemIO_dir error:' + e.message);
+                               console.error('SystemIO_dir error:' + e.message);
                        }
                },
 
@@ -121,7 +116,6 @@ function SystemIO() {
                 * @returnss {array}
                 */
                getPathData: function SystemIO_getPathData(filePath) {
-                       console.log('SystemIO_getPathData', filePath);
                        var path = {
                                originalPath: filePath,
                                fileName: '',
@@ -143,13 +137,11 @@ function SystemIO() {
                 * @param {string} file encoding
                 */
                saveFileContent: function SystemIO_saveFileContent(filePath, fileContent, onSaveSuccess, fileEncoding) {
-                       console.log('SystemIO_saveFileContent', filePath, fileContent.length, fileEncoding);
                        var pathData = this.getPathData(filePath),
                                self = this,
                                fileHandle;
 
                        function onOpenDirSuccess(dir) {
-                               console.log('SystemIO_saveFileContent:_onOpenDirSuccess', dir);
                                // create new file
                                fileHandle = self.createFile(dir, pathData.fileName);
                                if (fileHandle !== false) {
@@ -169,23 +161,19 @@ function SystemIO() {
                 * @param {function} success callback
                 */
                deleteNode: function SystemIO_deleteNode(nodePath, onSuccess) {
-                       console.log('SystemIO_deleteNode', nodePath);
                        var pathData = this.getPathData(nodePath),
                                self = this;
 
                        function onDeleteSuccess() {
-                               console.log('SystemIO_deleteNode:_onDeleteSuccess');
                                onSuccess();
                        }
 
                        function onDeleteError(e) {
-                               console.log('SystemIO_deleteNode:_onDeleteError', e);
+                               console.error('SystemIO_deleteNode:_onDeleteError', e);
                        }
 
                        function onOpenDirSuccess(dir) {
-                               console.log('SystemIO_deleteNode:_onOpenDirSuccess', dir);
                                var onListFiles = function (files) {
-                                       console.log('SystemIO_deleteNode:_onListFiles', files);
                                        if (files.length > 0) {
                                                // file exists;
                                                if (files[0].isDirectory) {
@@ -215,12 +203,11 @@ function SystemIO() {
                 * Deletes specified file
                 *
                 * @param {File} dir
-                * @param {string} file path
-                * @param {function} delete success callback
-                * @param {function} delete error callback
+                * @param {string} filePath file path
+                * @param {function} onDeleteSuccess delete success callback
+                * @param {function} onDeleteError delete error callback
                 */
                deleteFile: function SystemIO_deleteFile(dir, filePath, onDeleteSuccess, onDeleteError) {
-                       console.log('SystemIO_deleteFile', filePath);
                        try {
                                dir.deleteFile(filePath, onDeleteSuccess, onDeleteError);
                        } catch (e) {
@@ -239,7 +226,6 @@ function SystemIO() {
                 * @returns {boolean}
                 */
                deleteDir: function SystemIO_deleteDir(dir, dirPath, onDeleteSuccess, onDeleteError) {
-                       console.log('SystemIO_deleteDir', dir, dirPath);
                        try {
                                dir.deleteDirectory(dirPath, false, onDeleteSuccess, onDeleteError);
                        } catch (e) {
@@ -248,6 +234,33 @@ function SystemIO() {
                        }
 
                        return true;
+               },
+
+               /**
+                * The method check the file exists;
+                * @param {string} filePath
+                * @param {function} onCheck success callback
+                * @returns {undefined}
+                */
+               fileExists: function SystemIO_fileExists(filePath, onCheck) {
+                       var pathData = this.getPathData(filePath);
+
+                       function onOpenDirSuccess(dir) {
+                               try {
+                                       dir.resolve(pathData.fileName);
+                                       onCheck(true);
+                               } catch (error) {
+                                       onCheck(false);
+                               }
+                       }
+
+                       // if directory not exists, the file also not exists;
+                       function onOpenDirError(error) {
+                               onCheck(false);
+                       }
+
+                       // open directory
+                       this.openDir(pathData.dirName, onOpenDirSuccess, onOpenDirError);
                }
        };
 }());
index 445858f..309a3bd 100644 (file)
@@ -15,7 +15,7 @@
  */
 
 /*jslint devel: true*/
-/*global $, Audio, window, localStorage, tizen, canvas, SystemIO */
+/*global $, Audio, window, localStorage, tizen, canvas, SystemIO, document, navigator, clearInterval, setInterval, setTimeout */
 var selfCamera;
 function SelfCamera() {
        "use strict";
@@ -44,6 +44,18 @@ function SelfCamera() {
                this.timer = value;
                $('#timer2, #timer5, #timer10').removeClass('selected');
                $('#timer' + value).addClass('selected');
+               selfCamera.video.play();
+       };
+
+       SelfCamera.prototype.refreshPhoto = function (fileHandle, onSuccess, onError) {
+               var filePath = fileHandle.toURI().replace('file://', '');
+               if (typeof (onSuccess) !== 'function') {
+                       onSuccess = function () {};
+               }
+               if (typeof (onError) !== 'function'){
+                       onError = function () {};
+               }
+               tizen.content.scanFile(filePath, onSuccess, onError);
        };
 
        SelfCamera.prototype.onCaptureVideoSuccess = function onCaptureVideoSuccess(stream) {
@@ -56,7 +68,7 @@ function SelfCamera() {
        };
 
        SelfCamera.prototype.onCaptureVideoError = function onCaptureVideoError(e) {
-               console.error(e.message);
+               console.error(e);
        };
 
        SelfCamera.prototype.startPreview = function startPreview() {
@@ -80,14 +92,13 @@ function SelfCamera() {
        SelfCamera.prototype.increaseSequence = function increaseSequence() {
                var seq, str;
                function fillStr(num) {
-                       var str = num;
-                       if (num < 10) {
-                               str = '0' + str;
-                       }
-                       if (num < 100) {
-                               str = '0' + str;
+                       num = num.toString();
+                       if (num.length < 2) {
+                               num = '00' + num;
+                       } else if (num.length < 3) {
+                               num = '0' + num;
                        }
-                       return str;
+                       return num;
                }
 
                if (this.sequence) {
@@ -105,24 +116,48 @@ function SelfCamera() {
                var regexp = new RegExp('^' + this.saveDirectory);
                if (this.filename) {
                        $('#upImage').css('background-image', 'url(' + this.loadDirectory + this.filename.replace(regexp, '') + ')');
+                       $('#thumbnail').css('background-image', 'url("./images/transparent.png")');
                }
        };
 
-       SelfCamera.prototype.launchGallery = function launchGallery() {
-               var service, onReply;
-               this.filename = this.loadDirectory + this.IMG_PREFIX + this.sequence + '.png';
+       SelfCamera.prototype.onFileExistsCheck = function (exists) {
+               if (!exists) {
+                       this.setLastPhoto();
+               }
+       };
 
-               service = new tizen.ApplicationControl('http://tizen.org/appcontrol/operation/view', null, null, null);
+       SelfCamera.prototype.launchPreview = function launchPreview() {
+               var service, onReply, self = this;
+               if (this.filename === '') {
+                       return false;
+               }
 
-               onReply = {
-                       onsuccess: function (data) {},
-                       onfailure: function () {
-                               alert('fail');
+               function fillStr(num) {
+                       num = num.toString();
+                       if (num.length < 2) {
+                               num = '00' + num;
+                       } else if (num.length < 3) {
+                               num = '0' + num;
                        }
+                       return num;
+               }
+
+               this.showPhotoPreview(this.loadDirectory + this.filename);
+               return true;
+       };
+
+       SelfCamera.prototype.showGallery = function showGallery(service) {
+               var onReply, self = this;
+               onReply = {
+                       onsuccess: function (data) {
+                               self.showPhotoPreview(data[0].value[0]);
+                       },
+                       onfailure: function () {}
                };
 
                try {
-                       tizen.application.launchAppControl(service, null, function () {}, function (err) {
+                       tizen.application.launchAppControl(service, null, function () {
+                       }, function (err) {
                                console.error('Gallery launch failed: ' + err.message);
                        }, onReply);
                } catch (exc) {
@@ -130,6 +165,20 @@ function SelfCamera() {
                }
        };
 
+       SelfCamera.prototype.showPhotoPreview = function showPhotoPreview(file) {
+               var service, onReply, self = this;
+               service = new tizen.ApplicationControl('http://tizen.org/appcontrol/operation/view', file, "image/*");
+               onReply = {onsuccess: function () {}, onfailure: function () {}};
+
+               try {
+                       tizen.application.launchAppControl(service, null, function () {}, function (err) {
+                               console.error('Photo launch failed: ' + err.message);
+                       }, onReply);
+               } catch (exc) {
+                       alert('Exception: ' + exc.message);
+               }
+       };
+
        SelfCamera.prototype.setLoadDirectory = function setLoadDirectory(dirName) {
                this.loadDirectory = dirName;
                if (!this.loadDirectory.match(/\/$/)) {
@@ -141,13 +190,19 @@ function SelfCamera() {
                var data, onSuccess = function (fileHandle) {
                        this.setLoadDirectory(fileHandle.parent.toURI());
                        this.loadThumbnail();
+                       this.refreshPhoto(fileHandle);
                }.bind(this);
 
                try {
-                       data = canvas.toDataURL().replace('data:image/png;base64,', '');
+                       data = canvas.toDataURL().replace('data:image/png;base64,', '').replace('data:,', '');
+                       if (data === '') {
+                               throw {message: "No image source"};
+                       }
                } catch (e) {
+                       this.filename = '';
                        console.error('canvas.toDataUrl error: ' + e.message);
-                       alert('canvas.toDataUrl error.');
+                       alert("Data source error: " + e.message);
+                       return;
                }
 
                try {
@@ -163,7 +218,7 @@ function SelfCamera() {
                }
        };
 
-       SelfCamera.prototype.captureImage = function captureImage(filename, video) {
+       SelfCamera.prototype.captureImage = function captureImage(video) {
                this.img.width = video.videoWidth;
                this.img.height = video.videoHeight;
                this.img.getContext('2d').drawImage(video, 0, 0);
@@ -187,21 +242,21 @@ function SelfCamera() {
 
        SelfCamera.prototype.takePhoto = function takePhoto() {
                this.increaseSequence();
-               this.filename = this.saveDirectory + 'selfcam_widget_' + this.sequence + '.png';
+               this.filename = 'selfcam_widget_' + this.sequence + '.png';
 
-               this.captureImage(this.filename, this.video);
+               this.captureImage(this.video);
                this.savePhoto();
        };
 
        SelfCamera.prototype.savePhoto = function savePhoto() {
-               this.saveCanvas(this.img, this.filename);
+               this.saveCanvas(this.img, this.saveDirectory + this.filename);
        };
 
        SelfCamera.prototype.findLastPhoto = function findLastPhoto(onFind) {
                function onDir(files) {
                        if (files && files.length > 0) {
                                files = files.sort(function (a, b) {
-                                       return (a.name === b.name) ? 0 : (a.name < b.name ? -1 : 1);
+                                       return (a.created === b.created) ? 0 : (a.created < b.created ? -1 : 1);
                                });
                                onFind(files.pop());
                        } else {
@@ -210,10 +265,10 @@ function SelfCamera() {
                }
 
                function onError(e) {
-                       console.error('systemIO.dir error: ', e.message);
+                       console.error('systemIO.dir error: ' + e.message);
                }
 
-               this.systemIO.dir(this.saveDirectory, onDir, onError, this.IMG_PREFIX);
+               this.systemIO.dir(this.saveDirectory, onDir, onError, null);
        };
 
        SelfCamera.prototype.onCountdownInterval = function onCountdownInterval() {
@@ -244,6 +299,14 @@ function SelfCamera() {
        };
 
        SelfCamera.prototype.bindEvents = function bindEvents() {
+               var self = this;
+
+               document.addEventListener('webkitvisibilitychange', function (event) {
+                       if (document.webkitVisibilityState === 'visible') {
+                               setTimeout(function () { $('#background').get(0).play(); }, 200);
+                               self.systemIO.fileExists(self.filename, self.onFileExistsCheck.bind(self));
+                       }
+               });
 
                $('shutter').mousedown(function (ev) {
                        $('shutter').addClass('active');
@@ -255,6 +318,10 @@ function SelfCamera() {
                        $('shutter').removeClass('active');
                });
 
+               this.video.addEventListener("canplay", function () {
+                       self.centerPreview();
+               });
+
                $('#exit').on('click', function () {
                        var app = tizen.application.getCurrentApplication();
                        app.exit();
@@ -263,21 +330,35 @@ function SelfCamera() {
                $('#timer5').on('click', this.setTimer.bind(this, DELAY_5_SECOND));
                $('#timer10').on('click', this.setTimer.bind(this, DELAY_10_SECOND));
 
-               $('#thumbnail').on('click', this.launchGallery.bind(this));
+               $('#thumbnail').on('click', this.launchPreview.bind(this));
                $('#shutter').on('touchstart', this.startCountdown.bind(this));
 
                $('#background').on('click', function () { this.play(); });
        };
 
-       SelfCamera.prototype.init = function init() {
-               this.video = document.getElementById('background');
-               this.systemIO = new SystemIO();
+       SelfCamera.prototype.centerPreview = function () {
+               $('#center').width($('#background').width());
+               $('#center').css('margin-left', '-'+($('#background').width()/2)+'px');
+       };
+
+       SelfCamera.prototype.setLastPhoto = function () {
                this.findLastPhoto(function (file) {
                        if (file) {
                                this.setLoadDirectory(file.parent.toURI());
                                this.setFileName(file.name);
+                               $('#thumbnail').css('background-image', 'url("./images/transparent.png")');
+                       } else {
+                               $('#upImage').css('background-image', '');
+                               $('#thumbnail').css('background-image', 'url("./images/none.png")');
+                               this.filename = '';
                        }
                }.bind(this));
+       };
+
+       SelfCamera.prototype.init = function init() {
+               this.video = document.getElementById('background');
+               this.systemIO = new SystemIO();
+               this.setLastPhoto();
                this.setTimer(DELAY_2_SECOND);
                this.startPreview();
                this.bindEvents();