android studio opencv c++ jni环境集成教程 无需opencv manager

背景:c++代码移植到android.  c++代码用到opencv jni库,花了很长时间,希望能帮到大家

前期准备:

opencv-3.4.1-android-sdk 自行官网下载


开始:

1. 新建一个support c++ android 项目

2. import module

android studio opencv c++ jni环境集成教程 无需opencv manager_第1张图片

3. 配置gradle和 当前项目一致。

android studio opencv c++ jni环境集成教程 无需opencv manager_第2张图片android studio opencv c++ jni环境集成教程 无需opencv manager_第3张图片


4.依赖

android studio opencv c++ jni环境集成教程 无需opencv manager_第4张图片


5. main 目录下新建jniLibs 文件夹,打开 从opencv 官网下载的 OpenCV-android-sdk\sdk\native\libs  把libs 下的所有文件复制到 jniLibs里面。

android studio opencv c++ jni环境集成教程 无需opencv manager_第5张图片


 注:不同文件夹对应不同cpu架构,集成opencv 2.4版本 会出现匹配不全的情况。若明确设备cpu架构,可添加过滤减少apk体积,如图

android studio opencv c++ jni环境集成教程 无需opencv manager_第6张图片


6. 简单调用:

public class ExampleActivity extends AppCompatActivity  implements CameraBridgeViewBase.CvCameraViewListener2 {

    private static final String TAG = "ExampleActivity";
    private CameraBridgeViewBase mOpenCvCameraView;

    BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case BaseLoaderCallback.SUCCESS:
                    Log.i("msg", "成功加载");
                    mOpenCvCameraView.enableView();
                    break;
                default:
                    super.onManagerConnected(status);
                    Log.i("msg", "加载失败");
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.fd_activity_surface_view);
        mOpenCvCameraView.setVisibility(CameraBridgeViewBase.VISIBLE);
        mOpenCvCameraView.setCvCameraViewListener(this);

    }
    @Override
    protected void onResume() {
        super.onResume();
        if (!OpenCVLoader.initDebug()) {
            Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization");
            //注释掉,无需opencv manager
//            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_13, MainActivity.this, mLoaderCallback);
        } else {
            Log.d(TAG, "OpenCV library found inside package. Using it!");
            mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
        }
    }

    @Override
    public void onCameraViewStarted(int width, int height) {

    }

    @Override
    public void onCameraViewStopped() {

    }

    @Override
    public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
        return inputFrame.rgba();
    }
}

7. 集成c++代码 所需opencv库

(1)打开cmakeList.txt

android studio opencv c++ jni环境集成教程 无需opencv manager_第7张图片

 (2)添加open cv路径


 
  
#需要换成自己的sdk路径 注意复制路径的时候将 "\"换成"/"
set( OpenCV_DIR C:/Users/EDZ/Downloads/opencv-3.4.1-android-sdk/OpenCV-android-sdk/sdk/native/jni )

find_package(OpenCV REQUIRED )
if(OpenCV_FOUND)
include_directories(${OpenCV_INCLUDE_DIRS})
message(STATUS "OpenCV library status:")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")
else(OpenCV_FOUND)
message(FATAL_ERROR "OpenCV library not found")
endif(OpenCV_FOUND)
(3)add_library添加自己的c++文件
android studio opencv c++ jni环境集成教程 无需opencv manager_第8张图片


(4)建立连接
android studio opencv c++ jni环境集成教程 无需opencv manager_第9张图片

这样自己的C++代码 就可以找到所需的opencv头文件

android studio opencv c++ jni环境集成教程 无需opencv manager_第10张图片


希望可以帮到大家。有问题可以留言

 
  

 
  
 
  


你可能感兴趣的:(open,cv)