index.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="apple-mobile-web-app-capable" content="yes" />
  6. <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1"/>
  7. <meta name="theme-color" content="#4b75ff">
  8. <link rel="stylesheet" href="../script/semantic/semantic.min.css">
  9. <script src="../script/jquery.min.js"></script>
  10. <script src="../script/semantic/semantic.min.js"></script>
  11. <script src="../script/ao_module.js"></script>
  12. <link rel="manifest" crossorigin="use-credentials" href="manifest.json">
  13. <title>Camera</title>
  14. <style>
  15. body{
  16. background-color: #000000;
  17. }
  18. .previewer{
  19. width: 100%;
  20. max-height: 100%;
  21. }
  22. .previewer.mirror{
  23. -webkit-transform: scaleX(-1);
  24. transform: scaleX(-1);
  25. }
  26. #controls{
  27. position: fixed;
  28. left: 0px;
  29. bottom: 0px;
  30. width: 100%;
  31. padding: 12px;
  32. padding-bottom: 20px;
  33. }
  34. #verticalUI .shutterContainer{
  35. display: flex;
  36. justify-content: space-evenly;
  37. }
  38. #verticalUI .shutter.image{
  39. width: 5em;
  40. }
  41. #verticalUI .settingButton{
  42. position: absolute;
  43. bottom: 2em;
  44. left: 1em;
  45. }
  46. #verticalUI .cameraFunctionPanel{
  47. position: fixed;
  48. width: 100%;
  49. top:0px;
  50. left: 0px;
  51. }
  52. #verticalUI .cameraFunctionPanel .camswitch{
  53. position: absolute;
  54. top: 1em;
  55. right: 1em;
  56. box-shadow: none;
  57. background-color: transparent !important;
  58. }
  59. #sidewayUI .shutterContainer{
  60. position: fixed;
  61. right: 0px;
  62. height: 100%;
  63. top:0px;
  64. width: 5em;
  65. }
  66. #sidewayUI .shutter.image{
  67. position: absolute;
  68. right: 20px;
  69. width: 5em;
  70. top: 50%;
  71. -ms-transform: translateY(-50%);
  72. transform: translateY(-50%);
  73. }
  74. #sidewayUI .settingButton{
  75. position: fixed;
  76. top: 2em;
  77. right: 1em;
  78. }
  79. #sidewayUI .cameraFunctionPanel{
  80. position: fixed;
  81. height: 100%;
  82. top:0px;
  83. left: 0px;
  84. }
  85. #sidewayUI .cameraFunctionPanel .camswitch{
  86. position: absolute;
  87. top: 1em;
  88. left: 1em;
  89. box-shadow: none;
  90. background-color: transparent !important;
  91. }
  92. #shutterCover{
  93. position: fixed;
  94. top:0px;
  95. left: 0px;
  96. width: 100%;
  97. height: 100%;
  98. background-color: black;
  99. display:none;
  100. }
  101. #albumn{
  102. position: fixed;
  103. bottom: 2em;
  104. right: 1em;
  105. }
  106. .ablumnpreview{
  107. width: 3em;
  108. cursor: pointer;
  109. }
  110. .latestPreview{
  111. width: 3em !important;
  112. height: 3em !important;
  113. }
  114. .zoombarcontainer{
  115. width: 100%;
  116. padding-bottom: 8px;
  117. padding-left: 20px;
  118. padding-right: 20px;
  119. }
  120. .sideway.zoombarcontainer{
  121. display: flex;
  122. justify-content: space-evenly;
  123. }
  124. #zoombar{
  125. width: 100%;
  126. }
  127. #zoombar.sideway{
  128. width: 50% !important;
  129. }
  130. </style>
  131. </head>
  132. <body>
  133. <video id="camera" class="previewer" autoplay></video>
  134. <div id="shutterCover"></div>
  135. <canvas id="canvas" style="display:none;"></canvas>
  136. <div id="controls">
  137. <div class="zoombarcontainer">
  138. <input id="zoombar" type="range" min="1" max="100" value="50">
  139. </div>
  140. <div></div>
  141. <div id="verticalUI">
  142. <div class="cameraFunctionPanel">
  143. <div class="ui button small icon basic camswitch" onclick="switchCamera();">
  144. <img src="img/icons/camswitch.svg">
  145. </div>
  146. </div>
  147. <div class="shutterContainer">
  148. <div onclick="takePicture();" ontouchdown="takePicture();">
  149. <img class="ui shutter image" src="img/shutter.png">
  150. </div>
  151. </div>
  152. <div class="settingButton">
  153. <div>
  154. <img class="ui tiny image savemodebutton" style="cursor: pointer;" onclick="toggleSaveFormat();" src="img/icons/jpg-mode.svg"/>
  155. </div>
  156. </div>
  157. </div>
  158. <div id="sidewayUI">
  159. <div class="cameraFunctionPanel">
  160. <div class="ui button small icon basic camswitch" onclick="switchCamera();">
  161. <img src="img/icons/camswitch.svg">
  162. </div>
  163. </div>
  164. <div class="shutterContainer">
  165. <div onclick="takePicture();" ontouchdown="takePicture();">
  166. <img class="ui shutter image" src="img/shutter.png">
  167. </div>
  168. </div>
  169. <div class="settingButton">
  170. <div>
  171. <img class="ui tiny image savemodebutton" style="cursor: pointer;" onclick="toggleSaveFormat();" src="img/icons/jpg-mode.svg"/>
  172. </div>
  173. </div>
  174. </div>
  175. <div id="albumn">
  176. <div class="ablumnpreview" onclick="openImageView();">
  177. <img class="ui fluid image latestPreview" src="img/module_icon.png">
  178. </div>
  179. </div>
  180. </div>
  181. <script>
  182. let isMobile = false;
  183. let saveFolder = "user:/Photo/DCIM";
  184. let cameraFacingBackground = true;
  185. let videoStream = null;
  186. let saveFormat = "jpg";
  187. if( /Android|webOS|iPhone|iPad|Mac|Macintosh|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
  188. isMobile = true;
  189. }
  190. $(document).ready(function(){
  191. ao_module_agirun("Camera/backend/readydir.js", {savetarget: saveFolder}, function(){
  192. //Folder structure ready
  193. initcamera();
  194. });
  195. loadAblumnPreview();
  196. initVideoDeviceConstraints();
  197. });
  198. function openImageView(){
  199. window.location.href = "preview.html";
  200. }
  201. $(window).on("resize", function(){
  202. handleWindowResize();
  203. });
  204. handleWindowResize();
  205. function handleWindowResize(){
  206. var width = window.innerWidth;
  207. var height = window.innerHeight;
  208. if (width > height){
  209. //Using the phone sideway
  210. $("#verticalUI").hide();
  211. $("#sidewayUI").show();
  212. $(".zoombarcontainer").addClass("sideway");
  213. $("#zoombar").addClass("sideway");
  214. }else{
  215. //Use the phone vertically
  216. $("#verticalUI").show();
  217. $("#sidewayUI").hide();
  218. $(".zoombarcontainer").removeClass("sideway");
  219. $("#zoombar").removeClass("sideway");
  220. }
  221. }
  222. function loadAblumnPreview(){
  223. ao_module_agirun("Camera/backend/loadLatestPhoto.js", {savetarget: saveFolder}, function(data){
  224. if (data.error !== undefined){
  225. console.log(data.error);
  226. }else if (data != ""){
  227. $(".latestPreview").attr("src", `../media/?file=${data}`);
  228. console.log(data);
  229. }
  230. });
  231. }
  232. function toggleSaveFormat(){
  233. if (saveFormat == "jpg"){
  234. //Switch to png mode
  235. saveFormat = "png";
  236. $(".savemodebutton").attr('src','img/icons/png-mode.svg');
  237. }else{
  238. //Switch to jpg mode
  239. saveFormat = "jpg";
  240. $(".savemodebutton").attr('src','img/icons/jpg-mode.svg');
  241. }
  242. }
  243. function takePicture(){
  244. $("#shutterCover").show(0);
  245. setTimeout(function(){
  246. $("#shutterCover").hide();
  247. }, 500);
  248. var canvas = document.getElementById('canvas');
  249. var video = document.getElementById('camera');
  250. canvas.width = video.videoWidth;
  251. canvas.height = video.videoHeight;
  252. canvas.getContext('2d').drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
  253. var mime = "image/jpeg";
  254. if (saveFormat == "png"){
  255. mime = "image/png";
  256. }
  257. canvas.toBlob(function(blob){
  258. //const img = new Image();
  259. //img.src = URL.createObjectURL(blob);
  260. //window.open(img.src);
  261. //Generate a filename for this photo
  262. var d = new Date();
  263. var filename = "DSC_" + d.getFullYear() + d.getMonth() + d.getDate() + "_" + d.getHours() + d.getMinutes() + d.getSeconds() + "." + saveFormat;
  264. //Upload it to server
  265. uploadImageToServer(filename, blob, function(){
  266. //Update the image preview
  267. loadAblumnPreview();
  268. });
  269. }, mime);
  270. }
  271. //Upload the file to server
  272. function uploadFile(file, uploadPath, callback=undefined){
  273. let url = '../../system/file_system/upload'
  274. let formData = new FormData()
  275. let xhr = new XMLHttpRequest()
  276. formData.append('file', file);
  277. formData.append('path', uploadPath);
  278. xhr.open('POST', url, true)
  279. xhr.upload.addEventListener("progress", function(e) {
  280. var progress = (e.loaded * 100.0 / e.total) || 100;
  281. console.log(progress);
  282. });
  283. xhr.addEventListener('readystatechange', function(e) {
  284. if (xhr.readyState == 4 && xhr.status == 200) {
  285. //Uplaod process ok
  286. var resp = JSON.parse(e.target.response);
  287. if (callback != undefined){
  288. callback(resp);
  289. }
  290. }else{
  291. //Upload failed
  292. if (callback != undefined){
  293. callback({
  294. error: "File upload failed"
  295. });
  296. }
  297. }
  298. });
  299. xhr.send(formData);
  300. }
  301. function uploadImageToServer(filename, blob, callback){
  302. var imageFile = ao_module_utils.blobToFile(blob, filename, blob.type);
  303. uploadFile(imageFile, "user:/Photo/DCIM", callback);
  304. }
  305. function initVideoDeviceConstraints(){
  306. let supports = navigator.mediaDevices.getSupportedConstraints();
  307. console.log(supports);
  308. if( supports['facingMode'] == false ) {
  309. $(".camswitch").addClass("disabled");
  310. }
  311. }
  312. function switchCamera(){
  313. //Stop all the previous streams
  314. videoStream.getTracks().forEach(t => {
  315. t.stop();
  316. });
  317. //Toggle the new camera
  318. cameraFacingBackground = !cameraFacingBackground;
  319. if (cameraFacingBackground){
  320. //Use environment
  321. $(".previewer").removeClass("mirror");
  322. initcamera();
  323. }else{
  324. //Use user
  325. $(".previewer").addClass("mirror");
  326. initcamera('user');
  327. }
  328. }
  329. function initcamera(facingmode = 'environment'){
  330. //Define the camera constraints
  331. const constraints = {
  332. video: {
  333. width: { ideal: 2048 },
  334. height: { ideal: 1080 },
  335. zoom: true,
  336. facingMode: facingmode
  337. },
  338. audio: false
  339. };
  340. let video = document.querySelector("video");
  341. //Just to make sure it is oaused when starting
  342. video.pause()
  343. video.srcObject = null
  344. navigator.mediaDevices.getUserMedia(constraints).then((stream) => {
  345. const [track] = stream.getVideoTracks();
  346. videoStream = stream;
  347. try{
  348. const capabilities = track.getCapabilities();
  349. const settings = track.getSettings();
  350. console.log(track, settings);
  351. //alert(JSON.stringify(settings));
  352. video.srcObject = stream;
  353. video.play();
  354. //Check if zoom exists. If yes, allow zoom
  355. if (!('zoom' in settings)) {
  356. $("#zoombar").hide();
  357. }else{
  358. var input = $("#zoombar")[0];
  359. input.min = capabilities.zoom.min;
  360. input.max = capabilities.zoom.max;
  361. input.step = capabilities.zoom.step;
  362. input.value = settings.zoom;
  363. input.oninput = function(event) {
  364. track.applyConstraints({
  365. advanced: [ {zoom: event.target.value} ]
  366. });
  367. }
  368. }
  369. }catch(ex){
  370. //This video input support nothing
  371. $("#zoombar").hide();
  372. video.srcObject = stream;
  373. }
  374. });
  375. }
  376. </script>
  377. </body>
  378. </html>