增加转换开机工具类

This commit is contained in:
微微一笑
2025-07-05 16:14:23 +08:00
parent aede64dacd
commit 445e4b65cf
7 changed files with 2824 additions and 7935 deletions

View File

@ -54,10 +54,105 @@ function formatDate(fmt, date) {
return fmt;
}
/**
* 将图片数据转换为硬件需要的RGB565格式大端序
* @param {Uint8ClampedArray} imageData - canvas getImageData返回的像素数据
* @param {number} width - 图片宽度默认160
* @param {number} height - 图片高度默认80
* @returns {ArrayBuffer} 转换后的RGB565数据
*/
function imageData2RGB565(imageData, width = 160, height = 80) {
const totalPixels = width * height;
const rgb565Array = new Uint8Array(totalPixels * 2); // 每个像素2字节
let arrayIndex = 0;
// 每4个字节为一个像素(RGBA)
for (let i = 0; i < imageData.length; i += 4) {
const r = imageData[i]; // 红色分量
const g = imageData[i + 1]; // 绿色分量
const b = imageData[i + 2]; // 蓝色分量
// imageData[i + 3] 是alpha透明度RGB565不需要
// 转换为RGB565格式
const r5 = (r >> 3) & 0x1F; // 8位红色转5位
const g6 = (g >> 2) & 0x3F; // 8位绿色转6位
const b5 = (b >> 3) & 0x1F; // 8位蓝色转5位
// 合并为16位RGB565值
const rgb565 = (r5 << 11) | (g6 << 5) | b5;
// 大端序存储(高字节在前)
rgb565Array[arrayIndex++] = (rgb565 >> 8) & 0xFF; // 高字节
rgb565Array[arrayIndex++] = rgb565 & 0xFF; // 低字节
}
return rgb565Array.buffer;
}
/**
* 将RGB565数据转换为十六进制字符串用于调试
* @param {ArrayBuffer} rgb565Data - RGB565数据
* @returns {string} 十六进制字符串
*/
function rgb565ToHexString(rgb565Data) {
const uint8Array = new Uint8Array(rgb565Data);
return Array.from(uint8Array)
.map(byte => byte.toString(16).padStart(2, '0').toUpperCase())
.join('');
}
/**
* 将RGB565数据转换为C数组格式字符串用于调试
* @param {ArrayBuffer} rgb565Data - RGB565数据
* @param {string} arrayName - 数组名称,默认'gImage_logo'
* @returns {string} C数组格式字符串
*/
function rgb565ToCArray(rgb565Data, arrayName = 'gImage_logo') {
const uint8Array = new Uint8Array(rgb565Data);
let cCode = `const unsigned char ${arrayName}[${uint8Array.length}] = {\n`;
for (let i = 0; i < uint8Array.length; i += 16) {
let line = '';
for (let j = 0; j < 16 && i + j < uint8Array.length; j++) {
line += `0X${uint8Array[i + j].toString(16).padStart(2, '0').toUpperCase()}`;
if (i + j < uint8Array.length - 1) line += ',';
}
cCode += line + '\n';
}
cCode += '};\n';
return cCode;
}
/**
* 验证RGB565数据格式
* @param {ArrayBuffer} rgb565Data - RGB565数据
* @param {number} expectedWidth - 期望宽度
* @param {number} expectedHeight - 期望高度
* @returns {object} 验证结果
*/
function validateRGB565Data(rgb565Data, expectedWidth = 160, expectedHeight = 80) {
const uint8Array = new Uint8Array(rgb565Data);
const expectedSize = expectedWidth * expectedHeight * 2;
return {
isValid: uint8Array.length === expectedSize,
actualSize: uint8Array.length,
expectedSize: expectedSize,
width: expectedWidth,
height: expectedHeight,
pixelCount: uint8Array.length / 2
};
}
module.exports = {
hex2Array: hex2Array,
array2Hex: array2Hex,
string2Array: string2Array,
array2String: array2String,
formatDate: formatDate
formatDate: formatDate,
imageData2RGB565: imageData2RGB565,
rgb565ToHexString: rgb565ToHexString,
rgb565ToCArray: rgb565ToCArray,
validateRGB565Data: validateRGB565Data
}