Android NDK ImageDecoder 函数需要构建错误
Android NDK ImageDecoder functions requires build error
我正在尝试获取 GLES 纹理的 png 文件的字节数据。
我的 png 文件在 app\source\main\assets.
Texture::Texture(const char *path) {
AAsset* asset = loadAsset(path);
AImageDecoder* decoder;
int result = AImageDecoder_createFromAAsset(asset, &decoder);
if(result != ANDROID_IMAGE_DECODER_SUCCESS){
__android_log_print(ANDROID_LOG_VERBOSE, "Android2", "Texture constructor create decode FAIL. path: &s", path);
}
const AImageDecoderHeaderInfo* info = AImageDecoder_getHeaderInfo(decoder);
this->width = AImageDecoderHeaderInfo_getWidth(info);
this->height = AImageDecoderHeaderInfo_getHeight(info);
size_t stride = AImageDecoder_getMinimumStride(decoder);
size_t size = height * stride;
void* pixels = malloc(size);
result = AImageDecoder_decodeImage(decoder, pixels, stride, size);
if(result != ANDROID_IMAGE_DECODER_SUCCESS){
__android_log_print(ANDROID_LOG_VERBOSE, "Android2", "Texture constructor decode image FAIL. path: &s", path);
}
//GLES create texture and bind data(pixels)
free(pixels);
AAsset_close(asset);
}
但我遇到了以下构建错误:
Build command failed.
Error while executing process C:\Users\jaewo\AppData\Local\Android\Sdk\cmake.10.2.4988404\bin\ninja.exe with arguments {-C C:\Users\jaewo\AndroidStudioProjects\ANdroid2\app\.cxx\cmake\debug\x86 native-lib}
ninja: Entering directory `C:\Users\jaewo\AndroidStudioProjects\ANdroid2\app\.cxx\cmake\debug\x86'
[1/2] Building CXX object CMakeFiles/native-lib.dir/Texture.cpp.o
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:20:122: warning: data argument not used by format string [-Wformat-extra-args]
__android_log_print(ANDROID_LOG_VERBOSE, "Android2", "Texture constructor create decode FAIL. path: &s", path);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:32:121: warning: data argument not used by format string [-Wformat-extra-args]
__android_log_print(ANDROID_LOG_VERBOSE, "Android2", "Texture constructor decode image FAIL. path: &s", path);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
2 warnings generated.
[2/2] Linking CXX shared library C:\Users\jaewo\AndroidStudioProjects\ANdroid2\app\build\intermediates\cmake\debug\obj\x86\libnative-lib.so
FAILED: C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/build/intermediates/cmake/debug/obj/x86/libnative-lib.so
cmd.exe /C "cd . && C:\Users\jaewo\AppData\Local\Android\Sdk\ndk.4.7075529\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=i686-none-linux-android30 --gcc-toolchain=C:/Users/jaewo/AppData/Local/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=C:/Users/jaewo/AppData/Local/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/windows-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -std=c++17 -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libnative-lib.so -o C:\Users\jaewo\AndroidStudioProjects\ANdroid2\app\build\intermediates\cmake\debug\obj\x86\libnative-lib.so CMakeFiles/native-lib.dir/native-lib.cpp.o CMakeFiles/native-lib.dir/AssetManager.cpp.o CMakeFiles/native-lib.dir/Texture.cpp.o -llog -landroid -lGLESv3 box2d/libbox2d.a -latomic -lm && cd ."
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:18: error: undefined reference to 'AImageDecoder_createFromAAsset'
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:23: error: undefined reference to 'AImageDecoder_getHeaderInfo'
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:24: error: undefined reference to 'AImageDecoderHeaderInfo_getWidth'
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:25: error: undefined reference to 'AImageDecoderHeaderInfo_getHeight'
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:27: error: undefined reference to 'AImageDecoder_getMinimumStride'
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:30: error: undefined reference to 'AImageDecoder_decodeImage'
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:35: error: undefined reference to 'AImageDecoder_delete'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
在应用 build.gradle 文件中,我将 minSDKVersion 设置为 30。
我认为我应该 link 一个关于 ImageDecoder 的库或者在我的 CMakeLists.txt 中做一些事情。
但我不确定。
target_link_libraries(
native-lib
log
android
GLESv3
box2d)
您需要 link jnigraphics
.
例如:
target_link_libraries(
native-lib
log
android
GLESv3
box2d
jnigraphics)
image-decoder
sample project 中有一个示例。
我正在尝试获取 GLES 纹理的 png 文件的字节数据。 我的 png 文件在 app\source\main\assets.
Texture::Texture(const char *path) {
AAsset* asset = loadAsset(path);
AImageDecoder* decoder;
int result = AImageDecoder_createFromAAsset(asset, &decoder);
if(result != ANDROID_IMAGE_DECODER_SUCCESS){
__android_log_print(ANDROID_LOG_VERBOSE, "Android2", "Texture constructor create decode FAIL. path: &s", path);
}
const AImageDecoderHeaderInfo* info = AImageDecoder_getHeaderInfo(decoder);
this->width = AImageDecoderHeaderInfo_getWidth(info);
this->height = AImageDecoderHeaderInfo_getHeight(info);
size_t stride = AImageDecoder_getMinimumStride(decoder);
size_t size = height * stride;
void* pixels = malloc(size);
result = AImageDecoder_decodeImage(decoder, pixels, stride, size);
if(result != ANDROID_IMAGE_DECODER_SUCCESS){
__android_log_print(ANDROID_LOG_VERBOSE, "Android2", "Texture constructor decode image FAIL. path: &s", path);
}
//GLES create texture and bind data(pixels)
free(pixels);
AAsset_close(asset);
}
但我遇到了以下构建错误:
Build command failed.
Error while executing process C:\Users\jaewo\AppData\Local\Android\Sdk\cmake.10.2.4988404\bin\ninja.exe with arguments {-C C:\Users\jaewo\AndroidStudioProjects\ANdroid2\app\.cxx\cmake\debug\x86 native-lib}
ninja: Entering directory `C:\Users\jaewo\AndroidStudioProjects\ANdroid2\app\.cxx\cmake\debug\x86'
[1/2] Building CXX object CMakeFiles/native-lib.dir/Texture.cpp.o
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:20:122: warning: data argument not used by format string [-Wformat-extra-args]
__android_log_print(ANDROID_LOG_VERBOSE, "Android2", "Texture constructor create decode FAIL. path: &s", path);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:32:121: warning: data argument not used by format string [-Wformat-extra-args]
__android_log_print(ANDROID_LOG_VERBOSE, "Android2", "Texture constructor decode image FAIL. path: &s", path);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
2 warnings generated.
[2/2] Linking CXX shared library C:\Users\jaewo\AndroidStudioProjects\ANdroid2\app\build\intermediates\cmake\debug\obj\x86\libnative-lib.so
FAILED: C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/build/intermediates/cmake/debug/obj/x86/libnative-lib.so
cmd.exe /C "cd . && C:\Users\jaewo\AppData\Local\Android\Sdk\ndk.4.7075529\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=i686-none-linux-android30 --gcc-toolchain=C:/Users/jaewo/AppData/Local/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=C:/Users/jaewo/AppData/Local/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/windows-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -std=c++17 -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libnative-lib.so -o C:\Users\jaewo\AndroidStudioProjects\ANdroid2\app\build\intermediates\cmake\debug\obj\x86\libnative-lib.so CMakeFiles/native-lib.dir/native-lib.cpp.o CMakeFiles/native-lib.dir/AssetManager.cpp.o CMakeFiles/native-lib.dir/Texture.cpp.o -llog -landroid -lGLESv3 box2d/libbox2d.a -latomic -lm && cd ."
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:18: error: undefined reference to 'AImageDecoder_createFromAAsset'
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:23: error: undefined reference to 'AImageDecoder_getHeaderInfo'
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:24: error: undefined reference to 'AImageDecoderHeaderInfo_getWidth'
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:25: error: undefined reference to 'AImageDecoderHeaderInfo_getHeight'
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:27: error: undefined reference to 'AImageDecoder_getMinimumStride'
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:30: error: undefined reference to 'AImageDecoder_decodeImage'
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:35: error: undefined reference to 'AImageDecoder_delete'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
在应用 build.gradle 文件中,我将 minSDKVersion 设置为 30。 我认为我应该 link 一个关于 ImageDecoder 的库或者在我的 CMakeLists.txt 中做一些事情。 但我不确定。
target_link_libraries(
native-lib
log
android
GLESv3
box2d)
您需要 link jnigraphics
.
例如:
target_link_libraries(
native-lib
log
android
GLESv3
box2d
jnigraphics)
image-decoder
sample project 中有一个示例。