wallpaper.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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, // 图片路径
  66. cropScale: '1:1', // 裁剪比例
  67. success: (res) => {
  68. }
  69. })
  70. // wx.getImageInfo({
  71. // src: tempFilePaths[0],
  72. // success: function (imageInfo) {
  73. // console.log(imageInfo);
  74. // //获取到image-cropper实例
  75. // //开始裁剪
  76. // // that.setData({
  77. // // src: imageInfo.path,//要裁剪的图片
  78. // // })
  79. // // wx.navigateTo({
  80. // // url: `/pages/crop/crop?path=${tempFilePaths[0]}&width=${imageInfo.width}&height=${imageInfo.height}`
  81. // // });
  82. // },
  83. // fail: function (err) {
  84. // console.error('获取图片信息失败', err);
  85. // }
  86. // });
  87. },
  88. fail: function (err) {
  89. console.error('选择图片失败', err);
  90. }
  91. });
  92. },
  93. async convertToRGB565(inputPath, outputPath) {
  94. try {
  95. // todo
  96. let sharp = null
  97. const metadata = await sharp(inputPath).metadata();
  98. const { width, height } = metadata;
  99. const buffer = await sharp(inputPath)
  100. .raw()
  101. .toBuffer();
  102. const outputBuffer = Buffer.alloc(width * height * 2); // 2 bytes per pixel for RGB565
  103. for (let y = 0; y < height; y++) {
  104. for (let x = 0; x < width; x++) {
  105. const index = (y * width + x) * 4; // RGBA
  106. const r = buffer[index];
  107. const g = buffer[index + 1];
  108. const b = buffer[index + 2];
  109. const r5 = (r >> 3) & 0x1F; // 5 bits for red
  110. const g6 = (g >> 2) & 0x3F; // 6 bits for green
  111. const b5 = (b >> 3) & 0x1F; // 5 bits for blue
  112. const rgb565 = (r5 << 11) | (g6 << 5) | b5;
  113. const outputIndex = (y * width + x) * 2;
  114. outputBuffer[outputIndex] = (rgb565 >> 8) & 0xFF; // High byte
  115. outputBuffer[outputIndex + 1] = rgb565 & 0xFF; // Low byte
  116. }
  117. }
  118. _this.startImage()
  119. // fs.writeFileSync(outputPath, outputBuffer);
  120. console.log(`Conversion successful: ${outputPath}`);
  121. } catch (error) {
  122. console.error('Error converting image:', error);
  123. }
  124. },
  125. imageTap(e) {
  126. console.log(e)
  127. // wxfile://tmp_d3e57489ead39c698676ff860df9cb8a37f66ee1a4777dbb.jpg
  128. let image = e.currentTarget.dataset.image;
  129. _this.setData({
  130. topImg: image ?? {}
  131. })
  132. }, wallpaperList() {
  133. let _this = this;
  134. deviceWallPaper().then(res => {
  135. console.log("壁纸列表", res);
  136. topImg = res.data[0];
  137. _this.setData({
  138. imageList: res.data ?? [],
  139. topImg: res.data[0] ?? {}
  140. })
  141. })
  142. },
  143. startImage() {
  144. let _this = this;
  145. BtHelper.sendData(BtCmd.wallPaper(1));
  146. },
  147. sendImage(imageBuffer) {
  148. let chunkSize = 20;
  149. for (let i = 0; i < imageBuffer.length; i += chunkSize) {
  150. const chunk = imageBuffer.slice(i, i + chunkSize);
  151. // this.sendImage(chunk);
  152. BtHelper.wallPaperData(chunk)
  153. }
  154. },
  155. endImage() {
  156. BtHelper.sendData(BtCmd.wallPaper(0));
  157. },
  158. /**
  159. * 生命周期函数--监听页面加载
  160. */
  161. onLoad(options) {
  162. this.wallpaperList();
  163. EventMan
  164. },
  165. /**
  166. * 生命周期函数--监听页面初次渲染完成
  167. */
  168. onReady() {
  169. },
  170. /**
  171. * 生命周期函数--监听页面显示
  172. */
  173. onShow() {
  174. },
  175. /**
  176. * 生命周期函数--监听页面隐藏
  177. */
  178. onHide() {
  179. },
  180. /**
  181. * 生命周期函数--监听页面卸载
  182. */
  183. onUnload() {
  184. },
  185. /**
  186. * 页面相关事件处理函数--监听用户下拉动作
  187. */
  188. onPullDownRefresh() {
  189. },
  190. /**
  191. * 页面上拉触底事件的处理函数
  192. */
  193. onReachBottom() {
  194. },
  195. /**
  196. * 用户点击右上角分享
  197. */
  198. onShareAppMessage() {
  199. }
  200. })