159 lines
4.7 KiB
JavaScript
159 lines
4.7 KiB
JavaScript
function hex2Array(hex) {
|
||
//16进制字符串转字节数组
|
||
var result = [];
|
||
if (hex.length % 2 == 1) hex = '0' + hex
|
||
for (var i = 0; i < hex.length; i += 2) {
|
||
result.push(parseInt(hex.substring(i, i + 2), 16));
|
||
}
|
||
result = Uint8Array.from(result)
|
||
return result.buffer
|
||
}
|
||
|
||
function array2Hex(value) {
|
||
//value类型为 ArrayBuffer
|
||
let arr = new Uint8Array(value)
|
||
var hex = Buffer.from(arr).toString('hex').toUpperCase()
|
||
return hex
|
||
}
|
||
|
||
function string2Array(str) {
|
||
//value类型为 ArrayBuffer
|
||
let arr = new Uint8Array(str.length)
|
||
for (var i = 0; i < str.length; i++) {
|
||
arr[i] = str.charCodeAt(i)
|
||
}
|
||
return arr.buffer
|
||
}
|
||
|
||
function array2String(value) {
|
||
//value类型为 ArrayBuffer
|
||
let arr = new Uint8Array(value)
|
||
var str = String.fromCharCode.apply(null, arr)
|
||
console.error('str=[' + str + ']')
|
||
return str
|
||
}
|
||
|
||
function formatDate(fmt, date) {
|
||
let ret;
|
||
const opt = {
|
||
"y+": date.getFullYear().toString(), // 年
|
||
"M+": (date.getMonth() + 1).toString(), // 月
|
||
"d+": date.getDate().toString(), // 日
|
||
"H+": date.getHours().toString(), // 时
|
||
"m+": date.getMinutes().toString(), // 分
|
||
"s+": date.getSeconds().toString(), // 秒
|
||
"S+": date.getMilliseconds().toString() // 毫秒
|
||
// 有其他格式化字符需求可以继续添加,必须转化成字符串
|
||
};
|
||
for (let k in opt) {
|
||
ret = new RegExp("(" + k + ")").exec(fmt);
|
||
if (ret) {
|
||
fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
|
||
};
|
||
};
|
||
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,
|
||
imageData2RGB565: imageData2RGB565,
|
||
rgb565ToHexString: rgb565ToHexString,
|
||
rgb565ToCArray: rgb565ToCArray,
|
||
validateRGB565Data: validateRGB565Data
|
||
}
|