OpenCV3.4源码交叉编译android库

  • 交叉编译环境
    系统:Ubuntu18.04
    ndk:android-ndk-14b
    (ndk下载地址,选对应版本)https://developer.android.google.cn/ndk/downloads/index.html
    cmake:3.10
    为了方便使用,先用NDK自带的脚本生成独立交叉编译工具链:
cd /opt        ###/opt为android-ndk-r14b所在的目录,根据你对应的目录改变
./android-ndk-r14b/build/tools/make-standalone-toolchain.sh --toolchain=arm-linux-androideabi-4.9 --platform=android-21 --install-dir=./android-toolchain-r14b/android-armv7 --arch=arm --force

--install-dir为生成的独立交叉编译工具链的安装目录
--force 加上这个选项会先移除安装目录下已经存在的工具
执行完成上面的命令之后会再/opt/下生成/android-toolchain-r14b/android-armv7目录
-编译OpenCV android静态库

git clone https://github.com/opencv/opencv.git
git checkout 3.4
cd opencv
mkdir build && cd build#新建文件夹存放编译文件,并进入文件夹
cmake -DCMAKE_TOOLCHAIN_FILE=../platforms/android/android.toolchain.cmake -DCMAKE_ANDROID_NDK=/opt/android-ndk-r14b -DANDROID_NATIVE_API_LEVEL=21 -DBUILD_ANDROID_PROJECTS=OFF -DBUILD_ANDROID_EXAMPLES=OFF -DCMAKE_BUILD_TYPE=Release -DBUILD_JAVA=OFF -DCMAKE_ANDROID_ARCH_ABI=armeabi-v7a -DCMAKE_INSTALL_PREFIX=/opencv3.4/install .. #配置编译选项,..表示CMakeLists.txt在上一层目录
make
make install #到这里opencv就安装到了/opencv3.4/install文件目录下

注意:如果cmake这句出现问题,很大概率是-DCMAKE_TOOLCHAIN_FILE=../platforms/android/android.toolchain.cmake的问题,原因这里就不说明了,解决办法是把这个opencv自带的android.toolchain.cmake换成你的ndk中带有的android.toolchain.cmake,它在android-ndk-r14b/build/cmake下,也就是你只需要把-DCMAKE_TOOLCHAIN_FILE=/你的ndk/android-ndk-r14b/build/cmake/android.toolchain.cmake,当然ndk14b是不会有这个问题的,大概率是你需要使用其他版本的ndk,如17,20。
-DCMAKE_ANDROID_ARCH_ABI 设置编译的版本,目前设置的是armv7版本,如果需要编译arm64,则设置为arm64-v8a即可。 该选项可以设置为:
“armeabi-v7a”, “armeabi”, “armeabi-v7a with NEON”, “armeabi-v7a-hard with NEON”, “armeabi-v7a with VFPV3”, “armeabi-v6 with VFP”, “arm64-v8a”, “mips”, “mips64”, “x86”, “x86_64”

  • 交叉编译OpenCV demo for android
mkdir test && cd test

新建test.cpp,内容如下:

#include "opencv2/opencv.hpp"

int main(){
    cv::Mat img = cv::imread("test.jpg");
    cv::imwrite("result.jpg", out);
    return 0;
}

新建CMakeLists.txt,内容入下:

cmake_minimum_required(VERSION 3.10.2)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fPIE -pie")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fPIE -pie")
set(OpenCVHome /opencv3.4/install/sdk/native)
set(NDK_STANDALONE_TOOLCHAIN /opt/android-toolchain-r14b/android-armv7)
set(CMAKE_C_COMPILER ${NDK_STANDALONE_TOOLCHAIN}/bin/clang)
set(CMAKE_CXX_COMPILER ${NDK_STANDALONE_TOOLCHAIN}/bin/clang++)
set(CMAKE_FIND_ROOT_PATH ${NDK_STANDALONE_TOOLCHAIN})
add_definitions("--sysroot=${NDK_STANDALONE_TOOLCHAIN}/sysroot")


include_directories(/opencv/include)
include_directories(${OpenCVHome}/jni/include)
link_directories(${OpenCVHome}/staticlibs/armeabi-v7a ${OpenCVHome}/3rdparty/libs/armeabi-v7a)

link_libraries(opencv_imgcodecs opencv_imgproc opencv_highgui opencv_core opencv_video opencv_features2d opencv_videoio cpufeatures tegra_hal IlmImf libjasper libjpeg-turbo libpng libprotobuf ittnotify libtiff libwebp quirc log z)
add_executable(test ./test.cpp)

在test目录下新建build文件夹,

mkdir build && cd build
cmake ../
make

此时生成了test可执行文件,把test可执行文件和test.jpg adb push到手机上后,

./test 
ll #目录下生成result.jpg则说明opencv移植成功

你可能感兴趣的:(OpenCV3.4源码交叉编译android库)