wallpaper.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // pages/piano/wallpaper/wallpaper.js
  2. const { deviceWallPaper } = require('../../../request/deviceListRequest')
  3. // const sharp = require('sharp');
  4. // const fs = require('fs');
  5. const {
  6. BtHelper
  7. } = require('../../../devices/bt_helper');
  8. const { BtCmd } = require('../../../devices/bluetooth/bt_cmd');
  9. import EventManager from '../../../utils/event_bus'
  10. // import EventManager from '../../utils/event_bus'
  11. Page({
  12. /**
  13. * 页面的初始数据
  14. */
  15. data: {
  16. topImg: {
  17. pic: "wxfile://tmp_d3e57489ead39c698676ff860df9cb8a37f66ee1a4777dbb.jpg",
  18. id: "",
  19. name: ""
  20. },
  21. imageList: [
  22. {
  23. pic: "https://inews.gtimg.com/om_bt/OAVMydtx9BsJxf5i_thi4Oll9sR1px-Esmtv6UHSxoisEAA/641",
  24. id: "",
  25. name: ""
  26. },
  27. {
  28. pic: "https://inews.gtimg.com/om_bt/OAVMydtx9BsJxf5i_thi4Oll9sR1px-Esmtv6UHSxoisEAA/641",
  29. id: "",
  30. name: ""
  31. },
  32. {
  33. pic: "https://inews.gtimg.com/om_bt/OAVMydtx9BsJxf5i_thi4Oll9sR1px-Esmtv6UHSxoisEAA/641",
  34. id: "",
  35. name: ""
  36. },
  37. {
  38. pic: "https://inews.gtimg.com/om_bt/OAVMydtx9BsJxf5i_thi4Oll9sR1px-Esmtv6UHSxoisEAA/641",
  39. id: "",
  40. name: ""
  41. },
  42. {
  43. pic: "https://inews.gtimg.com/om_bt/OAVMydtx9BsJxf5i_thi4Oll9sR1px-Esmtv6UHSxoisEAA/641",
  44. id: "",
  45. name: ""
  46. }
  47. ],
  48. selectIndex: 0,
  49. nvabarData: {
  50. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  51. title: '壁纸设置', //导航栏 中间的标题
  52. },
  53. src: '',
  54. width: 250,//宽度
  55. height: 250,//高度
  56. _imageBuffer: null,
  57. }, footerTap() {
  58. const that = this;
  59. wx.chooseImage({
  60. count: 1, // 最多可以选择的图片张数
  61. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  62. sourceType: ['album'], // 从相册选择
  63. success: function (res) {
  64. const tempFilePaths = res.tempFilePaths;
  65. console.log('获取图片信息成功', res);
  66. wx.cropImage({
  67. src: tempFilePaths[0], // 图片路径
  68. cropScale: '1:1', // 裁剪比例
  69. success: (res) => {
  70. console.log('裁剪成功', res);
  71. that.setData({
  72. src: res.tempFilePath
  73. })
  74. }
  75. })
  76. // wx.getImageInfo({
  77. // src: tempFilePaths[0],
  78. // success: function (imageInfo) {
  79. // console.log(imageInfo);
  80. // //获取到image-cropper实例
  81. // //开始裁剪
  82. // // that.setData({
  83. // // src: imageInfo.path,//要裁剪的图片
  84. // // })
  85. // // wx.navigateTo({
  86. // // url: `/pages/crop/crop?path=${tempFilePaths[0]}&width=${imageInfo.width}&height=${imageInfo.height}`
  87. // // });
  88. // },
  89. // fail: function (err) {
  90. // console.error('获取图片信息失败', err);
  91. // }
  92. // });
  93. },
  94. fail: function (err) {
  95. console.error('选择图片失败', err);
  96. }
  97. });
  98. },
  99. async convertToRGB565(inputPath, outputPath) {
  100. try {
  101. // todo
  102. let sharp = null
  103. const metadata = await sharp(inputPath).metadata();
  104. const { width, height } = metadata;
  105. const buffer = await sharp(inputPath)
  106. .raw()
  107. .toBuffer();
  108. const outputBuffer = Buffer.alloc(width * height * 2); // 2 bytes per pixel for RGB565
  109. for (let y = 0; y < height; y++) {
  110. for (let x = 0; x < width; x++) {
  111. const index = (y * width + x) * 4; // RGBA
  112. const r = buffer[index];
  113. const g = buffer[index + 1];
  114. const b = buffer[index + 2];
  115. const r5 = (r >> 3) & 0x1F; // 5 bits for red
  116. const g6 = (g >> 2) & 0x3F; // 6 bits for green
  117. const b5 = (b >> 3) & 0x1F; // 5 bits for blue
  118. const rgb565 = (r5 << 11) | (g6 << 5) | b5;
  119. const outputIndex = (y * width + x) * 2;
  120. outputBuffer[outputIndex] = (rgb565 >> 8) & 0xFF; // High byte
  121. outputBuffer[outputIndex + 1] = rgb565 & 0xFF; // Low byte
  122. }
  123. }
  124. _this.data._imageBuffer = outputBuffer;
  125. _this.startImage()
  126. // fs.writeFileSync(outputPath, outputBuffer);
  127. console.log(`Conversion successful: ${outputPath}`);
  128. } catch (error) {
  129. console.error('Error converting image:', error);
  130. }
  131. },
  132. imageTap(e) {
  133. console.log(e)
  134. // wxfile://tmp_d3e57489ead39c698676ff860df9cb8a37f66ee1a4777dbb.jpg
  135. let image = e.currentTarget.dataset.image;
  136. _this.setData({
  137. topImg: image ?? {}
  138. })
  139. }, wallpaperList() {
  140. let _this = this;
  141. deviceWallPaper().then(res => {
  142. console.log("壁纸列表", res);
  143. let data = res.data ?? []
  144. if (data.length == 0) {
  145. _this.setData({
  146. imageList: [],
  147. topImg: {},
  148. })
  149. return;
  150. }
  151. topImg = data[0];
  152. _this.setData({
  153. imageList: data ?? [],
  154. topImg: data[0] ?? {}
  155. })
  156. })
  157. },
  158. startImage() {
  159. let _this = this;
  160. BtHelper.sendData(BtCmd.wallPaper(1));
  161. },
  162. sendImage(imageBuffer) {
  163. let chunkSize = 20;
  164. for (let i = 0; i < imageBuffer.length; i += chunkSize) {
  165. const chunk = imageBuffer.slice(i, i + chunkSize);
  166. // this.sendImage(chunk);
  167. BtHelper.wallPaperData(chunk)
  168. }
  169. },
  170. endImage() {
  171. BtHelper.sendData(BtCmd.wallPaper(0));
  172. },
  173. /**
  174. * 生命周期函数--监听页面加载
  175. */
  176. onLoad(options) {
  177. let param = JSON.parse(options.param)
  178. console.log(param)
  179. this.wallpaperList();
  180. let _this = this;
  181. EventManager.addNotification(CmdEvent.eventName, function (event) {
  182. let name = event.name;
  183. console.log("OTA页:", event)
  184. switch (name) {
  185. case EnumCmdEvent.wallPaper:
  186. let otaCmd = event.wallPaper;
  187. let kind = event.kind;
  188. if (otaCmd === 1 && kind == 1) {
  189. // 开始发送
  190. _this.sendOtaData()
  191. } else if (otaCmd === 0 && kind == 1) {
  192. // 发送结束
  193. _this.endEnd()
  194. }
  195. break;
  196. }
  197. }, this)
  198. },
  199. /**
  200. * 生命周期函数--监听页面初次渲染完成
  201. */
  202. onReady() {
  203. },
  204. /**
  205. * 生命周期函数--监听页面显示
  206. */
  207. onShow() {
  208. },
  209. /**
  210. * 生命周期函数--监听页面隐藏
  211. */
  212. onHide() {
  213. },
  214. /**
  215. * 生命周期函数--监听页面卸载
  216. */
  217. onUnload() {
  218. EventManager.removeNotification(CmdEvent.eventName, this);
  219. },
  220. /**
  221. * 页面相关事件处理函数--监听用户下拉动作
  222. */
  223. onPullDownRefresh() {
  224. },
  225. /**
  226. * 页面上拉触底事件的处理函数
  227. */
  228. onReachBottom() {
  229. },
  230. /**
  231. * 用户点击右上角分享
  232. */
  233. onShareAppMessage() {
  234. }
  235. })