qrcode_add.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. {Template header}
  2. <body>
  3. <style type="text/css">
  4. #video {width:100%;height:calc(100% - 60px);margin:50px 0 10px 0;}#cz {
  5. padding:5px;
  6. position: absolute;
  7. z-index: 1;
  8. left: 0px;
  9. top: 0px;
  10. }#cz div {float:left;margin-right:20px;}
  11. #startButton {
  12. height: 32px;
  13. line-height: 32px;
  14. padding: 0 16px;
  15. color: #fff;
  16. background-color: #2084db;
  17. float: left;
  18. margin-right: 16px;
  19. border-radius: 5px;
  20. display: block;
  21. cursor: pointer;
  22. }
  23. select{ width: 280px;
  24. height: 28px;
  25. color: #333;
  26. border: 1px #ccc solid;
  27. font-size: 13px;
  28. margin-right: 20px;
  29. border-radius: 5px;}
  30. </style>
  31. <div id="cz">
  32. <div id="sourceSelectPanel">
  33. <label for="sourceSelect">选择摄像头 </label>
  34. <select id="sourceSelect">
  35. </select>
  36. </div>
  37. <div>
  38. </div>
  39. <div style="display:none">
  40. <select id="decoding-style" size="1">
  41. <option value="once">Decode once</option>
  42. <option value="continuously">Decode continuously</option>
  43. </select>
  44. </div>
  45. <div id="startButton">开始识别</div><div id="result"></div>
  46. <canvas style="display: none;"></canvas>
  47. </div>
  48. <video id="video"></video>
  49. <audio id="music_d">
  50. <source src="{$theme}music/d.mp3" type="audio/mpeg">
  51. </audio>
  52. <audio id="music_s">
  53. <source src="{$theme}music/s.mp3" type="audio/mpeg">
  54. </audio>
  55. <audio id="music_c">
  56. <source src="{$theme}music/c.mp3" type="audio/mpeg">
  57. </audio>
  58. <script type="text/javascript" src="{$theme}js/zxing.js"></script>
  59. <script type="text/javascript">
  60. function decodeOnce(codeReader, selectedDeviceId) {
  61. codeReader.decodeFromInputVideoDevice(selectedDeviceId, 'video').then((result) => {
  62. console.log(result)
  63. // document.getElementById('result').textContent = result.text;
  64. $("#music_s")[0].play();
  65. $("#resetButton").click();
  66. jt(result.text);
  67. // window.location.reload();
  68. }).catch((err) => {
  69. console.error(err)
  70. document.getElementById('result').textContent = err
  71. })
  72. }
  73. function decodeContinuously(codeReader, selectedDeviceId) {
  74. codeReader.decodeFromInputVideoDeviceContinuously(selectedDeviceId, 'video', (result, err) => {
  75. if (result) {
  76. // 解码成功
  77. //console.log('Found QR code!', result)
  78. //document.getElementById('result').textContent = result.text;
  79. }
  80. if (err) {
  81. // As long as this error belongs into one of the following categories
  82. // the code reader is going to continue as excepted. Any other error
  83. // will stop the decoding loop.
  84. //
  85. // Excepted Exceptions:
  86. //
  87. // - NotFoundException
  88. // - ChecksumException
  89. // - FormatException
  90. if (err instanceof ZXing.NotFoundException) {
  91. console.log('找不到二维码,请重试.')
  92. }
  93. if (err instanceof ZXing.ChecksumException) {
  94. console.log('无法识别二维码,请重试.')
  95. }
  96. if (err instanceof ZXing.FormatException) {
  97. console.log('无法识别二维码,请重试.')
  98. }
  99. }
  100. })
  101. }
  102. window.addEventListener('load', function () {
  103. let selectedDeviceId;
  104. const codeReader = new ZXing.BrowserQRCodeReader()
  105. console.log('ZXing code reader initialized')
  106. codeReader.getVideoInputDevices()
  107. .then((videoInputDevices) => {
  108. const sourceSelect = document.getElementById('sourceSelect')
  109. selectedDeviceId = videoInputDevices[0].deviceId
  110. if (videoInputDevices.length >= 1) {
  111. videoInputDevices.forEach((element) => {
  112. const sourceOption = document.createElement('option')
  113. sourceOption.text = element.label
  114. sourceOption.value = element.deviceId
  115. sourceSelect.appendChild(sourceOption)
  116. })
  117. sourceSelect.onchange = () => {
  118. selectedDeviceId = sourceSelect.value;
  119. };
  120. const sourceSelectPanel = document.getElementById('sourceSelectPanel')
  121. sourceSelectPanel.style.display = 'block'
  122. }
  123. $("#startButton").click(function() {
  124. const decodingStyle = document.getElementById('decoding-style').value;
  125. if (decodingStyle == "once") {
  126. decodeOnce(codeReader, selectedDeviceId);
  127. } else {
  128. decodeContinuously(codeReader, selectedDeviceId);
  129. }
  130. console.log(`Started decode from camera with id ${selectedDeviceId}`)
  131. })
  132. $("#resetButton").click(function() {
  133. codeReader.reset()
  134. document.getElementById('result').textContent = '';
  135. console.log('Reset.')
  136. })
  137. })
  138. .catch((err) => {
  139. console.error(err)
  140. })
  141. })
  142. var video = document.querySelectorAll('video')[0];
  143. var canvas = document.querySelectorAll('canvas')[0];
  144. var ctx = canvas.getContext('2d');
  145. var width = 960;
  146. var height = 720;
  147. canvas.width = width;
  148. canvas.height = height;
  149. video.src = 'movie.mp4?t=' + new Date().getTime();
  150. video.width = width;
  151. video.height = height;
  152. video.autoplay = true;
  153. video.loop = true;
  154. function jt(text) {
  155. ctx.drawImage(video, 0, 0, width, height); // 将video中的数据绘制到canvas里
  156. var img = canvas.toDataURL('image/png');
  157. layx.load('loadId','操作中,请稍后');
  158. var fcdata = "number="+text+"&warehouse="+ $("select[name='warehouse']").children("option:selected").val()+"&img="+encodeURIComponent(img);
  159. $.ajax({
  160. url: "/qrcode/add/",
  161. data: fcdata,
  162. type: "POST",
  163. dataType: "json",
  164. success: function(a) {
  165. layx.destroy('loadId');
  166. if (a && a.success) {
  167. $("#music_d")[0].play();
  168. layx.html('nocontrol-style','图像','<img src="'+img+'" />',{
  169. minWidth:962,
  170. minHeight:722,
  171. position:'cc',
  172. isOverToMax:true,
  173. shadable:0.5,
  174. autodestroy:2000,
  175. control:false,
  176. storeStatus:false
  177. });
  178. layx.msg(a.msg,{dialogIcon:'success',position:'cc',stickable:true});
  179. setTimeout(function(){
  180.     $("#startButton").click();
  181. }, 2000);
  182. } else {
  183. $("#resetButton").click();
  184. if(a.img)
  185. {
  186. layx.html('nocontrol-style','图像','<img src="'+a.img+'" />',{
  187. minWidth:962,
  188. minHeight:722,
  189. position:'cc',
  190. isOverToMax:true,
  191. shadable:0.5,
  192. control:false,
  193. storeStatus:false
  194. });
  195. layx.confirm('默认提示','此单已经录入图像,是否替换?',null,{
  196. stickable:true,
  197. buttons:[
  198. {
  199. label:'替换',
  200. callback:function(id, button, event){
  201. thimg(fcdata,img);
  202. layx.destroy(id);
  203. }
  204. },
  205. {
  206. label:'取消',
  207. callback:function(id, button, event){
  208. $("#startButton").click();
  209. layx.destroy('nocontrol-style');
  210. layx.destroy(id);
  211. }
  212. }
  213. ]
  214. });
  215. }
  216. else
  217. {
  218. $("#music_c")[0].play();
  219. layx.msg(a.msg,{dialogIcon:'error',position:'cc'});
  220. $("#startButton").click();
  221. }
  222. }
  223. }
  224. });
  225. };
  226. function thimg(fcdata,img){
  227. layx.destroy('nocontrol-style');
  228. $.ajax({
  229. url: "/qrcode/edit/",
  230. data: fcdata,
  231. type: "POST",
  232. dataType: "json",
  233. success: function(a) {
  234. layx.destroy('loadId');
  235. if (a && a.success) {
  236. $("#music_d")[0].play();
  237. layx.html('nocontrol-style','图像','<img src="'+img+'" />',{
  238. minWidth:962,
  239. minHeight:722,
  240. position:'cc',
  241. isOverToMax:true,
  242. shadable:0.5,
  243. autodestroy:5000,
  244. control:false,
  245. storeStatus:false
  246. });
  247. layx.msg(a.msg,{dialogIcon:'success',position:'cc',stickable:true});
  248. setTimeout(function(){
  249.     $("#startButton").click();
  250. }, 3500);
  251. } else {
  252. $("#music_c")[0].play();
  253. layx.msg(a.msg,{dialogIcon:'error',position:'cc'});
  254. $("#startButton").click();
  255. }
  256. }
  257. });
  258. }
  259. </script>
  260. {Template footer}