I am making an Android application with vulkan. I saved bitmap in "texture.png" file in the device internal storage. I want to show this image with vulkan. Then I need to pass image data to c++ somehow.
I tried below process:
But stbi_load() return NULL and stb_error message is "PNG not supported: unknown PNG chunk type".
How to fix to load image in android internal storage file?
outlet of my code.
// create file path (kotlin)
val filePath: String = File(context.filesDir, "texture.png").path
// load image file (c++)
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
void loadImage(const std::string filePath) {// same as ↑ filePath
int width, height, channels;
auto* pImage = stbi_load(filePath.c_str(), &width, &height, &channels, 0);
if (pImage == nullptr) {
return;// I'm here.
}
// (some successive process)
}
I found the alternative to pass image data to c++.
I tried and give up this.
It is working very well. I can show the file image with vulkan now.
code outlet
JNIEXPORT void JNICALL
Java_com_example_yourproject_NativeAPI_loadBitmap(JNIEnv* env, jobject thiz, jobject bitmap) {
AndroidBitmapInfo androidBitmapInfo;
AndroidBitmap_getInfo(env, bitmap, &androidBitmapInfo);
void* pixels;// image data.
AndroidBitmap_lockPixels(env, bitmap, &pixels);
AndroidBitmap_unlockPixels(env, bitmap);
・
・
・
// For example, copy image data to vulkan image memory.
void* p;
vkMapMemory(device, imageMemory, 0, VK_WHOLE_SIZE, 0, &p);
memcpy(p, pixels, width * height * sizeof(uint32_t));
vkUnmapMemory(device, imageMemory);
}
Don't forget to link jnigraphics in CMakeList.txt!