cropper.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. this.cropper.getImg((obj) => {
  98. // app.globalData.imgSrc = obj.url;
  99. console.log("裁剪图片:", obj);
  100. _this.readLocalFileAndConvertToBase64(obj.url);
  101. });
  102. },
  103. // 读取本地文件并转换为 Base64 字符串
  104. readLocalFileAndConvertToBase64(filePath) {
  105. const fs = wx.getFileSystemManager();
  106. let _this = this;
  107. fs.readFile({
  108. filePath: filePath,
  109. encoding: 'base64',
  110. success: (res) => {
  111. // const base64Data = 'data:image/png;base64,' + res.data;
  112. let rgbData = _this.RGBAtoRGB565(res.data)
  113. console.log("转换rgb:", rgbData.length)
  114. _this.data._imageBuffer = rgbData;
  115. _this.startImage();
  116. },
  117. fail: (err) => {
  118. console.error('读取文件失败:', err);
  119. }
  120. });
  121. },
  122. startImage() {
  123. BtHelper.sendData(BtCmd.wallPaper(1));
  124. },
  125. sendImage(imageBuffer) {
  126. if (imageBuffer == null) {
  127. wx.showToast({
  128. title: '图片裁剪失败',
  129. icon: 'none'
  130. })
  131. _this.endImage(2)
  132. return;
  133. }
  134. let chunkSize = 20;
  135. let _this = this
  136. let total = imageBuffer.length
  137. for (let i = 0; i < total; i += chunkSize) {
  138. const chunk = imageBuffer.slice(i, i + chunkSize);
  139. BtHelper.wallPaperData(chunk)
  140. updateProgress(i, total);
  141. if (i >= chunkSize) {
  142. _this.endImage(0)
  143. }
  144. }
  145. },
  146. endImage(value) {
  147. BtHelper.sendData(BtCmd.wallPaper(value));
  148. },
  149. startProgress() {
  150. this.setData({
  151. progress: 0,
  152. showProgress: true
  153. })
  154. },
  155. updateProgress(chunk, total) {
  156. let progress = chunk / total * 100;
  157. let _this = this
  158. if (progress >= 100) {
  159. _this.setData({
  160. progress: 0,
  161. showProgress: false,
  162. // showCropImg: false
  163. });
  164. wx.showToast({
  165. title: '图片上传成功',
  166. })
  167. } else {
  168. _this.setData({
  169. progress: progress,
  170. });
  171. }
  172. },
  173. /**
  174. * 生命周期函数--监听页面加载
  175. */
  176. onLoad(options) {
  177. console.log(options.param)
  178. // let json = JSON.parse(options.param)
  179. this.cropper = this.selectComponent("#image-cropper");
  180. this.cropper.imgReset();
  181. // this.setData({
  182. // scr:json,
  183. // })
  184. this.upload(); //上传图片
  185. let _this = this;
  186. EventManager.addNotification(CmdEvent.eventName, function (event) {
  187. let name = event.cmdEvent;
  188. console.log("壁纸页:", event)
  189. switch (name) {
  190. case EnumCmdEvent.wallPaper:
  191. let otaCmd = event.wallPaper;
  192. let kind = event.kind;
  193. if (otaCmd === 1 && kind == 1) {
  194. // 开始发送
  195. _this.sendImage(_this.data._imageBuffer)
  196. _this.startProgress()
  197. } else if (otaCmd === 0 && kind == 1) {
  198. // 发送结束
  199. _this.endImage(0)
  200. }
  201. break;
  202. default:
  203. break;
  204. }
  205. }, this)
  206. },
  207. /**
  208. * 生命周期函数--监听页面初次渲染完成
  209. */
  210. onReady() {
  211. },
  212. /**
  213. * 生命周期函数--监听页面显示
  214. */
  215. onShow() {
  216. },
  217. /**
  218. * 生命周期函数--监听页面隐藏
  219. */
  220. onHide() {
  221. },
  222. /**
  223. * 生命周期函数--监听页面卸载
  224. */
  225. onUnload() {
  226. EventManager.removeNotification(CmdEvent.eventName, this);
  227. },
  228. /**
  229. * 页面相关事件处理函数--监听用户下拉动作
  230. */
  231. onPullDownRefresh() {
  232. },
  233. /**
  234. * 页面上拉触底事件的处理函数
  235. */
  236. onReachBottom() {
  237. },
  238. /**
  239. * 用户点击右上角分享
  240. */
  241. onShareAppMessage() {
  242. }
  243. })