Main.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. MWF.xDesktop.requireApp("FaceSet", "Actions.RestActions", null, false);
  2. MWF.xApplication.FaceSet.options.multitask = false;
  3. MWF.xApplication.FaceSet.Main = new Class({
  4. Extends: MWF.xApplication.Common.Main,
  5. Implements: [Options, Events],
  6. options: {
  7. "style1": "default",
  8. "style": "default",
  9. "name": "FaceSet",
  10. "icon": "icon.png",
  11. "width": "1010",
  12. "height": "560",
  13. "isResize": false,
  14. "isMax": false,
  15. "title": MWF.xApplication.FaceSet.LP.title
  16. },
  17. onQueryLoad: function(){
  18. this.lp = MWF.xApplication.FaceSet.LP;
  19. this.action = MWF.Actions.get("x_faceset_control");
  20. this.faceTokens = [];
  21. //this.action = new MWF.xApplication.FaceSet.Actions.RestActions();
  22. },
  23. loadApplication: function(callback){
  24. this.node = new Element("div", {"styles": this.css.node}).inject(this.content);
  25. this.contentNode = new Element("div", {"styles": this.css.contentNode}).inject(this.node);
  26. this.picAreaNode = new Element("div", {"styles": this.css.picAreaNode}).inject(this.node);
  27. COMMON.AjaxModule.loadDom("../o2_lib/adapter/adapter.js", function(){
  28. this.createVideo();
  29. this.createAction();
  30. this.createInfor();
  31. if (callback) callback();
  32. }.bind(this));
  33. },
  34. createVideo: function(){
  35. this.videoAreaNode = new Element("div", {"styles": this.css.videoAreaNode}).inject(this.contentNode);
  36. this.videoAreaNode.set("html", "<video autoplay>"+this.lp.noMedia+"</video>");
  37. this.video = this.videoAreaNode.getFirst().setStyles(this.css.video);
  38. this.videoStart();
  39. },
  40. videoStart: function(){
  41. navigator.mediaDevices.getUserMedia({
  42. audio: false,
  43. video: true
  44. }).then(function(stream){
  45. this.video.srcObject = stream;
  46. }.bind(this)).catch(function(error){
  47. console.log('navigator.getUserMedia error: ', error);
  48. });
  49. },
  50. createAction: function(){
  51. this.actionAreaNode = new Element("div", {"styles": this.css.actionAreaNode}).inject(this.contentNode);
  52. this.actionNode = new Element("button", {"styles": this.css.actionNode, "text": this.lp.button_takePic}).inject(this.actionAreaNode);
  53. this.actionNode.addEvent("click", function(){
  54. this.takePhoto();
  55. }.bind(this));
  56. this.actionCompletedNode = new Element("button", {"styles": this.css.actionNode, "text": this.lp.button_completed}).inject(this.actionAreaNode);
  57. this.actionCompletedNode.setStyle("display", "none");
  58. this.actionCompletedNode.addEvent("click", function(){
  59. this.completed();
  60. }.bind(this));
  61. },
  62. createInfor: function(){
  63. this.inforNode = new Element("div", {"styles": this.css.inforNode}).inject(this.contentNode);
  64. this.setInfor();
  65. },
  66. setInfor: function(text){
  67. if (text){
  68. this.inforNode.set("text", text);
  69. }else{
  70. switch (this.faceTokens.length){
  71. case 1:
  72. this.inforNode.set("text", this.lp.pic2);
  73. break;
  74. case 2:
  75. this.inforNode.set("text", this.lp.pic3);
  76. break;
  77. case 3:
  78. this.inforNode.set("text", this.lp.completed);
  79. break;
  80. default:
  81. this.inforNode.set("text", this.lp.pic1);
  82. break;
  83. }
  84. }
  85. },
  86. takePhoto: function(){
  87. this.inforNode.set("text", this.lp.getFaceToken);
  88. this.actionNode.set("disable", true);
  89. var canvasAreaNode = new Element("div", {"styles": this.css.canvasAreaNode}).inject(this.picAreaNode);
  90. var canvas = new Element("canvas", {"styles": this.css.canvas}).inject(canvasAreaNode);
  91. // canvas.width = this.video.videoWidth;
  92. // canvas.height = this.video.videoHeight;
  93. canvas.width = 308;
  94. canvas.height = (308/this.video.videoWidth)*this.video.videoHeight;
  95. var margin = (225-canvas.height)/2;
  96. canvas.setStyle("margin-top", ""+margin+"px");
  97. canvas.getContext('2d').drawImage(this.video, 0, 0, canvas.width, canvas.height);
  98. this.detect(canvas);
  99. },
  100. detect: function(canvas){
  101. var blob = this.toBlob(canvas.toDataURL());
  102. var formData = new FormData();
  103. formData.append('file', blob);
  104. this.action.detect(this.desktop.session.user.unique, "1", formData, {"name": "pic", "size": blob.size}, function(json){
  105. var face = json.data.faces[0];
  106. this.faceTokens.push(face.face_token);
  107. this.setInfor();
  108. if (this.faceTokens.length>=3){
  109. this.actionCompletedNode.setStyle("display", "block");
  110. this.actionNode.setStyle("display", "none");
  111. }else{
  112. this.actionCompletedNode.setStyle("display", "none");
  113. this.actionNode.setStyle("display", "block");
  114. this.actionNode.set("disable", false);
  115. }
  116. this.drawToken(canvas, face);
  117. }.bind(this));
  118. },
  119. drawToken: function(canvas, face){
  120. var ctx = canvas.getContext('2d');
  121. ctx.fillStyle = "#00b9eb";
  122. Object.each(face.landmark, function(v, k){
  123. ctx.fillRect(v.x, v.y, 2,2);
  124. }.bind(this));
  125. },
  126. // recordFaceToken: function(face){
  127. // var action = MWF.Actions.get("x_organization_assemble_control");
  128. // action.listPersonAttribute(this.desktop.session.user.id, function(json){
  129. // var attr = null;
  130. // for (var i = 0; i<json.data.length; i++){
  131. // if (json.data[i].name==="facetoken"){
  132. // attr = json.data[i];
  133. // break;
  134. // }
  135. // }
  136. // if (attr){
  137. // if (!attr.attributeList) attr.attributeList = [];
  138. // if (attr.attributeList.length>=3){
  139. // var removeFace = attr.attributeList.shift();
  140. //
  141. // }
  142. // }else{
  143. //
  144. // }
  145. //
  146. // }.bind(this));
  147. // },
  148. toBlob : function( base64 ){
  149. var bytes;
  150. if( base64.substr( 0, 10 ) === 'data:image' ){
  151. bytes=window.atob(base64.split(',')[1]);
  152. }else{
  153. bytes=window.atob(base64)
  154. }
  155. var ab = new ArrayBuffer(bytes.length);
  156. var ia = new Uint8Array(ab);
  157. for (var i = 0; i < bytes.length; i++) {
  158. ia[i] = bytes.charCodeAt(i);
  159. }
  160. return new Blob( [ab] , {type : "image/png" });
  161. },
  162. completed: function(){
  163. var action = MWF.Actions.get("x_organization_assemble_control");
  164. MWF.UD.getDataJson("faceTokens", function(json){
  165. if (!json) json = {"tokens": []};
  166. var faceset = window.location.host;
  167. faceset = faceset.replace(/\./g, "_");
  168. this.action.getFaceSet(faceset, null, function(){
  169. this.completedToken(json);
  170. }.bind(this), function(){
  171. this.action.createFaceSet(faceset, null, function(){
  172. this.completedToken(json);
  173. }.bind(this));
  174. }.bind(this));
  175. }.bind(this));
  176. // action.listPersonAttribute(this.desktop.session.user.id, function(json){
  177. // var attr = null;
  178. // for (var i = 0; i<json.data.length; i++){
  179. // if (json.data[i].name==="facetoken"){
  180. // attr = json.data[i];
  181. // break;
  182. // }
  183. // }
  184. // if (!attr){
  185. // attr = {
  186. // "description": this.lp.description,
  187. // "name": "facetoken",
  188. // "person": this.desktop.session.user.id,
  189. // "attributeList": []
  190. // };
  191. // }
  192. //
  193. // }.bind(this),function(e){
  194. // this.inforNode.set("text", this.lp.saveAttrError);
  195. // }.bind(this));
  196. },
  197. completedToken: function(json){
  198. this.removeTokenFromFaceset(json, function(){
  199. this.saveTokenToFaceset(function(){
  200. json.tokens = this.faceTokens;
  201. MWF.UD.putData("faceTokens", json, function(){
  202. this.close();
  203. }.bind(this),function(e){
  204. this.inforNode.set("text", this.lp.saveAttrError);
  205. }.bind(this))
  206. }.bind(this), function(){
  207. this.inforNode.set("text", this.lp.saveAttrError);
  208. }.bind(this));
  209. }.bind(this), function(){
  210. this.inforNode.set("text", this.lp.saveAttrError);
  211. }.bind(this));
  212. },
  213. removeTokenFromFaceset:function(json, success, error){
  214. var faceset = window.location.host;
  215. faceset = faceset.replace(/\./g, "_");
  216. if (json && json.tokens.length){
  217. var data = {"data": json.tokens};
  218. this.action.removeface(faceset, data, success, error);
  219. }else{
  220. if (success) success();
  221. }
  222. },
  223. saveTokenToFaceset: function(success, error){
  224. var faceset = window.location.host;
  225. faceset = faceset.replace(/\./g, "_");
  226. var data = {"data": this.faceTokens};
  227. this.action.addface(faceset, data, success, error);
  228. }
  229. });