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, 1); kzuResourceRelease(out_resource);
【3.3版本】
3.3可以参考kanzi安装目录下的 virtual listbox的例子。
下面是例子中的关键代码:
1 if (m_itemInfo->m_objectNode && m_image) 2 { 3 Domain* domain = m_itemGenerator->m_listBox->getDomain(); 4 5 // Create texture from image. If the image data format is correct, this shouldn't use any memory manager. 6 // Image texture will own the image so no need to delete it manually. 7 TextureSharedPtr texture = Texture::create(domain, m_image, Texture::CreateFlagClampAddress); 8 m_itemInfo->m_objectNode->setProperty(StandardMaterial::TextureProperty, texture); 9 10 // Adjust size of plane for displaying the image. 11 Node3DSharedPtr imagePlaneNode = m_itemInfo->m_objectNode->lookupNode<Node3D>("Stack Layout/Plane"); 12 13 kzUint height = kzcImageGetHeight(m_image); 14 kzUint width = kzcImageGetWidth(m_image); 15 float aspect = width * 1.0f / height; 16 17 kzFloat widthFactor = aspect; 18 kzFloat heightFactor = 1.0f; 19 20 if(aspect > 1.0f) 21 { 22 widthFactor = 1.0f; 23 heightFactor = 1.0f / aspect; 24 } 25 26 imagePlaneNode->setLayoutTransformation(Matrix4x4::createScale(Vector3(widthFactor, heightFactor, 1.0f))); 27 28 // Ownership of image was transferred to the texture. 29 m_image = 0; 30 } 31 32 // Remove task. 33 m_itemGenerator->m_tasksByItems.erase(m_itemInfo);
3.3版本的代码暂时还没有分析,待补充……