|
@@ -42,7 +42,7 @@ Page({
|
|
}
|
|
}
|
|
|
|
|
|
if (res.confirm) {
|
|
if (res.confirm) {
|
|
- BtHelper.getInstance().endImage(0)
|
|
|
|
|
|
+ this.endImage(2)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
})
|
|
@@ -96,6 +96,7 @@ Page({
|
|
cropScale: '1:1', // 裁剪比例
|
|
cropScale: '1:1', // 裁剪比例
|
|
success(res) {
|
|
success(res) {
|
|
console.log(res)
|
|
console.log(res)
|
|
|
|
+ that.convertImageToRGB565(res)
|
|
}
|
|
}
|
|
})
|
|
})
|
|
}
|
|
}
|
|
@@ -106,45 +107,76 @@ Page({
|
|
|
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
+ // 将图片转换为 RGB565
|
|
|
|
+ convertImageToRGB565(imagePath) {
|
|
|
|
+ const ctx = wx.createCanvasContext('imageCanvas');
|
|
|
|
+ let _this = this;
|
|
|
|
+ wx.getImageInfo({
|
|
|
|
+ src: imagePath,
|
|
|
|
+ success: (info) => {
|
|
|
|
+ const { width, height } = info;
|
|
|
|
+
|
|
|
|
+ // 设置 Canvas 尺寸
|
|
|
|
+ _this.setData({ canvasWidth: width, canvasHeight: height });
|
|
|
|
+
|
|
|
|
+ // 在 Canvas 中绘制图片
|
|
|
|
+ ctx.drawImage(imagePath, 0, 0, width, height);
|
|
|
|
+ ctx.draw(false, () => {
|
|
|
|
+ // 获取图片像素数据
|
|
|
|
+ wx.canvasGetImageData({
|
|
|
|
+ canvasId: 'imageCanvas',
|
|
|
|
+ x: 0,
|
|
|
|
+ y: 0,
|
|
|
|
+ width,
|
|
|
|
+ height,
|
|
|
|
+ success: (res) => {
|
|
|
|
+ const rgb565Data = _this.RGBAtoRGB565(res.data);
|
|
|
|
+ _this.data.imageBuffer = rgb565Data;
|
|
|
|
+ // this.saveAsBinFile(rgb565Data, width, height);
|
|
|
|
+ },
|
|
|
|
+ fail: (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];
|
|
|
|
|
|
+ // 将 RGBA 数据转换为 RGB565 格式
|
|
|
|
+ RGBAtoRGB565(data) {
|
|
|
|
+ const rgb565Array = new Uint16Array(data.length / 4);
|
|
|
|
+
|
|
|
|
+ for (let i = 0; i < data.length; i += 4) {
|
|
|
|
+ const r = data[i];
|
|
|
|
+ const g = data[i + 1];
|
|
|
|
+ const b = data[i + 2];
|
|
|
|
+ // 转换为 RGB565
|
|
|
|
+ const rgb565 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
|
|
|
|
+ rgb565Array[i / 4] = rgb565;
|
|
|
|
+ }
|
|
|
|
|
|
- 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
|
|
|
|
|
|
+ return rgb565Array;
|
|
|
|
+ },
|
|
|
|
|
|
- const rgb565 = (r5 << 11) | (g6 << 5) | b5;
|
|
|
|
|
|
+ // 保存为 .bin 文件
|
|
|
|
+ saveAsBinFile(data, width, height) {
|
|
|
|
+ const arrayBuffer = data.buffer;
|
|
|
|
|
|
- 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);
|
|
|
|
- }
|
|
|
|
|
|
+ wx.getFileSystemManager().writeFile({
|
|
|
|
+ filePath: `${wx.env.USER_DATA_PATH}/image_rgb565_${width}x${height}.bin`,
|
|
|
|
+ data: arrayBuffer,
|
|
|
|
+ encoding: 'binary',
|
|
|
|
+ success: () => {
|
|
|
|
+ wx.showToast({
|
|
|
|
+ title: '保存成功',
|
|
|
|
+ icon: 'success',
|
|
|
|
+ });
|
|
|
|
+ },
|
|
|
|
+ fail: (err) => {
|
|
|
|
+ console.error('文件保存失败:', err);
|
|
|
|
+ },
|
|
|
|
+ });
|
|
},
|
|
},
|
|
|
|
|
|
imageTap(e) {
|
|
imageTap(e) {
|
|
@@ -191,6 +223,14 @@ Page({
|
|
BtHelper.sendData(BtCmd.wallPaper(1));
|
|
BtHelper.sendData(BtCmd.wallPaper(1));
|
|
},
|
|
},
|
|
sendImage(imageBuffer) {
|
|
sendImage(imageBuffer) {
|
|
|
|
+ if (imageBuffer == null) {
|
|
|
|
+ wx.showToast({
|
|
|
|
+ title: '图片裁剪失败',
|
|
|
|
+ icon: 'none'
|
|
|
|
+ })
|
|
|
|
+ _this.endImage(2)
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
let chunkSize = 20;
|
|
let chunkSize = 20;
|
|
let _this = this
|
|
let _this = this
|
|
|
|
|
|
@@ -199,15 +239,17 @@ Page({
|
|
progressPercent: 0
|
|
progressPercent: 0
|
|
});
|
|
});
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
for (let i = 0; i < imageBuffer.length; i += chunkSize) {
|
|
for (let i = 0; i < imageBuffer.length; i += chunkSize) {
|
|
|
|
+
|
|
const chunk = imageBuffer.slice(i, i + chunkSize);
|
|
const chunk = imageBuffer.slice(i, i + chunkSize);
|
|
BtHelper.wallPaperData(chunk)
|
|
BtHelper.wallPaperData(chunk)
|
|
|
|
+ if (i >= chunkSize) {
|
|
|
|
+ _this.endImage(0)
|
|
|
|
+ }
|
|
}
|
|
}
|
|
},
|
|
},
|
|
- endImage() {
|
|
|
|
- BtHelper.sendData(BtCmd.wallPaper(0));
|
|
|
|
|
|
+ endImage(value) {
|
|
|
|
+ BtHelper.sendData(BtCmd.wallPaper(value));
|
|
},
|
|
},
|
|
startProgress() {
|
|
startProgress() {
|
|
let _this = this;
|
|
let _this = this;
|
|
@@ -230,7 +272,7 @@ Page({
|
|
} else {
|
|
} else {
|
|
progress += 1; // 每次增加1%
|
|
progress += 1; // 每次增加1%
|
|
_this.setData({
|
|
_this.setData({
|
|
- progress:progress,
|
|
|
|
|
|
+ progress: progress,
|
|
progressPercent: progress
|
|
progressPercent: progress
|
|
});
|
|
});
|
|
}
|
|
}
|
|
@@ -254,7 +296,8 @@ Page({
|
|
let kind = event.kind;
|
|
let kind = event.kind;
|
|
if (otaCmd === 1 && kind == 1) {
|
|
if (otaCmd === 1 && kind == 1) {
|
|
// 开始发送
|
|
// 开始发送
|
|
- _this.sendImage()
|
|
|
|
|
|
+ _this.sendImage(_this.data.imageBuffer)
|
|
|
|
+ _this.startProgress()
|
|
} else if (otaCmd === 0 && kind == 1) {
|
|
} else if (otaCmd === 0 && kind == 1) {
|
|
// 发送结束
|
|
// 发送结束
|
|
_this.endEnd()
|
|
_this.endEnd()
|