wallpaper.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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: true,
  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.setData({
  87. showCropImg: true,
  88. topImg: { "pic": res.tempFilePath }
  89. })
  90. that.convertImageToRGB565(res.tempFilePath)
  91. },
  92. })
  93. }
  94. })
  95. }
  96. })
  97. },
  98. // 将图片转换为 RGB565
  99. convertImageToRGB565(imagePath) {
  100. const ctx = wx.createCanvasContext('imageCanvas');
  101. // const ctx = Canvas.getContext('imageCanvas');
  102. let _this = this;
  103. let width = 533;
  104. let height = 533;
  105. try {
  106. // 设置 Canvas 尺寸
  107. // _this.setData({ canvasWidth: width, canvasHeight: height });
  108. // 在 Canvas 中绘制图片
  109. ctx.drawImage(imagePath, 0, 0, width, height);
  110. ctx.draw(false, () => {
  111. // 获取图片像素数据
  112. wx.canvasGetImageData({
  113. canvasId: 'imageCanvas',
  114. x: 0,
  115. y: 0,
  116. width,
  117. height,
  118. success: (res) => {
  119. console.log('获取像素数据成功:', res.data);
  120. if (res.data) {
  121. _this.setData({
  122. showCropImg: true,
  123. topImg: { "pic": imagePath }
  124. })
  125. const rgb565Data = _this.RGBAtoRGB565(res.data);
  126. _this.data.imageBuffer = rgb565Data;
  127. }
  128. // this.saveAsBinFile(rgb565Data, width, height);
  129. },
  130. fail: (err) => {
  131. console.error('获取像素数据失败:', err);
  132. },
  133. });
  134. });
  135. } catch (error) {
  136. console.log(error)
  137. }
  138. },
  139. // 将 RGBA 数据转换为 RGB565 格式
  140. RGBAtoRGB565(data) {
  141. const rgb565Array = new Uint16Array(data.length / 4);
  142. for (let i = 0; i < data.length; i += 4) {
  143. const r = data[i];
  144. const g = data[i + 1];
  145. const b = data[i + 2];
  146. // 转换为 RGB565
  147. const rgb565 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  148. rgb565Array[i / 4] = rgb565;
  149. }
  150. return rgb565Array;
  151. },
  152. // 保存为 .bin 文件
  153. saveAsBinFile(data, width, height) {
  154. const arrayBuffer = data.buffer;
  155. wx.getFileSystemManager().writeFile({
  156. filePath: `${wx.env.USER_DATA_PATH}/image_rgb565_${width}x${height}.bin`,
  157. data: arrayBuffer,
  158. encoding: 'binary',
  159. success: () => {
  160. wx.showToast({
  161. title: '保存成功',
  162. icon: 'success',
  163. });
  164. },
  165. fail: (err) => {
  166. console.error('文件保存失败:', err);
  167. },
  168. });
  169. },
  170. imageTap(e) {
  171. // console.log(e)
  172. // wxfile://tmp_d3e57489ead39c698676ff860df9cb8a37f66ee1a4777dbb.jpg
  173. let image = e.currentTarget.dataset.image;
  174. _this.setData({
  175. topImg: image ?? {}
  176. })
  177. }, wallpaperList() {
  178. let _this = this;
  179. deviceWallPaper().then(res => {
  180. console.log("壁纸列表", res);
  181. let data = res.data ?? []
  182. if (data.length == 0) {
  183. _this.setData({
  184. imageList: [],
  185. topImg: {},
  186. })
  187. return;
  188. }
  189. topImg = data[0];
  190. _this.setData({
  191. imageList: data ?? [],
  192. topImg: data[0] ?? {}
  193. })
  194. })
  195. },
  196. hideCut(e) {
  197. const img = arguments[0].detail
  198. if (img && img.path) {
  199. console.log("裁剪图片:", img)
  200. }
  201. this.setData({
  202. imageSrc: "",//要裁剪的图片
  203. showCrop: false
  204. })
  205. },
  206. startImage() {
  207. BtHelper.sendData(BtCmd.wallPaper(1));
  208. },
  209. sendImage(imageBuffer) {
  210. if (imageBuffer == null) {
  211. wx.showToast({
  212. title: '图片裁剪失败',
  213. icon: 'none'
  214. })
  215. _this.endImage(2)
  216. return;
  217. }
  218. let chunkSize = 20;
  219. let _this = this
  220. _this.setData({
  221. progress: 0,
  222. progressPercent: 0
  223. });
  224. for (let i = 0; i < imageBuffer.length; i += chunkSize) {
  225. const chunk = imageBuffer.slice(i, i + chunkSize);
  226. BtHelper.wallPaperData(chunk)
  227. if (i >= chunkSize) {
  228. _this.endImage(0)
  229. }
  230. }
  231. },
  232. endImage(value) {
  233. BtHelper.sendData(BtCmd.wallPaper(value));
  234. },
  235. startProgress() {
  236. let _this = this;
  237. this.setData({
  238. showProgress: true
  239. })
  240. // 定时器,每100毫秒执行一次
  241. let progress = 0;
  242. _this._timer = setInterval(function () {
  243. if (progress >= 100) {
  244. clearInterval(_this._timer); // 停止定时器
  245. _this.setData({
  246. progress: 0,
  247. progressPercent: 0,
  248. showProgress: false,
  249. // showCropImg: false
  250. });
  251. wx.showToast({
  252. title: '图片上传成功',
  253. })
  254. } else {
  255. progress += 1; // 每次增加1%
  256. _this.setData({
  257. progress: progress,
  258. progressPercent: progress
  259. });
  260. }
  261. }, 100);
  262. },
  263. /**
  264. * 生命周期函数--监听页面加载
  265. */
  266. onLoad(options) {
  267. let param = JSON.parse(options.param)
  268. console.log(param)
  269. this.wallpaperList();
  270. let _this = this;
  271. EventManager.addNotification(CmdEvent.eventName, function (event) {
  272. let name = event.cmdEvent;
  273. console.log("壁纸页:", event)
  274. switch (name) {
  275. case EnumCmdEvent.wallPaper:
  276. let otaCmd = event.wallPaper;
  277. let kind = event.kind;
  278. if (otaCmd === 1 && kind == 1) {
  279. // 开始发送
  280. _this.sendImage(_this.data.imageBuffer)
  281. _this.startProgress()
  282. } else if (otaCmd === 0 && kind == 1) {
  283. // 发送结束
  284. _this.endEnd()
  285. }
  286. break;
  287. }
  288. }, this)
  289. },
  290. /**
  291. * 生命周期函数--监听页面初次渲染完成
  292. */
  293. onReady() {
  294. },
  295. /**
  296. * 生命周期函数--监听页面显示
  297. */
  298. onShow() {
  299. },
  300. /**
  301. * 生命周期函数--监听页面隐藏
  302. */
  303. onHide() {
  304. },
  305. /**
  306. * 生命周期函数--监听页面卸载
  307. */
  308. onUnload() {
  309. EventManager.removeNotification(CmdEvent.eventName, this);
  310. },
  311. /**
  312. * 页面相关事件处理函数--监听用户下拉动作
  313. */
  314. onPullDownRefresh() {
  315. },
  316. /**
  317. * 页面上拉触底事件的处理函数
  318. */
  319. onReachBottom() {
  320. },
  321. /**
  322. * 用户点击右上角分享
  323. */
  324. onShareAppMessage() {
  325. }
  326. })