yx_zjz.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. window.onload = function() {
  2. var imgdataBase64 = null;
  3. var videoElement = document.querySelector('video');
  4. var videoSelect = document.querySelector('select#videoSource');
  5. var btn = document.querySelector("#btn");
  6. var canvas = document.querySelector("#canvas"); //
  7. var context = canvas.getContext('2d');
  8. var sfzImg = $("#sfzImg");
  9. var width = sfzImg.width();
  10. var contentWidth;
  11. var contentHeight;
  12. var scale;
  13. var canvasWidth;
  14. var canvasHeight;
  15. $("#video").bind("loadedmetadata", function() {
  16. contentWidth = this.videoWidth;
  17. contentHeight = this.videoHeight;
  18. scale = contentWidth / contentHeight;
  19. var height = width / scale;
  20. sfzImg.css('height', height)
  21. canvasWidth = width * 10;
  22. canvasHeight = height * 10;
  23. videoElement.setAttribute('width', contentWidth);
  24. videoElement.setAttribute('height', contentHeight);
  25. canvas.setAttribute('width', canvasWidth);
  26. canvas.setAttribute('height', canvasHeight);
  27. });
  28. var imgWidth = 1000;
  29. // var imgHeigh=1400;
  30. var imgHeigh = 1292;
  31. var settings = {
  32. width: imgWidth,
  33. height: imgHeigh,
  34. /* 改。Lin
  35. action: "/service?wdApplication=wd&wdService=uploadForAjax"
  36. */ action: "/service?ssServ=ulByHttp&type=img"
  37. };
  38. /**
  39. * 获取所有设备
  40. */
  41. function getDevices() {
  42. return navigator.mediaDevices.enumerateDevices();
  43. }
  44. //处理设备
  45. function gotDevices(deviceInfos) {
  46. for (const deviceInfo of deviceInfos) {
  47. const option = document.createElement('option');
  48. option.value = deviceInfo.deviceId;
  49. if (deviceInfo.kind === 'videoinput') {
  50. option.text = deviceInfo.label || `Camera $ {
  51. videoSelect.length + 1
  52. }`;
  53. videoSelect.appendChild(option);
  54. }
  55. }
  56. }
  57. function getStream() {
  58. const videoSource = videoSelect.value;
  59. const constraints = {
  60. video: {
  61. deviceId: videoSource
  62. }
  63. };
  64. return navigator.mediaDevices.getUserMedia(constraints).then(gotStream).
  65. catch(handleError);
  66. }
  67. function gotStream(stream) {
  68. window.stream = stream;
  69. videoElement.srcObject = stream;
  70. }
  71. function handleError(error) {
  72. console.error('Error: ', error);
  73. alert("如果您当前使用的是谷歌浏览器,请重新插拔摄像头接口,然后再刷新页面");
  74. }
  75. function tackcapture() {
  76. if (this.value == '拍照') {
  77. $("#video").css("display", "block");
  78. $("#image").css("display", "none");
  79. getStream().then(getDevices).then(gotDevices);
  80. this.value = "截取画面";
  81. } else if (this.value == '截取画面') {
  82. // 将视频画面捕捉后绘制到canvas里面
  83. videoElement.setAttribute('width', canvasWidth);
  84. videoElement.setAttribute('height', canvasHeight);
  85. context = canvas.getContext('2d');
  86. context.clearRect(0, 0, canvasWidth, canvasHeight);
  87. context.drawImage(videoElement, 0, 0,canvasWidth, canvasHeight);
  88. imgdataBase64 = canvas.toDataURL('image/png');
  89. initCropper1(document.getElementById("file"), settings, cropConfirm, convertBase64UrlToBlob(imgdataBase64));
  90. videoElement.setAttribute('width', contentWidth);
  91. videoElement.setAttribute('height', contentHeight);
  92. }
  93. }
  94. //关闭摄像头
  95. function closeMedia() {
  96. window.stream.getTracks().forEach(function(track) {
  97. track.stop();
  98. });
  99. }
  100. function cropConfirm(result) {
  101. closeMedia();
  102. var path = result.fileList[0].path;
  103. /* 改。Lin
  104. var url = "/service?wdService=getData&path=" + path;
  105. */ var url = "/service?ssServ=dlByHttp&type=img&path="+ path;
  106. $("#image").attr("src", url);
  107. $("#video").css("display", "none");
  108. $("#image").css("display", "block");
  109. $("#btn").val("拍照");
  110. $("input[name='zjzwj']").val(path);
  111. }
  112. //注册拍照截图功能
  113. btn.addEventListener('click', tackcapture, false);
  114. function convertBase64UrlToBlob(urlData) {
  115. var bytes = window.atob(urlData.split(',')[1]); //去掉url的头,并转换为byte
  116. //处理异常,将ascii码小于0的转换为大于0
  117. var ab = new ArrayBuffer(bytes.length);
  118. var ia = new Uint8Array(ab);
  119. for (var i = 0; i < bytes.length; i++) {
  120. ia[i] = bytes.charCodeAt(i);
  121. }
  122. return new Blob([ab], {
  123. type: 'image/jpeg'
  124. });
  125. }
  126. }
  127. var file = {};
  128. //裁剪
  129. function initCropper1(fileEle, settings, cropConfirm, fileDate) {
  130. file["name"] = new Date().getTime() + ".png";
  131. initWdCropper();
  132. wdCropper.topWin = wd.topWindow;
  133. wdCropper.thisWindow = window;
  134. wdCropper.settings = settings;
  135. wdCropper.fileEle = fileEle;
  136. wdCropper.cropConfirm = cropConfirm;
  137. /// 增加,类似 upload.js 里的 initCropper( -- 去掉 /wd/js/ueditor/dialogs/wdimage/upload.js,改用 /wd/js/upload.js。Lin
  138. // 不加的话,因为 wdCropper.cropper.fileName = null,导致不能复制到 /data/img/
  139. wdCropper.cropper.fileName = file.name;
  140. ///
  141. wdCropper.fileObj = fileDate;
  142. var uploadedImageURL = URL.createObjectURL(fileDate);
  143. var img = new Image();
  144. img.onload = function() {
  145. /*img.width = 300;
  146. img.height = 420;*/
  147. wdCropper.layer.show({
  148. width: img.width,
  149. height: img.height
  150. });
  151. wdCropper.cropper.init();
  152. wdCropper.cropper.replace(uploadedImageURL);
  153. }
  154. img.src = uploadedImageURL;
  155. }