如果在linux上,可以参考下面的guide搭建ncnn的编译框架
https://github.com/Tencent/ncnn/wiki/how-to-build#build-for-linux-x86 (Build ok)
使用的sample是
Sample: ncnn-android-styletransfer
https://github.com/nihui/ncnn-android-styletransfer
参考guide进行编译sample apk。有几个地方需要注意,还会有几个错误需要fix。
##前置条件
Ubuntu16.04,Android Studio(在window下编译apk也可以用)
##库和头文件引用
###1 可以使用绝对路径
set_target_properties(ncnn PROPERTIES IMPORTED_LOCATION /local/mnt/workspace/xxxx/ncnn/ncnn/build-android-aarch64/install/lib/libncnn.a)
include_directories(/local/mnt/workspace/xxxx/ncnn/ncnn/build-android-aarch64/install/include/ncnn)
###2 也可以使用相对路径,需要将lib文件和头文件copy到项目目录,注意需要在jni目录下,路径错误会有编译error提示路径
set_target_properties(ncnn PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/lib/${ANDROID_ABI}/libncnn.a)
include_directories(${CMAKE_SOURCE_DIR}/include/ncnn)
ncnn-android-styletransfer-master\app\src\main\jni\lib\arm64-v8a
ncnn-android-styletransfer-master\app\src\main\jni\lib\armeabi-v7a
ncnn-android-styletransfer-master\app\src\main\jni\include\ncnn
###3 lib的类型要合适,并和头文件是一套。自己编译的库文件使用不了,用了官方提供的ncnn-android-vulkan-lib.zip。(不匹配有编译错误如error: no member named 'create_gpu_instance' in namespace 'ncnn')
##error: no member named 'create_gpu_instance' in namespace 'ncnn'
这个问题除了上面介绍的匹配库文件之外,还要在jni文件加上
#include “gpu.h”
## dlopen failed: library "libomp.so"
安装的应用不能启动
Refer to
https://github.com/Tencent/ncnn/issues/976#issuecomment-592394493
https://github.com/android/ndk/issues/1028
add it in the CMakeLists.txt
target_link_libraries (styletransferncnn -static-openmp -fopenmp)
##在有的机型上转换图片,应用会退出
尚未分析log
## 运行结果
## 调用流程
其UI部分的流程比较简单,就是在actitity里面初始化,通过按钮触发对应的api实现功能。
## 对于 squeezenet的sample
https://github.com/nihui/ncnn-android-squeezenet
可以在GitHub上下载源码,在windows下用AS编译出apk来运行。
需要注意的是copy ncnn的lib和头文件的路径要根据jni的mk文件来确定。
根据
# change this folder path to yours
NCNN_INSTALL_PATH := ${LOCAL_PATH}/lib
LOCAL_SRC_FILES := $(NCNN_INSTALL_PATH)/$(TARGET_ARCH_ABI)/libncnn.a
LOCAL_C_INCLUDES := $(NCNN_INSTALL_PATH)/include $(NCNN_INSTALL_PATH)/include/ncnn
来拷贝文件到下面的目录
ncnn-android-squeezenet-master\app\src\main\jni\lib\include\ncnn
ncnn-android-squeezenet-master\app\src\main\jni\lib\arm64-v8a
ncnn-android-squeezenet-master\app\src\main\jni\lib\armeabi-v7a
编译出来的apk不能打开,会有crash log
02-04 05:07:50.936 3922 3922 D AndroidRuntime: Shutting down VM
02-04 05:07:50.938 3922 3922 E AndroidRuntime: FATAL EXCEPTION: main
02-04 05:07:50.938 3922 3922 E AndroidRuntime: Process: com.tencent.squeezencnn, PID: 3922
02-04 05:07:50.938 3922 3922 E AndroidRuntime: java.lang.UnsatisfiedLinkError: dlopen failed: library "libomp.so" not found
02-04 05:07:50.938 3922 3922 E AndroidRuntime: at java.lang.Runtime.loadLibrary0(Runtime.java:1071)
可以在mk文件里面添加-static-openmp,如下
LOCAL_CFLAGS += -static-openmp -fopenmp
LOCAL_CPPFLAGS += -static-openmp -fopenmp
LOCAL_LDFLAGS += -static-openmp -fopenmp