找回密码
 立即注册
搜索
热搜: 星闪 最新 活动
查看: 313|回复: 0

关于 uikit 如何动态创建字节的image

[复制链接]

33

主题

2

回帖

145

积分

注册会员

积分
145
发表于 2025-2-27 11:17:32 | 显示全部楼层 |阅读模式
我们有个300*300像素的图片通过两个步骤在表端显示
一·、图片转像素
input_bmp = 'C:/Users/15085/Desktop/img/111.bmp'
output_bin = 'C:/Users/15085/Desktop/img/111.txt'

from PIL import Image
import numpy as np

try:
    # 打开图像并转换为 RGB 模式
    image = Image.open(input_bmp).convert('RGB')
    print(image)
    print(image.size)
    # 将图像转换为 NumPy 数组
    images = np.asarray(image)

    # 获取图像的高度和宽度
    height, width, _ = images.shape

    # 打开文件以写入 RGB565 数据
    with open(output_bin, 'w') as f:
        # 写入数组声明
        f.write(f"const unsigned short image_rgb565[{height}][{width}] = {{\n")
        for y in range(height):
            f.write("    {")
            for x in range(width):
                # 获取当前像素的 RGB 值
                r, g, b = images[y, x]

                # 将 RGB 值转换为 RGB565 格式
                r5 = int((r >> 3) & 0x1F)  # 取红色的高 5 位
                g6 = int((g >> 2) & 0x3F)  # 取绿色的高 6 位
                b5 = int((b >> 3) & 0x1F)  # 取蓝色的高 5 位

                # 组合成 16 位的 RGB565 值
                rgb565 = int((r5 << 11) | (g6 << 5) | b5)

                f.write(f"0x{rgb565:04X}")
                if x < width - 1:
                    f.write(", ")
            f.write("}")
            if y < height - 1:
                f.write(",\n")
            else:
                f.write("\n")
        f.write("};\n")

    print("RGB565 数据已保存到 rgb565_array.c")

except FileNotFoundError:
    print("文件未找到,请检查文件路径是否正确。")
except Exception as e:
    print(f"发生了其他错误: {e}")

二、uikit 显示
void setupUi()
{
    // Add ImageView to App
    if (image1_ == nullptr) {
        image1_ = new UIImageView();
        views[0] = image1_;
    }
    image1_->SetPosition(67, 67);        
    image1_->Resize(300, 300);           
    image1_->SetViewId("image1");
    image1_->SetVisible(true);

    ///////////////////////////////////////////

    uint16_t imgWidth = 300;
    uint16_t imgHeight = 300;
    uint16_t alignedImgWidth = ALIGN_BYTE(imgWidth, BYTE_ALIGNMENT);
    uint32_t rgbSize = alignedImgWidth * imgHeight * 2;
    ImageInfo tempImgInfo = {0};
    tempImgInfo.color = 0;
    tempImgInfo.dataSize = rgbSize;
    tempImgInfo.header.width = imgWidth;
    tempImgInfo.header.height = imgHeight;
    tempImgInfo.header.colorMode = RGB565;
    tempImgInfo.header.compressMode = 0;
    uint8_t* rgbAddr = reinterpret_cast<uint8_t*>(ImageCacheMalloc(tempImgInfo));

    for (uint16_t i = 0; i < imgHeight; i++) {
        uint8_t* rowStart = rgbAddr + i * alignedImgWidth * 2;
        for (uint16_t j = 0; j < imgWidth; j++) {
            uint16_t* temp = (uint16_t*)(rowStart + j * 2);
            *temp = image_rgb565[j];
        }
    }
    image1_->SetSrc(&tempImgInfo);

    ///////////////////////////////////////////
}









您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|【淇诺科技】技术论坛 ( 粤ICP备14010465号-1|粤ICP备14010465号-1 )

GMT+8, 2026-4-2 19:16 , Processed in 0.123917 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表