Android NDK交叉编译openCV

2021-10-20  本文已影响0人  招金

下载openCV

下载NDK交叉编译链

注意android-ndk-r16b-linux-x86_64.zip编译测试程序时候会link出错,建议用r18b

下载android SDK

把脚本build.sh放入openCV根目录
#!/bin/bash
NDK_ROOT="/home/kaober/disk4T1/raojun3/android-ndk-r18b"
SDK_ROOT="/home/kaober/disk4T1/raojun3/android-sdk-linux"
### ABIs setup
ANDROID_ABI_LIST=("arm64-v8a" "armeabi-v7a")

### path setup
SCRIPT=$(readlink -f $0)
WD=`dirname $SCRIPT`
OPENCV_ROOT="${WD}"
N_JOBS=48

INSTALL_DIR="${WD}/build"
rm -rf "${INSTALL_DIR}"

### Make each ABI target iteratly and sequentially
for i in "${ANDROID_ABI_LIST[@]}"
do
    ANDROID_ABI="${i}"
    echo "Start building ${ANDROID_ABI} version"

    if [ "${ANDROID_ABI}" = "armeabi" ]; then
        API_LEVEL=19
    else
        API_LEVEL=24
    fi

    temp_build_dir="${OPENCV_ROOT}/platforms/build_android_${ANDROID_ABI}"

    ### Remove the build folder first, and create it
    rm -rf "${temp_build_dir}"
    mkdir -p "${temp_build_dir}"
    cd "${temp_build_dir}"

    echo ${INSTALL_DIR}
    cmake \
        -DCMAKE_MAKE_PROGRAM=/usr/bin/make \
        -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON \
        -DCMAKE_TOOLCHAIN_FILE=${NDK_ROOT}/build/cmake/android.toolchain.cmake \
        -DANDROID_ABI=${ANDROID_ABI} \
        -DANDROID_STL=c++_shared  \
        -DCMAKE_BUILD_TYPE=Release \
        -DANDROID_NATIVE_API_LEVEL=${API_LEVEL} \
        -DCMAKE_BUILD_TYPE=Release \
        -DWITH_FFMPEG=ON \
        -D BUILD_opencv_java=ON \
        -D BUILD_ANDROID_PROJECTS=ON \
        -D WITH_CUDA=OFF \
        -D WITH_MATLAB=OFF \
        -D BUILD_ANDROID_EXAMPLES=OFF \
        -D BUILD_DOCS=OFF \
        -D BUILD_PERF_TESTS=OFF \
        -D BUILD_TESTS=OFF \
        -DCMAKE_INSTALL_PREFIX="${INSTALL_DIR}" \
        -DANDROID_SDK=${SDK_ROOT} \
        ../..

    # Build it
    make -j${N_JOBS}

    # Install it
    make install/strip

    ### Remove temp build folder
    cd "${WD}"
    rm -rf "${temp_build_dir}"
    echo "end building ${ANDROID_ABI} version"
done

运行build.sh

build下就生成我们需要的OpenCV sdk。

验证SDK

编写测试cpp
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
 
#define CV_CXX_STD_ARRAY 0
#include "opencv2/highgui/highgui.hpp" 
#include "opencv2/imgproc/imgproc.hpp" //OpenCV 图像处理头文件 
 
using namespace cv; // 包含 cv 命名空间
using namespace std;
 
int main()
{
    // 读取图片
    Mat src = imread("/sdcard/1.jpg", 1);
    Mat dst;
 
    // 彩色转为灰度图片
    cvtColor(src,dst,CV_BGR2GRAY);
    imwrite("/sdcard/2.jpg",dst);
    
    src.release();
    dst.release();
    
    return 0;
}
构建CMakeLists.txt
cmake_minimum_required(VERSION 2.8.3)
project(main_arm)

add_definitions("-Wall -g")

set(OpenCVHome /home/kaober/disk4T1/raojun3/opencv-3.4.6/build/sdk/native)

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
        libtiff
        libwebp
        # quirc
        log
        z
    )

add_executable(${PROJECT_NAME} main.cpp)

install(TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION ${PROJECT_SOURCE_DIR})

编译build.sh

#/bin/bash

export ANDROID_NDK=/home/kaober/disk4T1/raojun3/android-ndk-r18b

rm -r build
mkdir build && cd build 

cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
    -DANDROID_ABI="armeabi-v7a" \
    -DANDROID_NDK=$ANDROID_NDK \
    -DANDROID_PLATFORM=android-26 \
    ..

make && make install

cd ..

运行build.sh

1634720693(1).png

编译完成

验证

验证成功

如果需要验证libopencv_java3.so动态库

用如下CMakeLists.txt编译运行程序

cmake_minimum_required(VERSION 2.8.3)
project(main_arm)

add_definitions("-Wall -g")

set(OpenCVHome /home/kaober/disk4T1/raojun3/opencv-3.4.6/build/sdk/native)

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

link_libraries(
        c++_static
        opencv_java3
        log
        z
    )

add_executable(${PROJECT_NAME} main.cpp)

install(TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION ${PROJECT_SOURCE_DIR})

如果运行main_arm 提示找不到libc++_shared.so库,不要用ndk的库拷贝进系统,用系统的libstdc++.so一份改名libc++_shared.so (android8.0验证)
上一篇下一篇

猜你喜欢

热点阅读