wallpaper.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 { EnumCmdEvent, CmdEvent } from '../../../devices/cmd_key_event';
  11. // import EventManager from '../../utils/event_bus'
  12. Page({
  13. /**
  14. * 页面的初始数据
  15. */
  16. data: {
  17. topImg:
  18. {
  19. // pic: "",
  20. // id: "",
  21. // name: ""
  22. },
  23. imageList: [
  24. ],
  25. selectIndex: 0,
  26. navbarData: {
  27. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  28. title: '壁纸设置', //导航栏 中间的标题
  29. callback() {
  30. if (this._imageBuffer) {
  31. wx.showModal({
  32. title: '保存图片中,确定要中断退出吗?',
  33. content: '',
  34. complete: (res) => {
  35. if (res.cancel) {
  36. }
  37. if (res.confirm) {
  38. this.endImage(2)
  39. }
  40. }
  41. })
  42. } else {
  43. wx.navigateBack({
  44. delta: 1,
  45. })
  46. }
  47. }
  48. },
  49. src: '',
  50. showProgress: false,
  51. showCropImg: false,
  52. width: 250,//宽度
  53. height: 250,//高度
  54. _imageBuffer: null,
  55. progressPercent: 0,
  56. _timer: null,
  57. },
  58. closePage() {
  59. },
  60. footerTap() {
  61. const that = this;
  62. wx.chooseMedia({
  63. count: 1,
  64. mediaType: ['image'],
  65. sourceType: ['album'],
  66. // camera: 'back',
  67. success(res) {
  68. console.log(res.tempFiles[0].tempFilePath)
  69. console.log(res.tempFiles[0].size)
  70. // that.setData({
  71. // imageSrc: res.tempFiles[0].tempFilePath, // 图片路径
  72. // showCrop: true
  73. // })
  74. // wx.editImage({
  75. // src: res.tempFiles[0].tempFilePath, // 图片路径
  76. // })
  77. wx.getImageInfo({
  78. src: res.tempFiles[0].tempFilePath,
  79. success(res) {
  80. console.log(res)
  81. wx.cropImage({
  82. src: res.path,// 图片路径
  83. cropScale: '1:1', // 裁剪比例
  84. success(res) {
  85. console.log("裁剪后的图片", res)
  86. that.convertImageToRGB565(res.tempFilePath)
  87. }
  88. })
  89. }
  90. })
  91. }
  92. })
  93. },
  94. // 将图片转换为 RGB565
  95. convertImageToRGB565(imagePath) {
  96. const ctx = wx.createCanvasContext('imageCanvas');
  97. // const ctx = Canvas.getContext('imageCanvas');
  98. let _this = this;
  99. let width = 400;
  100. let height = 400;
  101. try {
  102. // 设置 Canvas 尺寸
  103. // _this.setData({ canvasWidth: width, canvasHeight: height });
  104. // 在 Canvas 中绘制图片
  105. ctx.drawImage(imagePath, 0, 0, width, height);
  106. ctx.draw(false, () => {
  107. // 获取图片像素数据
  108. wx.canvasGetImageData({
  109. canvasId: 'imageCanvas',
  110. x: 0,
  111. y: 0,
  112. width,
  113. height,
  114. success: (res) => {
  115. console.log('获取像素数据成功:', res.data);
  116. if (res.data) {
  117. _this.setData({
  118. showCropImg: true,
  119. topImg: { "pic": imagePath }
  120. })
  121. const rgb565Data = _this.RGBAtoRGB565(res.data);
  122. _this.data.imageBuffer = rgb565Data;
  123. }
  124. // this.saveAsBinFile(rgb565Data, width, height);
  125. },
  126. fail: (err) => {
  127. console.error('获取像素数据失败:', err);
  128. },
  129. });
  130. });
  131. } catch (error) {
  132. console.log(error)
  133. }
  134. },
  135. // 将 RGBA 数据转换为 RGB565 格式
  136. RGBAtoRGB565(data) {
  137. const rgb565Array = new Uint16Array(data.length / 4);
  138. for (let i = 0; i < data.length; i += 4) {
  139. const r = data[i];
  140. const g = data[i + 1];
  141. const b = data[i + 2];
  142. // 转换为 RGB565
  143. const rgb565 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  144. rgb565Array[i / 4] = rgb565;
  145. }
  146. return rgb565Array;
  147. },
  148. // 保存为 .bin 文件
  149. saveAsBinFile(data, width, height) {
  150. const arrayBuffer = data.buffer;
  151. wx.getFileSystemManager().writeFile({
  152. filePath: `${wx.env.USER_DATA_PATH}/image_rgb565_${width}x${height}.bin`,
  153. data: arrayBuffer,
  154. encoding: 'binary',
  155. success: () => {
  156. wx.showToast({
  157. title: '保存成功',
  158. icon: 'success',
  159. });
  160. },
  161. fail: (err) => {
  162. console.error('文件保存失败:', err);
  163. },
  164. });
  165. },
  166. imageTap(e) {
  167. // console.log(e)
  168. // wxfile://tmp_d3e57489ead39c698676ff860df9cb8a37f66ee1a4777dbb.jpg
  169. let image = e.currentTarget.dataset.image;
  170. _this.setData({
  171. topImg: image ?? {}
  172. })
  173. }, wallpaperList() {
  174. let _this = this;
  175. deviceWallPaper().then(res => {
  176. console.log("壁纸列表", res);
  177. let data = res.data ?? []
  178. if (data.length == 0) {
  179. _this.setData({
  180. imageList: [],
  181. topImg: {},
  182. })
  183. return;
  184. }
  185. topImg = data[0];
  186. _this.setData({
  187. imageList: data ?? [],
  188. topImg: data[0] ?? {}
  189. })
  190. })
  191. },
  192. hideCut(e) {
  193. const img = arguments[0].detail
  194. if (img && img.path) {
  195. console.log("裁剪图片:", img)
  196. }
  197. this.setData({
  198. imageSrc: "",//要裁剪的图片
  199. showCrop: false
  200. })
  201. },
  202. startImage() {
  203. BtHelper.sendData(BtCmd.wallPaper(1));
  204. },
  205. sendImage(imageBuffer) {
  206. if (imageBuffer == null) {
  207. wx.showToast({
  208. title: '图片裁剪失败',
  209. icon: 'none'
  210. })
  211. _this.endImage(2)
  212. return;
  213. }
  214. let chunkSize = 20;
  215. let _this = this
  216. _this.setData({
  217. progress: 0,
  218. progressPercent: 0
  219. });
  220. for (let i = 0; i < imageBuffer.length; i += chunkSize) {
  221. const chunk = imageBuffer.slice(i, i + chunkSize);
  222. BtHelper.wallPaperData(chunk)
  223. if (i >= chunkSize) {
  224. _this.endImage(0)
  225. }
  226. }
  227. },
  228. endImage(value) {
  229. BtHelper.sendData(BtCmd.wallPaper(value));
  230. },
  231. startProgress() {
  232. let _this = this;
  233. this.setData({
  234. showProgress: true
  235. })
  236. // 定时器,每100毫秒执行一次
  237. let progress = 0;
  238. _this._timer = setInterval(function () {
  239. if (progress >= 100) {
  240. clearInterval(_this._timer); // 停止定时器
  241. _this.setData({
  242. progress: 0,
  243. progressPercent: 0,
  244. showProgress: false,
  245. showCropImg: false
  246. });
  247. wx.showToast({
  248. title: '图片上传成功',
  249. })
  250. } else {
  251. progress += 1; // 每次增加1%
  252. _this.setData({
  253. progress: progress,
  254. progressPercent: progress
  255. });
  256. }
  257. }, 100);
  258. },
  259. /**
  260. * 生命周期函数--监听页面加载
  261. */
  262. onLoad(options) {
  263. let param = JSON.parse(options.param)
  264. console.log(param)
  265. this.wallpaperList();
  266. let _this = this;
  267. EventManager.addNotification(CmdEvent.eventName, function (event) {
  268. let name = event.cmdEvent;
  269. console.log("壁纸页:", event)
  270. switch (name) {
  271. case EnumCmdEvent.wallPaper:
  272. let otaCmd = event.wallPaper;
  273. let kind = event.kind;
  274. if (otaCmd === 1 && kind == 1) {
  275. // 开始发送
  276. _this.sendImage(_this.data.imageBuffer)
  277. _this.startProgress()
  278. } else if (otaCmd === 0 && kind == 1) {
  279. // 发送结束
  280. _this.endEnd()
  281. }
  282. break;
  283. }
  284. }, this)
  285. },
  286. /**
  287. * 生命周期函数--监听页面初次渲染完成
  288. */
  289. onReady() {
  290. },
  291. /**
  292. * 生命周期函数--监听页面显示
  293. */
  294. onShow() {
  295. },
  296. /**
  297. * 生命周期函数--监听页面隐藏
  298. */
  299. onHide() {
  300. },
  301. /**
  302. * 生命周期函数--监听页面卸载
  303. */
  304. onUnload() {
  305. EventManager.removeNotification(CmdEvent.eventName, this);
  306. },
  307. /**
  308. * 页面相关事件处理函数--监听用户下拉动作
  309. */
  310. onPullDownRefresh() {
  311. },
  312. /**
  313. * 页面上拉触底事件的处理函数
  314. */
  315. onReachBottom() {
  316. },
  317. /**
  318. * 用户点击右上角分享
  319. */
  320. onShareAppMessage() {
  321. }
  322. })