|
@@ -13,11 +13,11 @@ Page({
|
|
|
*/
|
|
|
data: {
|
|
|
src: "",
|
|
|
- width: 300,//宽度
|
|
|
- height: 300,//高度
|
|
|
+ width: 320,//宽度
|
|
|
+ height: 320,//高度
|
|
|
|
|
|
- max_width: 300,
|
|
|
- max_height: 300,
|
|
|
+ max_width: 320,
|
|
|
+ max_height: 320,
|
|
|
disable_rotate: true, //是否禁用旋转
|
|
|
disable_ratio: true, //锁定比例
|
|
|
limit_move: true, //是否限制移动
|
|
@@ -27,41 +27,36 @@ Page({
|
|
|
},
|
|
|
cropper: null,
|
|
|
|
|
|
-
|
|
|
- // 将 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;
|
|
|
+ // 图片转RGB565
|
|
|
+ convertToRGB565(imageData) {
|
|
|
+ const { data, width, height } = imageData;
|
|
|
+ // 原始像素数据 (RGBA 格式)
|
|
|
+ const rgba = new Uint8Array(data);
|
|
|
+
|
|
|
+ // 创建 RGB565 的 ArrayBuffer
|
|
|
+ const pixelCount = width * height;
|
|
|
+ const rgb565Buffer = new ArrayBuffer(pixelCount * 2); // 每像素 2 字节
|
|
|
+ const rgb565View = new Uint16Array(rgb565Buffer);
|
|
|
+
|
|
|
+ // 转换每个像素
|
|
|
+ for (let i = 0; i < pixelCount; i++) {
|
|
|
+ const r = rgba[i * 4]; // Red
|
|
|
+ const g = rgba[i * 4 + 1]; // Green
|
|
|
+ const b = rgba[i * 4 + 2]; // Blue
|
|
|
+
|
|
|
+ // RGB565 的位运算
|
|
|
+ const r5 = (r >> 3) & 0x1F; // R 的高 5 位
|
|
|
+ const g6 = (g >> 2) & 0x3F; // G 的高 6 位
|
|
|
+ const b5 = (b >> 3) & 0x1F; // B 的高 5 位
|
|
|
+
|
|
|
+ // 组合成 RGB565 格式
|
|
|
+ const rgb565 = (r5 << 11) | (g6 << 5) | b5;
|
|
|
+
|
|
|
+ // 写入到 RGB565 的 buffer 中
|
|
|
+ rgb565View[i] = rgb565;
|
|
|
}
|
|
|
|
|
|
- return rgb565Array;
|
|
|
- },
|
|
|
-
|
|
|
- // 保存为 .bin 文件
|
|
|
- saveAsBinFile(data, width, height) {
|
|
|
- const arrayBuffer = data.buffer;
|
|
|
-
|
|
|
- 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);
|
|
|
- },
|
|
|
- });
|
|
|
+ return rgb565Buffer;
|
|
|
},
|
|
|
cropperload(e) {
|
|
|
console.log('cropper加载完成');
|
|
@@ -108,58 +103,82 @@ Page({
|
|
|
wx.showLoading({
|
|
|
title: '图片裁剪中',
|
|
|
})
|
|
|
+ const fs = wx.getFileSystemManager();
|
|
|
+
|
|
|
this.cropper.getImg((obj) => {
|
|
|
// app.globalData.imgSrc = obj.url;
|
|
|
console.log("裁剪图片:", obj);
|
|
|
- _this.readLocalFileAndConvertToBase64(obj.url);
|
|
|
- // _this.readBinFile("../../pagesA/images/th01.bin");
|
|
|
+ fs.readFile({
|
|
|
+ filePath: obj.url,
|
|
|
+ encoding: '', // 不指定编码以获取原始二进制数据
|
|
|
+ success: (res) => {
|
|
|
+ console.log("下载文件成功:", res)
|
|
|
+ let rgbData = _this.convertToRGB565({
|
|
|
+ width: obj.width,
|
|
|
+ height: obj.height,
|
|
|
+ data: res.data
|
|
|
+ })
|
|
|
+ _this.data._imageBuffer = rgbData;
|
|
|
+ console.log("下载文件成功2:", _this.data._imageBuffer)
|
|
|
+ wx.hideLoading();
|
|
|
+ wx.showLoading({
|
|
|
+ title: '开始传输图片',
|
|
|
+ })
|
|
|
+ _this.startImage();
|
|
|
+ },
|
|
|
+ fail: (err) => {
|
|
|
+ console.error('读取 .bin 文件失败:', err);
|
|
|
+ },
|
|
|
+ });
|
|
|
});
|
|
|
+ // 651kb的
|
|
|
+ // _this.downloadAndSaveFile("https://music-play.oss-cn-shenzhen.aliyuncs.com/backOss/file/6a909799a6924e6f86a4683e6da4fad4.bin");
|
|
|
+ // 55kb的
|
|
|
+ // _this.downloadAndSaveFile("https://music-play.oss-cn-shenzhen.aliyuncs.com/backOss/file/55d2dd22bd554eb19b71536bec4ba42c.bin");
|
|
|
+
|
|
|
},
|
|
|
- submitTest() {
|
|
|
- route_util.jump("../../pagesA/index/index")
|
|
|
- },
|
|
|
- // 读取本地文件并转换为 Base64 字符串
|
|
|
- readLocalFileAndConvertToBase64(filePath) {
|
|
|
+ // 下载网络文件并保存到本地
|
|
|
+ downloadAndSaveFile(url) {
|
|
|
+ // https://music-play.oss-cn-shenzhen.aliyuncs.com/backOss/file/6a909799a6924e6f86a4683e6da4fad4.bin
|
|
|
const fs = wx.getFileSystemManager();
|
|
|
let _this = this;
|
|
|
- fs.readFile({
|
|
|
- filePath: filePath,
|
|
|
- encoding: 'base64',
|
|
|
+ wx.downloadFile({
|
|
|
+ url: url, // 网络文件的 URL
|
|
|
success: (res) => {
|
|
|
- // const base64Data = 'data:image/png;base64,' + res.data;
|
|
|
- // let rgbData = _this.RGBAtoRGB565(res.data)
|
|
|
- let rgbData = res.data
|
|
|
- console.log("转换rgb:", rgbData.length)
|
|
|
- _this.data._imageBuffer = rgbData;
|
|
|
- wx.hideLoading();
|
|
|
- wx.showLoading({
|
|
|
- title: '开始传输图片',
|
|
|
- })
|
|
|
- _this.startImage();
|
|
|
- },
|
|
|
- fail: (err) => {
|
|
|
- console.error('读取文件失败:', err);
|
|
|
- }
|
|
|
- });
|
|
|
- },
|
|
|
- readBinFile(filePath) {
|
|
|
- const fs = wx.getFileSystemManager();
|
|
|
-
|
|
|
- fs.readFile({
|
|
|
- filePath: filePath,
|
|
|
- encoding: '', // 不指定编码以获取原始二进制数据
|
|
|
- success: (res) => {
|
|
|
- console.log("读取 .bin 文件成功:", res.data);
|
|
|
- // 处理读取到的二进制数据
|
|
|
- this.processBinData(res.data);
|
|
|
+ if (res.statusCode === 200) {
|
|
|
+ // 下载成功,res.tempFilePath 是临时文件路径
|
|
|
+ const tempFilePath = res.tempFilePath;
|
|
|
+ console.log("下载文件:", res)
|
|
|
+ fs.readFile({
|
|
|
+ filePath: tempFilePath,
|
|
|
+ // encoding: '', // 不指定编码以获取原始二进制数据
|
|
|
+ success: (res) => {
|
|
|
+ console.log("下载文件成功:", res.data)
|
|
|
+ let rgbData = res.data
|
|
|
+ _this.data._imageBuffer = rgbData;
|
|
|
+ // console.log("下载文件成功2:", _this.data._imageBuffer)
|
|
|
+
|
|
|
+ wx.hideLoading();
|
|
|
+ wx.showLoading({
|
|
|
+ title: '开始传输图片',
|
|
|
+ })
|
|
|
+ _this.startImage();
|
|
|
+ },
|
|
|
+ fail: (err) => {
|
|
|
+ console.error('读取 .bin 文件失败:', err);
|
|
|
+ },
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ console.error('下载文件失败:', res.statusCode);
|
|
|
+ }
|
|
|
},
|
|
|
fail: (err) => {
|
|
|
- console.error('读取 .bin 文件失败:', err);
|
|
|
+ console.error('下载文件失败:', err);
|
|
|
},
|
|
|
});
|
|
|
},
|
|
|
checkAndRequestImagePermission: function () {
|
|
|
- const self = this;
|
|
|
+ const _this = this;
|
|
|
|
|
|
// 检查用户是否已经授权访问相册
|
|
|
wx.getSetting({
|
|
@@ -205,98 +224,55 @@ Page({
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
-
|
|
|
- // 检查用户是否已经授权使用相机
|
|
|
- wx.getSetting({
|
|
|
- success(res) {
|
|
|
- if (!res.authSetting['scope.camera']) {
|
|
|
- // 用户未授权使用相机,请求用户授权
|
|
|
- wx.authorize({
|
|
|
- scope: 'scope.camera',
|
|
|
- success() {
|
|
|
- // 用户同意授权
|
|
|
- console.log('用户已授权使用相机');
|
|
|
- // 可以在这里执行使用相机的操作
|
|
|
- },
|
|
|
- fail() {
|
|
|
- // 用户拒绝授权
|
|
|
- console.log('用户拒绝授权使用相机');
|
|
|
- wx.showModal({
|
|
|
- title: '提示',
|
|
|
- content: '您拒绝了使用相机的权限,请在设置中手动开启',
|
|
|
- showCancel: false,
|
|
|
- success(res) {
|
|
|
- if (res.confirm) {
|
|
|
- // 跳转到设置页面
|
|
|
- wx.openSetting({
|
|
|
- success(settingRes) {
|
|
|
- if (settingRes.authSetting['scope.camera']) {
|
|
|
- console.log('用户已在设置中开启使用相机的权限');
|
|
|
- // 可以在这里执行使用相机的操作
|
|
|
- } else {
|
|
|
- console.log('用户仍未授权使用相机');
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
- });
|
|
|
- } else {
|
|
|
- // 用户已授权使用相机
|
|
|
- console.log('用户已授权使用相机');
|
|
|
- // 可以在这里执行使用相机的操作
|
|
|
- }
|
|
|
+ },
|
|
|
+ async startImage() {
|
|
|
+ BtHelper.sendCallBack(BtCmd.wallPaper(1), function (res) {
|
|
|
+ if (!res) {
|
|
|
+ wx.hideLoading()
|
|
|
+ wx.showToast({
|
|
|
+ title: '发送失败',
|
|
|
+ icon: 'none'
|
|
|
+ })
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
- startImage() {
|
|
|
- BtHelper.getInstance().send(BtCmd.wallPaper(1));
|
|
|
- },
|
|
|
sliceDataIntoChunks(data, chunkSize) {
|
|
|
+ console.log("发送图片数据:", data.byteLength)
|
|
|
const chunks = [];
|
|
|
- for (let i = 0; i < data.length; i += chunkSize) {
|
|
|
+ // for (let i = 0; i < data.length; i += chunkSize) {
|
|
|
+ for (let i = 0; i < data.byteLength; i += chunkSize) {
|
|
|
const chunk = data.slice(i, i + chunkSize);
|
|
|
chunks.push(chunk);
|
|
|
}
|
|
|
return chunks;
|
|
|
},
|
|
|
- startSendImage(imageBuffer) {
|
|
|
- if (imageBuffer == null) {
|
|
|
- wx.showToast({
|
|
|
- title: '图片裁剪失败',
|
|
|
- icon: 'none'
|
|
|
- })
|
|
|
- _this.endImage(2)
|
|
|
- return;
|
|
|
- }
|
|
|
- // _this.sendImage(imageBuffer, 0)
|
|
|
-
|
|
|
- // }
|
|
|
- },
|
|
|
async sendImage(imageBuffer, index) {
|
|
|
let _this = this
|
|
|
|
|
|
- // if (index >= chunkSize) {
|
|
|
- // wx.showModal({
|
|
|
- // title: '图片上传成功1',
|
|
|
- // showCancel: false
|
|
|
- // })
|
|
|
- // _this.endImage(0)
|
|
|
- // return;
|
|
|
- // }
|
|
|
-
|
|
|
- let chunks = this.sliceDataIntoChunks(imageBuffer, 20);
|
|
|
+ wx.hideLoading()
|
|
|
+ console.log("发送图片数据0:", index, imageBuffer.byteLength)
|
|
|
+ // console.log("发送图片数据0:", index, imageBuffer.length)
|
|
|
+ let chunks = this.sliceDataIntoChunks(imageBuffer, 16);
|
|
|
|
|
|
let next = 0;
|
|
|
- let total = imageBuffer.length;
|
|
|
+ let total = imageBuffer.byteLength;
|
|
|
+ // let total = imageBuffer.length;
|
|
|
+ let btHelper = BtHelper.getInstance();
|
|
|
for (let i = 0; i < chunks.length; i++) {
|
|
|
const chunk = chunks[i];
|
|
|
- next += chunk.length;
|
|
|
- console.log("发送图片数据:", i, next, chunk)
|
|
|
+ next += chunk.byteLength;
|
|
|
+ // next += chunk.length;
|
|
|
+ let uint8Array = new Uint8Array(chunk);
|
|
|
+ let unit16 = uint8Array.map(item => {
|
|
|
+ return item.toString(16)
|
|
|
+ });
|
|
|
+ console.log("发送图片数据1:", i, next, total, unit16)
|
|
|
+ _this.updateProgress(next, total);
|
|
|
+
|
|
|
+ await _this.delay(20);
|
|
|
+ let res = await btHelper.wallPaperSyncData(chunk);
|
|
|
+ // btHelper.wallPaperData(chunk);
|
|
|
|
|
|
- let res = await BtHelper.getInstance().wallPaperSyncData(chunk);
|
|
|
let nowDate = Date.now()
|
|
|
if (i === chunks.length - 1 && res) {
|
|
|
wx.showModal({
|
|
@@ -304,17 +280,18 @@ Page({
|
|
|
showCancel: false
|
|
|
})
|
|
|
_this.endImage(0)
|
|
|
+
|
|
|
} else if (!res) {
|
|
|
wx.showModal({
|
|
|
- title: '图片上传失败了',
|
|
|
+ title: '图片上传失败',
|
|
|
showCancel: false
|
|
|
})
|
|
|
_this.endImage(2)
|
|
|
- break;
|
|
|
}
|
|
|
- _this.updateProgress(next, total);
|
|
|
}
|
|
|
-
|
|
|
+ },
|
|
|
+ async delay(ms) {
|
|
|
+ return new Promise(resolve => setTimeout(resolve, ms));
|
|
|
},
|
|
|
endImage(value) {
|
|
|
BtHelper.getInstance().wallPaper(value);
|
|
@@ -337,8 +314,6 @@ Page({
|
|
|
wx.showToast({
|
|
|
title: '图片上传成功',
|
|
|
})
|
|
|
- _this.endImage(0)
|
|
|
-
|
|
|
} else {
|
|
|
_this.setData({
|
|
|
progress: progress,
|
|
@@ -349,8 +324,6 @@ Page({
|
|
|
* 生命周期函数--监听页面加载
|
|
|
*/
|
|
|
onLoad(options) {
|
|
|
- console.log(options.param)
|
|
|
- // let json = JSON.parse(options.param)
|
|
|
this.checkAndRequestImagePermission()
|
|
|
this.cropper = this.selectComponent("#image-cropper");
|
|
|
this.cropper.imgReset();
|