MaskPhotoView.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. imageNode: cc.Node,
  5. },
  6. distance(a, b) {
  7. let _x = b.x - a.x;
  8. let _y = b.y - a.y;
  9. return Math.sqrt(_x * _x + _y * _y);
  10. },
  11. onLoad() {
  12. this.maskEdge = 600;
  13. this.touch1 = null;
  14. this.touch2 = null;
  15. this.scaleRate = 0;
  16. this.lastRate = 1;
  17. this.distance1 = 0;
  18. this.distance2 = 0;
  19. this.node.on(cc.Node.EventType.TOUCH_START, (event) => {
  20. this.lastRate += this.scaleRate;
  21. this.targetPos = null;
  22. this.touch2 = null;
  23. this.touch1 = event.getLocation();
  24. this.lastMovePos = event.getLocation();
  25. })
  26. this.node.on(cc.Node.EventType.TOUCH_MOVE, (event) => {
  27. let location = event.getLocation();
  28. this.touch1 = location;
  29. if (event._touches.length >= 2) {
  30. if (!this.touch2) {
  31. this.touch2 = event._touches[1]._point;
  32. this.distance1 = this.distance(this.touch1, this.touch2);
  33. } else {
  34. // this.touch1 = event._touches[0]._point;
  35. // this.touch2 = event._touches[1]._point;
  36. // this.distance2 = this.distance(this.touch1, this.touch2);
  37. // let rate1 = Math.abs(this.distance2 - this.distance1) / 300;
  38. // let rate2 = rate1 + this.scaleRate;
  39. // if (rate2 < 0 || rate2 > 3) { return; }
  40. // this.scaleRate = rate1;
  41. // this.imageNode.setScale(rate2);
  42. }
  43. } else {
  44. let vec = location.sub(this.lastMovePos);
  45. if (!this.targetPos) { this.targetPos = this.imageNode.position; }
  46. else { this.targetPos = this.targetPos.add(vec); }
  47. this.lastMovePos = location;
  48. }
  49. })
  50. },
  51. update() {
  52. if (this.targetPos) {
  53. let posX = this.imageNode.position.x;
  54. let posY = this.imageNode.position.y
  55. posX = cc.misc.lerp(posX, this.targetPos.x, 0.25);
  56. posY = cc.misc.lerp(posY, this.targetPos.y, 0.25);
  57. let offsetX = Math.abs(this.imageNode.width / 2 - this.maskEdge / 2);
  58. let offsetY = Math.abs(this.imageNode.height / 2 - this.maskEdge / 2);
  59. if (posX > offsetX) {
  60. posX = offsetX;
  61. } else if (posX < -offsetX) {
  62. posX = -offsetX;
  63. } if (posY > offsetY) {
  64. posY = offsetY;
  65. } else if (posY < -offsetY) {
  66. posY = -offsetY;
  67. }
  68. this.imageNode.position = cc.v2(posX, posY);
  69. }
  70. },
  71. onDestroy() {
  72. if (this.tempTexture) {
  73. cc.loader.release(this.tempTexture);
  74. this.tempTexture = null;
  75. }
  76. },
  77. initPhoto: function (path, head_img) {
  78. this.head_img = head_img;
  79. this.lastMovePos = cc.v2(0, 0);
  80. this.imageNode.position = cc.v2(0, 0);
  81. let sprite = this.imageNode.getComponent(cc.Sprite);
  82. cc.loader.load(path, (err, tex) => {
  83. if (err) {
  84. cc.error(err);
  85. } else {
  86. this.maskEdge = 600;
  87. this.touch1 = null;
  88. this.touch2 = null;
  89. this.scaleRate = 0;
  90. this.lastRate = 1;
  91. this.distance1 = 0;
  92. this.distance2 = 0;
  93. this.tempTexture = tex;
  94. let width = tex.width;
  95. let height = tex.height;
  96. let spriteFrame = new cc.SpriteFrame(tex);
  97. let scale = 0;
  98. if (width < this.maskEdge && height > this.maskEdge) {
  99. scale = width / this.maskEdge;
  100. height *= scale;
  101. } else if (width > this.maskEdge && height < this.maskEdge) {
  102. scale = height / this.maskEdge;
  103. width *= scale;
  104. } else if (width < this.maskEdge && width > height) {
  105. scale = height / this.maskEdge;
  106. width *= scale;
  107. height *= scale;
  108. } else if (height < this.maskEdge && width < height) {
  109. scale = width / this.maskEdge;
  110. width *= scale;
  111. height *= scale;
  112. } else if (width > this.maskEdge && height > this.maskEdge && width > height) {
  113. scale = this.maskEdge / height;
  114. width *= scale;
  115. height *= scale;
  116. } else if (height > this.maskEdge && height > this.maskEdge && width < height) {
  117. scale = this.maskEdge / width;
  118. width *= scale;
  119. height *= scale;
  120. }
  121. this.imageNode.width = width;
  122. this.imageNode.height = height;
  123. sprite.spriteFrame = spriteFrame;
  124. }
  125. });
  126. },
  127. onBtnConfirm: function () {
  128. let node = new cc.Node();
  129. node.parent = cc.director.getScene();
  130. let camera = node.addComponent(cc.Camera);
  131. camera.cullingMask = 0xffffffff;
  132. let visibleSize = cc.view.getVisibleSize();
  133. node.x = visibleSize.width / 2;
  134. node.y = visibleSize.height / 2;
  135. //修改camera的参数可以控制部分截图,详情请参看官方文档
  136. camera.alignWithScreen = false;
  137. camera.orthoSize = 200;
  138. camera.position = cc.v2(200, 200);
  139. let width = this.maskEdge;
  140. let height = this.maskEdge;
  141. let texture = new cc.RenderTexture();
  142. texture.initWithSize(width, height, cc.game._renderContext.STENCIL_INDEX8);
  143. camera.targetTexture = texture;
  144. camera.render();
  145. let data = texture.readPixels();
  146. let picData = new Uint8Array(width * height * 4);
  147. let rowBytes = width * 4;
  148. for (let row = 0; row < height; row++) {
  149. let srow = height - 1 - row;
  150. let start = srow * width * 4;
  151. let reStart = row * width * 4;
  152. for (let i = 0; i < rowBytes; i++) {
  153. picData[reStart + i] = data[start + i];
  154. }
  155. }
  156. let filePath = jsb.fileUtils.getWritablePath() + 'uploadHead.png';
  157. let success = jsb.saveImageData(picData, width, height, filePath);
  158. if (success) {
  159. cc.loader.load(filePath, (err, tex) => {
  160. if (err) {
  161. cc.error(err);
  162. } else {
  163. let sprite = this.head_img;
  164. this.tempTexture = tex;
  165. let spriteFrame = new cc.SpriteFrame(tex);
  166. sprite.spriteFrame = spriteFrame;
  167. setTimeout(() => {
  168. let data = jsb.fileUtils.getDataFromFile(filePath);
  169. let sendData = "image=" + this._arrayBufferToBase64(data) + "&UserID=" + cc.vv.globalUserInfo.getUserID();
  170. let xhr = new XMLHttpRequest();
  171. xhr.onload = () => { cc.log("成功") }
  172. xhr.onerror = () => { cc.log("失败") }
  173. xhr.onreadystatechange = () => {
  174. cc.log(xhr.status)
  175. if (xhr.readyState !== 4) { return; }
  176. if (xhr.status === 200) {
  177. cc.log(JSON.parse(xhr.responseText).data);
  178. }
  179. };
  180. xhr.open("POST", "http://8.129.91.11:801/api/upload/upload_file");
  181. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  182. xhr.send(sendData);
  183. this.node.destroy();
  184. }, 100);
  185. }
  186. });
  187. }
  188. else {
  189. cc.error("save image data failed!");
  190. }
  191. },
  192. _arrayBufferToBase64(bytes) {
  193. var base64 = '';
  194. var encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  195. var byteLength = bytes.byteLength;
  196. var byteRemainder = byteLength % 3;
  197. var mainLength = byteLength - byteRemainder;
  198. var a, b, c, d;
  199. var chunk;
  200. // Main loop deals with bytes in chunks of 3
  201. for (var i = 0; i < mainLength; i = i + 3) {
  202. // Combine the three bytes into a single integer
  203. chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
  204. // Use bitmasks to extract 6-bit segments from the triplet
  205. a = (chunk & 16515072) >> 18; // 16515072 = (2^6 - 1) << 18
  206. b = (chunk & 258048) >> 12; // 258048 = (2^6 - 1) << 12
  207. c = (chunk & 4032) >> 6; // 4032 = (2^6 - 1) << 6
  208. d = chunk & 63; // 63 = 2^6 - 1
  209. // Convert the raw binary segments to the appropriate ASCII encoding
  210. base64 += encodings[a] + encodings[b] + encodings[c] + encodings[d];
  211. }
  212. // Deal with the remaining bytes and padding
  213. if (byteRemainder == 1) {
  214. chunk = bytes[mainLength];
  215. a = (chunk & 252) >> 2 // 252 = (2^6 - 1) << 2;
  216. // Set the 4 least significant bits to zero
  217. b = (chunk & 3) << 4 // 3 = 2^2 - 1;
  218. base64 += encodings[a] + encodings[b] + '==';
  219. }
  220. else if (byteRemainder == 2) {
  221. chunk = (bytes[mainLength] << 8) | bytes[mainLength + 1];
  222. a = (chunk & 16128) >> 8 // 16128 = (2^6 - 1) << 8;
  223. b = (chunk & 1008) >> 4 // 1008 = (2^6 - 1) << 4;
  224. // Set the 2 least significant bits to zero
  225. c = (chunk & 15) << 2 // 15 = 2^4 - 1;
  226. base64 += encodings[a] + encodings[b] + encodings[c] + '=';
  227. }
  228. return "data:image/jpeg;base64," + base64;
  229. // return base64;
  230. },
  231. });