cropper.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // pages/piano/cropper/cropper.js
  2. const { BtCmd } = require('../../../devices/bluetooth/bt_cmd');
  3. import EventManager from '../../../utils/event_bus'
  4. import { EnumCmdEvent, CmdEvent } from '../../../devices/cmd_key_event';
  5. const { BtHelper } = require('../../../devices/bt_helper');
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. src: "",
  12. width: 300,//宽度
  13. height: 300,//高度
  14. max_width: 300,
  15. max_height: 300,
  16. disable_rotate: true, //是否禁用旋转
  17. disable_ratio: true, //锁定比例
  18. limit_move: true, //是否限制移动
  19. showProgress: false,
  20. _imageBuffer: null,
  21. progress: 0,
  22. },
  23. cropper: null,
  24. // 将 RGBA 数据转换为 RGB565 格式
  25. RGBAtoRGB565(data) {
  26. const rgb565Array = new Uint16Array(data.length / 4);
  27. for (let i = 0; i < data.length; i += 4) {
  28. const r = data[i];
  29. const g = data[i + 1];
  30. const b = data[i + 2];
  31. // 转换为 RGB565
  32. const rgb565 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  33. rgb565Array[i / 4] = rgb565;
  34. }
  35. return rgb565Array;
  36. },
  37. // 保存为 .bin 文件
  38. saveAsBinFile(data, width, height) {
  39. const arrayBuffer = data.buffer;
  40. wx.getFileSystemManager().writeFile({
  41. filePath: `${wx.env.USER_DATA_PATH}/image_rgb565_${width}x${height}.bin`,
  42. data: arrayBuffer,
  43. encoding: 'binary',
  44. success: () => {
  45. wx.showToast({
  46. title: '保存成功',
  47. icon: 'success',
  48. });
  49. },
  50. fail: (err) => {
  51. console.error('文件保存失败:', err);
  52. },
  53. });
  54. },
  55. cropperload(e) {
  56. console.log('cropper加载完成');
  57. },
  58. upload() {
  59. let that = this;
  60. wx.chooseImage({
  61. count: 1,
  62. sizeType: ['original', 'compressed'],
  63. sourceType: ['album'],
  64. success(res) {
  65. wx.showLoading({
  66. title: '加载中',
  67. })
  68. const tempFilePaths = res.tempFilePaths[0];
  69. //重置图片角度、缩放、位置
  70. that.cropper.imgReset();
  71. that.setData({
  72. src: tempFilePaths
  73. });
  74. }
  75. })
  76. },
  77. loadimage(e) {
  78. wx.hideLoading();
  79. console.log('图片', e);
  80. this.cropper.imgReset();
  81. },
  82. clickcut(e) {
  83. console.log("clickcut:", e.detail);
  84. //图片预览
  85. wx.previewImage({
  86. current: e.detail.url, // 当前显示图片的http链接
  87. urls: [e.detail.url] // 需要预览的图片http链接列表
  88. })
  89. },
  90. cancel() {
  91. wx.navigateBack({
  92. delta: -1
  93. })
  94. },
  95. submit() {
  96. let _this = this
  97. wx.showLoading({
  98. title: '图片裁剪中',
  99. })
  100. this.cropper.getImg((obj) => {
  101. // app.globalData.imgSrc = obj.url;
  102. console.log("裁剪图片:", obj);
  103. _this.readLocalFileAndConvertToBase64(obj.url);
  104. });
  105. },
  106. // 读取本地文件并转换为 Base64 字符串
  107. readLocalFileAndConvertToBase64(filePath) {
  108. const fs = wx.getFileSystemManager();
  109. let _this = this;
  110. fs.readFile({
  111. filePath: filePath,
  112. encoding: 'base64',
  113. success: (res) => {
  114. // const base64Data = 'data:image/png;base64,' + res.data;
  115. // let rgbData = _this.RGBAtoRGB565(res.data)
  116. let rgbData = res.data
  117. console.log("转换rgb:", rgbData.length)
  118. _this.data._imageBuffer = rgbData;
  119. wx.hideLoading();
  120. wx.showLoading({
  121. title: '开始传输图片',
  122. })
  123. _this.startImage();
  124. },
  125. fail: (err) => {
  126. console.error('读取文件失败:', err);
  127. }
  128. });
  129. },
  130. startImage() {
  131. BtHelper.getInstance().send(BtCmd.wallPaper(1));
  132. },
  133. sliceDataIntoChunks(data, chunkSize) {
  134. const chunks = [];
  135. for (let i = 0; i < data.length; i += chunkSize) {
  136. const chunk = data.slice(i, i + chunkSize);
  137. chunks.push(chunk);
  138. }
  139. return chunks;
  140. },
  141. startSendImage(imageBuffer) {
  142. if (imageBuffer == null) {
  143. wx.showToast({
  144. title: '图片裁剪失败',
  145. icon: 'none'
  146. })
  147. _this.endImage(2)
  148. return;
  149. }
  150. // _this.sendImage(imageBuffer, 0)
  151. // }
  152. },
  153. async sendImage(imageBuffer, index) {
  154. let _this = this
  155. // if (index >= chunkSize) {
  156. // wx.showModal({
  157. // title: '图片上传成功1',
  158. // showCancel: false
  159. // })
  160. // _this.endImage(0)
  161. // return;
  162. // }
  163. let chunks = this.sliceDataIntoChunks(imageBuffer, 20);
  164. let next = 0;
  165. let total = imageBuffer.length;
  166. for (let i = 0; i < chunks.length; i++) {
  167. const chunk = chunks[i];
  168. next += chunk.length;
  169. console.log("发送图片数据:", i, next, chunk)
  170. let res = await BtHelper.getInstance().wallPaperSyncData(chunk);
  171. let nowDate = Date.now()
  172. if (i === chunks.length - 1 && res) {
  173. wx.showModal({
  174. title: '图片上传成功' + i + " / " + nowDate,
  175. showCancel: false
  176. })
  177. _this.endImage(0)
  178. } else if (!res) {
  179. wx.showModal({
  180. title: '图片上传失败了',
  181. showCancel: false
  182. })
  183. _this.endImage(2)
  184. break;
  185. }
  186. _this.updateProgress(next, total);
  187. }
  188. },
  189. endImage(value) {
  190. BtHelper.getInstance().wallPaper(value);
  191. },
  192. startProgress() {
  193. this.setData({
  194. progress: 0,
  195. showProgress: true
  196. })
  197. },
  198. updateProgress(chunk, total) {
  199. let progress = chunk / total;
  200. let _this = this
  201. if (chunk >= total) {
  202. _this.setData({
  203. progress: 0,
  204. showProgress: false,
  205. // showCropImg: false
  206. });
  207. wx.showToast({
  208. title: '图片上传成功',
  209. })
  210. _this.endImage(0)
  211. } else {
  212. _this.setData({
  213. progress: progress,
  214. });
  215. }
  216. },
  217. /**
  218. * 生命周期函数--监听页面加载
  219. */
  220. onLoad(options) {
  221. console.log(options.param)
  222. // let json = JSON.parse(options.param)
  223. this.cropper = this.selectComponent("#image-cropper");
  224. this.cropper.imgReset();
  225. // this.setData({
  226. // scr:json,
  227. // })
  228. this.upload(); //上传图片
  229. let _this = this;
  230. EventManager.addNotification(CmdEvent.eventName, function (event) {
  231. let name = event.cmdEvent;
  232. console.log("裁剪页:", EnumCmdEvent)
  233. switch (name) {
  234. case EnumCmdEvent.wallpaper:
  235. let otaCmd = event.wallpaper;
  236. let kind = event.heiJiaoKind;
  237. console.log("裁剪页:", otaCmd, kind)
  238. if (otaCmd === 1 && kind == 1) {
  239. // 开始发送
  240. _this.sendImage(_this.data._imageBuffer, 0)
  241. _this.startProgress()
  242. } else if (otaCmd === 0 && kind == 1) {
  243. // 发送结束
  244. // _this.endImage(0)
  245. } else if (kind == 0) {
  246. wx.hideLoading()
  247. wx.showModal({
  248. title: '图片上传失败了',
  249. showCancel: false
  250. })
  251. }
  252. break;
  253. default:
  254. break;
  255. }
  256. }, this)
  257. },
  258. /**
  259. * 生命周期函数--监听页面初次渲染完成
  260. */
  261. onReady() {
  262. },
  263. /**
  264. * 生命周期函数--监听页面显示
  265. */
  266. onShow() {
  267. },
  268. /**
  269. * 生命周期函数--监听页面隐藏
  270. */
  271. onHide() {
  272. },
  273. /**
  274. * 生命周期函数--监听页面卸载
  275. */
  276. onUnload() {
  277. EventManager.removeNotification(CmdEvent.eventName, this);
  278. },
  279. /**
  280. * 页面相关事件处理函数--监听用户下拉动作
  281. */
  282. onPullDownRefresh() {
  283. },
  284. /**
  285. * 页面上拉触底事件的处理函数
  286. */
  287. onReachBottom() {
  288. },
  289. /**
  290. * 用户点击右上角分享
  291. */
  292. onShareAppMessage() {
  293. }
  294. })