Kanzi编程基础3 - 图片读取与显示

时间:2021-01-16 20:55:24

Kanzi开发的时候会遇到需要从外部读取图片的情况。Kanzi2.8版本和3.3版本读取方法稍有不同,我们先看看2.8版本的api。

【2.8版本】

1)首先要从文件中读取一张图片

struct KzcImage* img;
kzcImageLoadFile(kzaApplicationGetSystemMemoryManager(application),"1.png",& img);

2)把文件中读取到的图片转成Texture

KzuImageTexture* texture;
kzuImageTextureCreateFromImage(resMgr, "FileTexture",
img, KZU_TEXTURE_FILTER_BILINEAR, KZU_TEXTURE_WRAP_CLAMP, 0.0f, &texture);

或者从内存数据中创建一个Texture:

kzuImageTextureCreateFromMemory(resourceManger,name,

                                        KZU_TEXTURE_CHANNELS_RGB,

                                        img.width(), img.height(), img.bits(),

                                        KZU_TEXTURE_FILTER_BILINEAR, KZU_TEXTURE_WRAP_CLAMP, &imageTexture);

其中resourceManger是一个ResourceManger对象,name为Texture的名称,KZU_TEXTURE_CHANNELS_RGB为创建的图像模式,bits()为图片数据,最终创建到imageTexture中。

3)创建之后转成resource并赋给plane节点

struct KzuResource* out_resource;

out_resource = kzuImageTextureToResource(texture);

kzuObjectNodeSetResourceIDResourceProperty(planeNode,KZU_PROPERTY_TYPE_TEXTURE,out_resource);

kzuObjectNodeSetIntProperty(m_planeNode, KZU_PROPERTY_TYPE_BLEND_MODE, );

kzuResourceRelease(out_resource);

【3.3版本】

3.3可以参考kanzi安装目录下的 virtual listbox的例子。

下面是例子中的关键代码:

 if (m_itemInfo->m_objectNode && m_image)
{
Domain* domain = m_itemGenerator->m_listBox->getDomain(); // Create texture from image. If the image data format is correct, this shouldn't use any memory manager.
// Image texture will own the image so no need to delete it manually.
TextureSharedPtr texture = Texture::create(domain, m_image, Texture::CreateFlagClampAddress);
m_itemInfo->m_objectNode->setProperty(StandardMaterial::TextureProperty, texture); // Adjust size of plane for displaying the image.
Node3DSharedPtr imagePlaneNode = m_itemInfo->m_objectNode->lookupNode<Node3D>("Stack Layout/Plane"); kzUint height = kzcImageGetHeight(m_image);
kzUint width = kzcImageGetWidth(m_image);
float aspect = width * 1.0f / height; kzFloat widthFactor = aspect;
kzFloat heightFactor = 1.0f; if(aspect > 1.0f)
{
widthFactor = 1.0f;
heightFactor = 1.0f / aspect;
} imagePlaneNode->setLayoutTransformation(Matrix4x4::createScale(Vector3(widthFactor, heightFactor, 1.0f))); // Ownership of image was transferred to the texture.
m_image = ;
} // Remove task.
m_itemGenerator->m_tasksByItems.erase(m_itemInfo);

3.3版本的代码暂时还没有分析,待补充……