在linux下编译android下的opencv,使用cmake的方法

#前一篇帖子实验了build_sdk.py来编译opencv,失败了。
#本篇尝试使用cmake来编译
#感谢这篇帖子提供的指导:https://www.cnblogs.com/jojodru/p/10025658.html
#
cd /home/ahfu
#选择opencv 3.4.6版本来编译
wget https://github.com/opencv/opencv/archive/3.4.6.tar.gz
tar -zxvf opencv-3.4.6.tar.gz
wget https://codeload.github.com/opencv/opencv_contrib/tar.gz/3.4.6
tar -zxvf opencv_contrib-3.4.6.tar.gz
#下载ndk
wget https://dl.google.com/android/repository/android-ndk-r14b-linux-x86_64.zip
unzip android-ndk-r14b-linux-x86_64.zip
export PATH=$PATH:/home/ahfu/android-ndk-r14b
# 注意,我一开始使用了NDK版本 android-ndk-r19c-linux-x86_64.zip,结果一堆问题
#下载ANDROID SDK
wget https://dl.google.com/android/repository/tools_r25.2.5-linux.zip
unzip tools_r25.2.5-linux.zip
export PATH=$PATH:/home/ahfu/
# 注意,一开始我下载了android sdk版本 android-sdk_r24.4.1-linux.tgz,有问题

#建立编译目录
cd opencv-3.4.6
mkdir build && cd build
#开始config
cmake -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON \
-DCMAKE_TOOLCHAIN_FILE="/home/ahfu/opencv-3.4.6/platforms/android/android.toolchain.cmake" \
-DANDROID_NDK="/home/ahfu/android-ndk-r14b" \
-D ANDROID_SDK="/home/ahfu/android_sdk/tools" \
-DANDROID_NATIVE_API_LEVEL=24 \
-DANDROID_ABI="armeabi-v7a" \
-DANDROID_CPP_FEATURES="rtti exceptions" \
-DANDROID_ARM_NEON=TRUE \
-DANDROID_STL=gnustl_static \
-DCMAKE_BUILD_TYPE=Release \
-DOPENCV_EXTRA_MODULES_PATH="/home/ahfu/opencv_contrib-3.4.6/modules" \
-DCMAKE_INSTALL_PREFIX="/home/ahfu/opencv_3.4.6_install/" \
-D BUILD_opencv_java=OFF \
-D BUILD_ANDROID_PROJECTS=OFF \
-D BUILD_ANDROID_EXAMPLES=OFF \
-D BUILD_DOCS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_opencv_features2d=OFF \
-D BUILD_FAT_JAVA_LIB=OFF \
-D BUILD_opencv_aruco=OFF \
-D BUILD_opencv_calib3d=OFF \
-D WITH_OPENCL=OFF \
-D WITH_CUDA=OFF \
-D WITH_MATLAB=OFF \
../
#或者指定所有组件的选项

cmake -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON \
          -DCMAKE_TOOLCHAIN_FILE="/home/ahfu/opencv-3.4.6/platforms/android/android.toolchain.cmake" \
          -DANDROID_NDK="/home/ahfu/android-ndk-r14b" \
          -DANDROID_NATIVE_API_LEVEL=24 \
          -DANDROID_ABI="armeabi-v7a" \
          -DANDROID_CPP_FEATURES="rtti exceptions" \
          -DANDROID_ARM_NEON=TRUE \
          -DANDROID_STL=gnustl_static \
          -DCMAKE_BUILD_TYPE=Release \
          -DOPENCV_EXTRA_MODULES_PATH="/home/ahfu/opencv_contrib-3.4.6/modules"  \
          -DCMAKE_INSTALL_PREFIX="/home/ahfu/opencv_3.4.6_install/" \
          -D BUILD_opencv_java=OFF \
          -D BUILD_ANDROID_PROJECTS=OFF \
          -D BUILD_ANDROID_EXAMPLES=OFF \
          -D BUILD_DOCS=OFF \
          -D BUILD_PERF_TESTS=OFF \
          -D BUILD_TESTS=OFF \          
          -D BUILD_FAT_JAVA_LIB=OFF \      
-D  BUILD_opencv_apps    =ON \
-D  BUILD_opencv_aruco    =ON \
-D  BUILD_opencv_bgsegm   =ON \
-D  BUILD_opencv_bioinspired  =ON \
-D  BUILD_opencv_calib3d   =OFF \
-D  BUILD_opencv_ccalib   =ON \
-D  BUILD_opencv_core    =ON \
-D  BUILD_opencv_datasets   =ON \
-D  BUILD_opencv_dnn    =ON \
-D  BUILD_opencv_dnn_objdetect  =ON \
-D  BUILD_opencv_dpm    =ON \
-D  BUILD_opencv_face    =ON \
-D  BUILD_opencv_features2d  =OFF \
-D  BUILD_opencv_flann    =ON \
-D  BUILD_opencv_fuzzy    =ON \
-D  BUILD_opencv_hfs    =ON \
-D  BUILD_opencv_highgui   =OFF \
-D  BUILD_opencv_img_hash   =ON \
-D  BUILD_opencv_imgcodecs   =ON \
-D  BUILD_opencv_imgproc   =ON \
-D  BUILD_opencv_java_bindings_gen =OFF \
-D  BUILD_opencv_js    =OFF \
-D  BUILD_opencv_line_descriptor =ON \
-D  BUILD_opencv_ml    =ON \
-D  BUILD_opencv_objdetect   =ON \
-D  BUILD_opencv_optflow   =ON \
-D  BUILD_opencv_phase_unwrapping =ON \
-D  BUILD_opencv_photo    =ON \
-D  BUILD_opencv_plot    =ON \
-D  BUILD_opencv_reg  =ON \
-D  BUILD_opencv_rgbd    =ON \
-D  BUILD_opencv_saliency   =ON \
-D  BUILD_opencv_shape    =ON \
-D  BUILD_opencv_stereo   =ON \
-D  BUILD_opencv_stitching  =ON \
-D  BUILD_opencv_structured_light =ON \
-D  BUILD_opencv_superres   =ON \
-D  BUILD_opencv_surface_matching =ON \
-D  BUILD_opencv_text    =ON \
-D  BUILD_opencv_tracking   =ON \
-D  BUILD_opencv_video   =ON \
-D  BUILD_opencv_videoio   =ON \
-D  BUILD_opencv_videostab  =ON \
-D  BUILD_opencv_world   =ON \
-D  BUILD_opencv_xfeatures2d  =OFF \
-D  BUILD_opencv_ximgproc   =ON \
-D  BUILD_opencv_xobjdetect  =ON \
-D  BUILD_opencv_xphoto =ON \
-D  WITH_ARITH_DEC    =ON \
-D  WITH_ARITH_ENC    =ON \
-D  WITH_CAROTENE     =ON \
-D  WITH_CLP      =ON \
-D  WITH_CPUFEATURES    =ON \
-D  WITH_CUDA      =OFF \
-D  WITH_FFMPEG     =ON \
-D  WITH_GDCM      =ON \
-D  WITH_GSTREAMER_0_10   =ON \
-D  WITH_HALIDE     =ON \
-D  WITH_IMGCODEC_HDR    =ON \
-D  WITH_IMGCODEC_PXM    =ON \
-D  WITH_IMGCODEC_SUNRASTER  =ON \
-D  WITH_INF_ENGINE  =ON \
-D  WITH_ITT     =ON \
-D  WITH_JASPER    =ON \
-D  WITH_JPEG     =ON \
-D  WITH_OPENCL    =OFF \
-D  WITH_OPENCL_SVM   =OFF \
-D  WITH_OPENEXR    =ON \
-D  WITH_OPENMP    =ON \
-D  WITH_OPENVX    =ON \
-D  WITH_PNG     =ON \
-D  WITH_PROTOBUF    =ON \
-D  WITH_PTHREADS_PF   =ON \
-D  WITH_QUIRC     =ON \
-D  WITH_TBB     =ON \
-D  WITH_TIFF     =ON \
-D  WITH_WEBP  =ON \
          ../

  


#执行完成后,如果需要进一步配置更多选项,可以执行
ccmake ..
#开始编译
make -j 40
make install

 

下面是我的用于测试的程序:

//jpg2bmp.cpp
#include 
#include 
#include 
#include 

#ifndef P
#define P(format, ...) do \
  { \
    printf("%s %s %d " format "\n", __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
    fflush(stdout); \
  } \
  while (0);
#endif

int main(int argc, char* argv[]){
    if (argc<3){
        printf("usage:%s  \n", argv[0]);
        return 0;
    }
    cv::Mat image = cv::imread(argv[1], cv::IMREAD_UNCHANGED);
    if (image.empty()){
        P("load pic %s fail", argv[1]);
        return -1;
    }
    cv::Mat bmp;
    cv::cvtColor(image, bmp, cv::COLOR_BGR2GRAY);   
    cv::imwrite(argv[2], bmp);
    P("ok");
    return 1;
}

/*
ndk-build NDK_LOG=1 V=1 NDK_DEBUG=1 NDK_PROJECT_PATH=$(pwd) NDK_APPLICATION_MK=./Application.mk APP_BUILD_SCRIPT=./Android.mk
*/

 

同一目录下用于编译的Application.mk

APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions -std=c++11 -fPIC
#注意:-fPIC一定要加,否则可执行程序在android上执行出现以下错误:
#     Android 5.0 and later only support position-independent executables (-fPIE).

APP_PLATFORM := android-24
# 注意:APP_PLATFORM一定要等于或者高于opencv编译时候的版本
#      否则链接的时候出现大量类似错误  undefined reference to 'rand'
APP_ABI := armeabi-v7a

 

Android.mk的内容:

include $(CLEAR_VARS)

LOCAL_PATH := ./


LOCAL_C_INCLUDES:=/home/ahfu/opencv_3.4.6_install/sdk/native/jni/include

LOCAL_ARM_NEON := true      
LOCAL_CFLAGS:=-g -pie -fPIC -DANDROID_ARM_NEON=TRUE -DANDROID_TOOLCHAIN=clang
LOCAL_LDLIBS +=  \
  -L"/home/ahfu/opencv_3.4.6_install/sdk/native/staticlibs/armeabi-v7a" -lopencv_core -lopencv_imgcodecs -lopencv_imgproc -lm -lz -landroid -llog \
  -L"/home/ahfu/opencv_3.4.6_install/sdk/native/3rdparty/libs/armeabi-v7a" -ltegra_hal -lcpufeatures -llibpng -llibjpeg-turbo -llibtiff -llibjasper -llibwebp -lIlmImf \
  -lopencv_core -lopencv_imgcodecs -lopencv_imgproc -ltegra_hal \
  -L"/home/ahfu/android-ndk-r14b/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/" \
  -lgnustl_static -ldl
#注意:链接阶段出现了大量stl链接时候的错误
#     不明原因,但是我强制指定了STL的链接目录,问题解决
#     链接目录:android-ndk-r14b/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/
#     链接库  -lgnustl_static

LOCAL_MODULE := jpg2bmp

LOCAL_SRC_FILES := jpg2bmp.cpp


LOCAL_LDLIBS +=  -lm -llog
include $(BUILD_EXECUTABLE)

  

使用ndk-build命令编译:
ndk-build NDK_LOG=1 V=1 NDK_DEBUG=1 NDK_PROJECT_PATH=$(pwd) NDK_APPLICATION_MK=./Application.mk APP_BUILD_SCRIPT=./Android.mk


#下载到mac上,然后再传到android
sz -y libs/armeabi-v7a/jpg2bmp

 

#以下是mac终端
adb push ~/Downloads/jpg2bmp 1.jpg /data/local/tmp
adb shell
   cd /data/local/tmp
   ./jpg2bmp 1.jpg 2.bmp
   exit
adb pull /data/local/tmp/2.bmp ./
open 2.bmp
#看到一张bmp格式的灰度图,执行成功

 

 

 

 

  

转载于:https://www.cnblogs.com/ahfuzhang/p/11069832.html

你可能感兴趣的:(在linux下编译android下的opencv,使用cmake的方法)