cropper.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. console.log("转换rgb:", rgbData.length)
  117. _this.data._imageBuffer = rgbData;
  118. wx.hideLoading();
  119. wx.showLoading({
  120. title: '开始传输图片',
  121. })
  122. _this.startImage();
  123. },
  124. fail: (err) => {
  125. console.error('读取文件失败:', err);
  126. }
  127. });
  128. },
  129. startImage() {
  130. BtHelper.getInstance().sendData(BtCmd.wallPaper(1));
  131. },
  132. startSendImage(imageBuffer) {
  133. if (imageBuffer == null) {
  134. wx.showToast({
  135. title: '图片裁剪失败',
  136. icon: 'none'
  137. })
  138. _this.endImage(2)
  139. return;
  140. }
  141. _this.sendImage(imageBuffer, 0)
  142. // }
  143. }, sendImage(imageBuffer, index) {
  144. let _this = this
  145. if (index >= chunkSize) {
  146. _this.endImage(0)
  147. return;
  148. }
  149. let chunkSize = 20;
  150. let total = imageBuffer.length
  151. let next = index + chunkSize;
  152. const chunk = imageBuffer.slice(index, next);
  153. BtHelper.getInstance().wallPaperData(chunk, function (res) {
  154. console.log("发送图片数据:", sendImage)
  155. _this.updateProgress(next, total);
  156. if (res) {
  157. _this.sendImage(imageBuffer)
  158. } else {
  159. wx.showModal({
  160. title: '图片上传失败了',
  161. showCancel: false
  162. })
  163. }
  164. })
  165. },
  166. endImage(value) {
  167. BtHelper.getInstance().wallPaper(value);
  168. },
  169. startProgress() {
  170. this.setData({
  171. progress: 0,
  172. showProgress: true
  173. })
  174. },
  175. updateProgress(chunk, total) {
  176. let progress = chunk / total * 100;
  177. let _this = this
  178. if (progress >= 100) {
  179. _this.setData({
  180. progress: 0,
  181. showProgress: false,
  182. // showCropImg: false
  183. });
  184. wx.showToast({
  185. title: '图片上传成功',
  186. })
  187. } else {
  188. _this.setData({
  189. progress: progress,
  190. });
  191. }
  192. },
  193. /**
  194. * 生命周期函数--监听页面加载
  195. */
  196. onLoad(options) {
  197. console.log(options.param)
  198. // let json = JSON.parse(options.param)
  199. this.cropper = this.selectComponent("#image-cropper");
  200. this.cropper.imgReset();
  201. // this.setData({
  202. // scr:json,
  203. // })
  204. this.upload(); //上传图片
  205. let _this = this;
  206. EventManager.addNotification(CmdEvent.eventName, function (event) {
  207. let name = event.cmdEvent;
  208. console.log("裁剪页:", EnumCmdEvent)
  209. switch (name) {
  210. case EnumCmdEvent.wallpaper:
  211. let otaCmd = event.wallpaper;
  212. let kind = event.heiJiaoKind;
  213. console.log("裁剪页:", otaCmd, kind)
  214. if (otaCmd === 1 && kind == 1) {
  215. // 开始发送
  216. _this.sendImage(_this.data._imageBuffer)
  217. _this.startProgress()
  218. } else if (otaCmd === 0 && kind == 1) {
  219. // 发送结束
  220. _this.endImage(0)
  221. } else if (kind == 0) {
  222. wx.hideLoading()
  223. wx.showModal({
  224. title: '图片上传失败了',
  225. showCancel: false
  226. })
  227. }
  228. break;
  229. default:
  230. break;
  231. }
  232. }, this)
  233. },
  234. /**
  235. * 生命周期函数--监听页面初次渲染完成
  236. */
  237. onReady() {
  238. },
  239. /**
  240. * 生命周期函数--监听页面显示
  241. */
  242. onShow() {
  243. },
  244. /**
  245. * 生命周期函数--监听页面隐藏
  246. */
  247. onHide() {
  248. },
  249. /**
  250. * 生命周期函数--监听页面卸载
  251. */
  252. onUnload() {
  253. EventManager.removeNotification(CmdEvent.eventName, this);
  254. },
  255. /**
  256. * 页面相关事件处理函数--监听用户下拉动作
  257. */
  258. onPullDownRefresh() {
  259. },
  260. /**
  261. * 页面上拉触底事件的处理函数
  262. */
  263. onReachBottom() {
  264. },
  265. /**
  266. * 用户点击右上角分享
  267. */
  268. onShareAppMessage() {
  269. }
  270. })