wallpaper.js 5.8 KB

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