screenshot.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // const util = require('@/static/utils/util.js');
  2. import * as THREE from 'three';
  3. const config = require('@/services/urlConfig.js');
  4. import { MessageBox } from 'mint-ui';
  5. export default {
  6. data() {
  7. return {
  8. canvas2d: null,
  9. }
  10. },
  11. watch: {},
  12. methods: {
  13. //触发主页面的截屏动作
  14. screenPromiseShot() {
  15. // let currUniPage = getCurrentPages()[getCurrentPages().length - 1].$vm;
  16. return new Promise(resolve => {
  17. this.screenshotResolve = resolve
  18. })
  19. },
  20. //转base64
  21. base64src(base64data, fun){
  22. const base64 = base64data; //base64格式图片
  23. const time = new Date().getTime();
  24. const imgPath = wx.env.USER_DATA_PATH + "/4DImage/" + util.formatDate(new Date(), "yyyyMMddhhmmss") + ".jpg";
  25. //如果图片字符串不含要清空的前缀,可以不执行下行代码.
  26. const imageData = base64.replace(/^data:image\/\w+;base64,/, "");
  27. const file = wx.getFileSystemManager();
  28. // console.warn("***base64src***",base64data)
  29. file.writeFileSync(imgPath, imageData, "base64");
  30. fun(imgPath);
  31. },
  32. // 翻转Y轴-像素方案
  33. flip(pixels, w, h, c) {
  34. if (!w || !h){
  35. return false;
  36. };
  37. // handle Arrays
  38. if (Array.isArray(pixels)) {
  39. var result = this.flip(new Uint8Array(pixels), w, h, c);
  40. for (var i = 0; i < pixels.length; i++) {
  41. pixels[i] = result[i];
  42. }
  43. return pixels;
  44. }
  45. // try{
  46. if (!c) c = pixels.length / (w * h);
  47. var h2 = h >> 1;
  48. var row = w * c;
  49. var Ctor = pixels.constructor;
  50. // make a temp buffer to hold one row
  51. var temp = new Ctor(w * c);
  52. for (var y = 0; y < h2; ++y) {
  53. var topOffset = y * row;
  54. var bottomOffset = (h - y - 1) * row;
  55. // make copy of a row on the top half
  56. temp.set(pixels.subarray(topOffset, topOffset + row));
  57. // copy a row from the bottom half to the top
  58. pixels.copyWithin(topOffset, bottomOffset, bottomOffset + row);
  59. // copy the copy of the top half row to the bottom half
  60. pixels.set(temp, bottomOffset);
  61. }
  62. // }catch(e){
  63. // //TODO handle the exception
  64. // alert(e+row+":"+topOffset+'-'+bottomOffset)
  65. // }
  66. },
  67. getClipPicUrl(resolve){
  68. let gl = this.renderer.getContext();
  69. let frameBuffer = new THREE.Vector2();
  70. let pix = window.devicePixelRatio;
  71. // this.renderer.getDrawingBufferSize(frameBuffer);//返回一个包含渲染器绘图缓存宽度和高度(单位像素)的对象。
  72. let width = parseInt(this.width*pix )|| frameBuffer.x;
  73. let height = parseInt(this.height*pix) || frameBuffer.y;
  74. console.warn("***getClipPicUrl***",width,height,this.bottomLeftX*pix,this.bottomLeftY*pix);
  75. let pixelData = new Uint8Array(width * height * 4);
  76. gl.readPixels(this.bottomLeftX*pix, this.bottomLeftY*pix, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixelData);
  77. this.flip(pixelData, width, height, 4);//入参必须是整数 width height
  78. // alert("getClipPicUrl1"+width)
  79. const canvas = document.getElementById('canvas');
  80. let context = canvas.getContext('2d');
  81. let img = context.createImageData(width, height);
  82. for (var i = 0; i < img.data.length; i += 4) {
  83. img.data[i + 0] = pixelData[i + 0];
  84. img.data[i + 1] = pixelData[i + 1];
  85. img.data[i + 2] = pixelData[i + 2];
  86. img.data[i + 3] = pixelData[i + 3];
  87. }
  88. // img.data = pixelData;
  89. canvas.height = img.height;
  90. canvas.width = img.width;
  91. context.putImageData(img, 0, 0);
  92. // alert("getClipPicUrl2"+width)
  93. // let data = drawCanvasCtx.getImageData(100, 100, 200, 200);
  94. // canvas.width = 100;
  95. // canvas.height = 100;
  96. // context.putImageData(data, 0, 0)
  97. let imgBase64 = canvas.toDataURL();
  98. this.uploadFile(imgBase64,resolve);
  99. // return
  100. },
  101. /**
  102. * 截图
  103. */
  104. async shottingAction(type=1) {
  105. // let currUniPage = getCurrentPages()[getCurrentPages().length - 1].$vm;
  106. //时机合适时-像素存在则触发生成
  107. return new Promise(resolve => {
  108. this.screenPromiseShot().then((param) => {
  109. if(param){
  110. let {pixelData,width,height} = param;
  111. this.flip(pixelData,width,height, 4);
  112. let canvas = document.getElementById('canvas');
  113. let context = canvas.getContext('2d');
  114. let img = context.createImageData(width, height);
  115. for (var i = 0; i < img.data.length; i += 4) {
  116. img.data[i + 0] = pixelData[i + 0];
  117. img.data[i + 1] = pixelData[i + 1];
  118. img.data[i + 2] = pixelData[i + 2];
  119. img.data[i + 3] = pixelData[i + 3];
  120. }
  121. canvas.height = img.height;
  122. canvas.width = img.width;
  123. context.putImageData(img, 0, 0);
  124. let imgBase64 = canvas.toDataURL();
  125. this.uploadFile(imgBase64,resolve);
  126. }else{
  127. // let imgBase64 = this.canvas.toDataURL();
  128. // this.base64src(imgBase64, (res) => {
  129. // console.warn('转化后的url:', res)
  130. // this.upload(res,resolve);
  131. // })
  132. // let img = new Image();
  133. // img.src = this.canvas.toDataURL('image/jpeg');// toDataURL :图片格式转成 base64
  134. let imgBase64 = this.canvas.toDataURL();
  135. if(type==2){
  136. resolve(imgBase64)
  137. }else if(type==1){
  138. this.uploadFile(imgBase64,resolve)
  139. }
  140. }
  141. })
  142. })
  143. },
  144. async uploadFile(file, cb = null) {
  145. let data = {
  146. "base64Str": file,
  147. };
  148. let result = await requestConfig("uploadBase64", data, true);
  149. console.log(result, 'gggggg');
  150. if (result && result.success) {
  151. let shottingImg = result.single.filePath;
  152. cb(shottingImg);
  153. }else{
  154. cb("")
  155. }
  156. if(this && this.starRender && typeof(this.starRender)=='function'){
  157. this.starRender()//截图成功后,启动渲染
  158. }
  159. },
  160. //上传图片
  161. async upload(filePath, cb = null) {
  162. var fileName = "4DImage/" + util.formatDate(new Date(), "yyyyMMddhhmmss") + '.jpg';
  163. let tokenObj = await requestConfig("getUploadToken", {});
  164. console.log("tokenObj:", tokenObj);
  165. var token = tokenObj.single.token;
  166. var resultUrl = tokenObj.single.resultUrl; //上传图片的访问前缀this.resultUrl =
  167. // let currUniPage = getCurrentPages()[getCurrentPages().length - 1].$vm;
  168. uni.uploadFile({
  169. url: "https://up.qiniup.com",
  170. filePath: filePath,
  171. name: "file",
  172. formData: {
  173. key: fileName,
  174. token: token,
  175. },
  176. success: (uploadFileRes) => {
  177. let obj = JSON.parse(uploadFileRes.data);
  178. let shottingImg = resultUrl + obj.key;
  179. console.warn("***截图图片***", shottingImg);
  180. if (cb ) {
  181. cb(shottingImg)
  182. }
  183. },
  184. fail: (error) => {
  185. cb("")
  186. },
  187. complete(e) {
  188. if(this && this.starRender && typeof(this.starRender)=='function'){
  189. this.starRender()//截图成功后,启动渲染
  190. }
  191. }
  192. });
  193. },
  194. }
  195. }