qrcode_automatic.html 9.1 KB

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