// pages/piano/wallpaper/wallpaper.js const { deviceWallPaper } = require('../../../request/deviceListRequest') // const sharp = require('sharp'); // const fs = require('fs'); const { BtHelper } = require('../../../devices/bt_helper'); const { BtCmd } = require('../../../devices/bluetooth/bt_cmd'); import EventManager from '../../../utils/event_bus' // import EventManager from '../../utils/event_bus' Page({ /** * 页面的初始数据 */ data: { topImg: { pic: "wxfile://tmp_d3e57489ead39c698676ff860df9cb8a37f66ee1a4777dbb.jpg", id: "", name: "" }, imageList: [ { pic: "https://inews.gtimg.com/om_bt/OAVMydtx9BsJxf5i_thi4Oll9sR1px-Esmtv6UHSxoisEAA/641", id: "", name: "" }, { pic: "https://inews.gtimg.com/om_bt/OAVMydtx9BsJxf5i_thi4Oll9sR1px-Esmtv6UHSxoisEAA/641", id: "", name: "" }, { pic: "https://inews.gtimg.com/om_bt/OAVMydtx9BsJxf5i_thi4Oll9sR1px-Esmtv6UHSxoisEAA/641", id: "", name: "" }, { pic: "https://inews.gtimg.com/om_bt/OAVMydtx9BsJxf5i_thi4Oll9sR1px-Esmtv6UHSxoisEAA/641", id: "", name: "" }, { pic: "https://inews.gtimg.com/om_bt/OAVMydtx9BsJxf5i_thi4Oll9sR1px-Esmtv6UHSxoisEAA/641", id: "", name: "" } ], selectIndex: 0, nvabarData: { showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示 title: '壁纸设置', //导航栏 中间的标题 }, src: '', width: 250,//宽度 height: 250,//高度 _imageBuffer: null, }, footerTap() { const that = this; wx.chooseImage({ count: 1, // 最多可以选择的图片张数 sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album'], // 从相册选择 success: function (res) { const tempFilePaths = res.tempFilePaths; console.log('获取图片信息成功', res); wx.cropImage({ src: tempFilePaths[0], // 图片路径 cropScale: '1:1', // 裁剪比例 success: (res) => { console.log('裁剪成功', res); that.setData({ src: res.tempFilePath }) } }) // wx.getImageInfo({ // src: tempFilePaths[0], // success: function (imageInfo) { // console.log(imageInfo); // //获取到image-cropper实例 // //开始裁剪 // // that.setData({ // // src: imageInfo.path,//要裁剪的图片 // // }) // // wx.navigateTo({ // // url: `/pages/crop/crop?path=${tempFilePaths[0]}&width=${imageInfo.width}&height=${imageInfo.height}` // // }); // }, // fail: function (err) { // console.error('获取图片信息失败', err); // } // }); }, fail: function (err) { console.error('选择图片失败', err); } }); }, async convertToRGB565(inputPath, outputPath) { try { // todo let sharp = null const metadata = await sharp(inputPath).metadata(); const { width, height } = metadata; const buffer = await sharp(inputPath) .raw() .toBuffer(); const outputBuffer = Buffer.alloc(width * height * 2); // 2 bytes per pixel for RGB565 for (let y = 0; y < height; y++) { for (let x = 0; x < width; x++) { const index = (y * width + x) * 4; // RGBA const r = buffer[index]; const g = buffer[index + 1]; const b = buffer[index + 2]; const r5 = (r >> 3) & 0x1F; // 5 bits for red const g6 = (g >> 2) & 0x3F; // 6 bits for green const b5 = (b >> 3) & 0x1F; // 5 bits for blue const rgb565 = (r5 << 11) | (g6 << 5) | b5; const outputIndex = (y * width + x) * 2; outputBuffer[outputIndex] = (rgb565 >> 8) & 0xFF; // High byte outputBuffer[outputIndex + 1] = rgb565 & 0xFF; // Low byte } } _this.data._imageBuffer = outputBuffer; _this.startImage() // fs.writeFileSync(outputPath, outputBuffer); console.log(`Conversion successful: ${outputPath}`); } catch (error) { console.error('Error converting image:', error); } }, imageTap(e) { console.log(e) // wxfile://tmp_d3e57489ead39c698676ff860df9cb8a37f66ee1a4777dbb.jpg let image = e.currentTarget.dataset.image; _this.setData({ topImg: image ?? {} }) }, wallpaperList() { let _this = this; deviceWallPaper().then(res => { console.log("壁纸列表", res); let data = res.data ?? [] if (data.length == 0) { _this.setData({ imageList: [], topImg: {}, }) return; } topImg = data[0]; _this.setData({ imageList: data ?? [], topImg: data[0] ?? {} }) }) }, startImage() { let _this = this; BtHelper.sendData(BtCmd.wallPaper(1)); }, sendImage(imageBuffer) { let chunkSize = 20; for (let i = 0; i < imageBuffer.length; i += chunkSize) { const chunk = imageBuffer.slice(i, i + chunkSize); // this.sendImage(chunk); BtHelper.wallPaperData(chunk) } }, endImage() { BtHelper.sendData(BtCmd.wallPaper(0)); }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { let param = JSON.parse(options.param) console.log(param) this.wallpaperList(); let _this = this; EventManager.addNotification(CmdEvent.eventName, function (event) { let name = event.name; console.log("OTA页:", event) switch (name) { case EnumCmdEvent.wallPaper: let otaCmd = event.wallPaper; let kind = event.kind; if (otaCmd === 1 && kind == 1) { // 开始发送 _this.sendOtaData() } else if (otaCmd === 0 && kind == 1) { // 发送结束 _this.endEnd() } break; } }, this) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { EventManager.removeNotification(CmdEvent.eventName, this); }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })