Android引入OpenCV的示例

简介

在移动开发中,如果我们要实现一些图像处理相关的功能,难免要用到OpenCV。而OpenCV是用c++开发的。我们在Android中,需要使用jni的方法去使用它。

引入配置

我们引入jni开发的基本配置方法,已经在另一篇博客中介绍过了,不再赘述。这一次我们无非是要引入第三方的c++库。

首先,我们找到或新建jniLibs文件夹,然后将依赖的动态库和静态库(路径为OpenCV-android-sdk\sdk\native\libs)拷贝到\src\main\jniLibs下面。

然后,找到cpp文件夹。将include文件夹(路径OpenCV-android-sdk\sdk\native\jni\include)拷贝到cpp(路径\app\src\main\cpp)文件夹里。

接着,我们需要引入c++的相关支持。在module的build.gradle里面加入:

externalNativeBuild {
  cmake {
    //arguments '-DANDROID_TOOLCHAIN=clang', '-DANDROID_STL=gnustl_static'
    cppFlags "-std=c++11","-frtti", "-fexceptions"
  }
}
ndk{
  abiFilters 'armeabi-v7a'
}

最后,我们需要在CMakeLists.txt中,加入opencv相关的声明。由于配置比较复杂,在此直接将demo的CMakeLists.txt的内容贴到这里。

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

set(CMAKE_VERBOSE_MAKEFILE on)
set(libs "${CMAKE_SOURCE_DIR}/src/main/jniLibs")
include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/include)

add_library(libopencv_java3 SHARED IMPORTED )
set_target_properties(libopencv_java3 PROPERTIES
  IMPORTED_LOCATION "${libs}/${ANDROID_ABI}/libopencv_java3.so")

add_library(libopencv_calib3d STATIC IMPORTED )
set_target_properties(libopencv_calib3d PROPERTIES
  IMPORTED_LOCATION "${libs}/${ANDROID_ABI}/libopencv_calib3d.a")

add_library(libopencv_core STATIC IMPORTED )
set_target_properties(libopencv_core PROPERTIES
  IMPORTED_LOCATION "${libs}/${ANDROID_ABI}/libopencv_core.a")

add_library(libopencv_features2d STATIC IMPORTED )
set_target_properties(libopencv_features2d PROPERTIES
  IMPORTED_LOCATION "${libs}/${ANDROID_ABI}/libopencv_features2d.a")

add_library(libopencv_flann STATIC IMPORTED )
set_target_properties(libopencv_flann PROPERTIES
  IMPORTED_LOCATION "${libs}/${ANDROID_ABI}/libopencv_flann.a")

add_library(libopencv_highgui STATIC IMPORTED )
set_target_properties(libopencv_highgui PROPERTIES
  IMPORTED_LOCATION "${libs}/${ANDROID_ABI}/libopencv_highgui.a")

add_library(libopencv_imgcodecs STATIC IMPORTED )
set_target_properties(libopencv_imgcodecs PROPERTIES
  IMPORTED_LOCATION "${libs}/${ANDROID_ABI}/libopencv_imgcodecs.a")

add_library(libopencv_imgproc STATIC IMPORTED )
set_target_properties(libopencv_imgproc PROPERTIES
  IMPORTED_LOCATION "${libs}/${ANDROID_ABI}/libopencv_imgproc.a")

add_library(libopencv_ml STATIC IMPORTED )
set_target_properties(libopencv_ml PROPERTIES
  IMPORTED_LOCATION "${libs}/${ANDROID_ABI}/libopencv_ml.a")

add_library(libopencv_objdetect STATIC IMPORTED )
set_target_properties(libopencv_objdetect PROPERTIES
  IMPORTED_LOCATION "${libs}/${ANDROID_ABI}/libopencv_objdetect.a")

add_library(libopencv_photo STATIC IMPORTED )
set_target_properties(libopencv_photo PROPERTIES
  IMPORTED_LOCATION "${libs}/${ANDROID_ABI}/libopencv_photo.a")

add_library(libopencv_shape STATIC IMPORTED )
set_target_properties(libopencv_shape PROPERTIES
  IMPORTED_LOCATION "${libs}/${ANDROID_ABI}/libopencv_shape.a")

add_library(libopencv_stitching STATIC IMPORTED )
set_target_properties(libopencv_stitching PROPERTIES
  IMPORTED_LOCATION "${libs}/${ANDROID_ABI}/libopencv_stitching.a")

add_library(libopencv_superres STATIC IMPORTED )
set_target_properties(libopencv_superres PROPERTIES
  IMPORTED_LOCATION "${libs}/${ANDROID_ABI}/libopencv_superres.a")

add_library(libopencv_video STATIC IMPORTED )
set_target_properties(libopencv_video PROPERTIES
  IMPORTED_LOCATION "${libs}/${ANDROID_ABI}/libopencv_video.a")

add_library(libopencv_videoio STATIC IMPORTED )
set_target_properties(libopencv_videoio PROPERTIES
  IMPORTED_LOCATION "${libs}/${ANDROID_ABI}/libopencv_videoio.a")

add_library(libopencv_videostab STATIC IMPORTED )
set_target_properties(libopencv_videostab PROPERTIES
  IMPORTED_LOCATION "${libs}/${ANDROID_ABI}/libopencv_videostab.a")

add_library(libopencv_ts STATIC IMPORTED )
set_target_properties(libopencv_ts PROPERTIES
  IMPORTED_LOCATION "${libs}/${ANDROID_ABI}/libopencv_ts.a")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11 -fexceptions -frtti")

#include_directories(D:/Projects/Android/CLMAndroid/OpenCV-android-sdk/sdk/native/jni/include )
#set(OpenCV_DIR D:/Projects/Android/CLMAndroid/OpenCV-android-sdk/sdk/native/jni)
#find_package(OpenCV REQUIRED)
#target_link_libraries(${OpenCV_LIBS})

add_library( # Sets the name of the library.
       native-lib

       # Sets the library as a shared library.
       SHARED

       # Provides a relative path to your source file(s).
       src/main/cpp/native-lib.cpp )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
       log-lib

       # Specifies the name of the NDK library that
       # you want CMake to locate.
       log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
            native-lib android log
  libopencv_java3 libopencv_calib3d libopencv_core libopencv_features2d libopencv_flann libopencv_highgui libopencv_imgcodecs
  libopencv_imgproc libopencv_ml libopencv_objdetect libopencv_photo libopencv_shape libopencv_stitching libopencv_superres
  libopencv_video libopencv_videoio libopencv_videostab
            # Links the target library to the log library
            # included in the NDK.
            ${log-lib} )

这样配置之后,我们就可以在cpp中,直接使用OpenCV库了。在此贴一个简单的均值滤波:

#include <jni.h>
#include <string>
#include <opencv2/opencv.hpp>
#include <bits/stdc++.h>
#include <android/log.h>
#include<opencv2/core/core.hpp>
#include"opencv2/imgproc/imgproc.hpp"
using namespace cv;
using namespace std;
#define MAKE_ARGB(a, r, g, b) ((a&0xff)<<24) | ((r&0xff)<<16) | ((g&0xff)<<8) | (b&0xff)
#define MAKE_RGB565(r, g, b) ((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3))
extern "C"
{
  JNIEXPORT jintArray JNICALL
  Java_com_live_longsiyang_opencvdemo_OpenCVNativeManager_getBlurImage(JNIEnv *env, jobject thiz,
                                     jintArray pixels_, jint w,
                                     jint h) {
    int colorType = CV_8UC4;
    jint *pixels = env->GetIntArrayElements(pixels_, NULL);
    Mat imgMat(h, w, colorType, pixels);
    Mat out(h, w, colorType);
    blur(imgMat, out, Size(20, 20));
    env->ReleaseIntArrayElements(pixels_, pixels, 0);
    int *outIntImage = new int[w * h];
    for (int i = 0; i < w * h; i++) {
      int a, r, g, b;
      for (int j = 0; j < 4; j++) {
        a = out.data[i * 4 + 3];
        r = out.data[i * 4 + 2];
        g = out.data[i * 4 + 1];
        b = out.data[i * 4 + 0];
        outIntImage[i] = (int) out.data[i * 4 + j];
      }
      outIntImage[i] = MAKE_ARGB(a,r,g,b);

    }
    jintArray result = env->NewIntArray(w * h);
    env->SetIntArrayRegion(result, 0, w * h, outIntImage);
    return result;
  }
}

这样我们就能看到一个基本的模糊效果的demo。

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
  static {
    System.loadLibrary("native-lib");
  }
  ImageView mIvTest;
  Button mBtnTest;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mIvTest = (ImageView) findViewById(R.id.img_test);
    mIvTest.setImageResource(R.drawable.beauty_steward_course_video_thumb_icon);
    mBtnTest = (Button) findViewById(R.id.btn_test);
    mBtnTest.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.beauty_steward_course_video_thumb_icon);
        int w = srcBitmap.getWidth();
        int h = srcBitmap.getHeight();
        int[] pixels = new int[w*h];
        srcBitmap.getPixels(pixels,0,w,0,0,w,h);
        int[] outPixels = OpenCVNativeManager.getBlurImage(pixels ,w,h);
        Bitmap destBitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
        destBitmap.setPixels(outPixels,0,w,0,0,w,h);
        mIvTest.setImageBitmap(destBitmap);

      }
    });
    findViewById(R.id.btn_reset).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mIvTest.setImageResource(R.drawable.beauty_steward_course_video_thumb_icon);
      }
    });
  }
}

导出.so

在build中找到这个so,这就是我们需要的文件。此外,由于我们使用了opencv相关的库,我们还需要将opencv的相关文件复制出来。将其导入我们需要的工程。

导入之后,我们只需要在module的build.gradle里面加入:

android {
  ...

  sourceSets {
    main {
      jniLibs.srcDirs = ['src/main/jniLibs']
      aidl.srcDirs = ['src/main/aidl']
    }
  }
}

之后,我们就可以直接调用

System.loadLibrary("native-lib");

值得注意的是,JNI的方法是与路径名相关的,所以当我们以这样的方法调用native方法时,我们需要路径名匹配。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

您可能感兴趣的文章:

  • Android Studio中配置OpenCV库开发环境的教程
(0)

相关推荐

  • Android Studio中配置OpenCV库开发环境的教程

    1.下载 进入官网(http://opencv.org/)下载OpenCV4Android并解压.目录结构如下图所示. 其中,sdk目录即是我们开发opencv所需要的类库:samples目录中存放着若干opencv应用示例(包括人脸检测等),可为我们进行android下的opencv开发提供参考:doc目录为opencv类库的使用说明及api文档等:而apk目录则存放着对应于各内核版本的OpenCV_2.4.3.2_Manager_2.4应用安装包.此应用用来管理手机设备中的opencv类库,

  • Android引入OpenCV的示例

    简介 在移动开发中,如果我们要实现一些图像处理相关的功能,难免要用到OpenCV.而OpenCV是用c++开发的.我们在Android中,需要使用jni的方法去使用它. 引入配置 我们引入jni开发的基本配置方法,已经在另一篇博客中介绍过了,不再赘述.这一次我们无非是要引入第三方的c++库. 首先,我们找到或新建jniLibs文件夹,然后将依赖的动态库和静态库(路径为OpenCV-android-sdk\sdk\native\libs)拷贝到\src\main\jniLibs下面. 然后,找到c

  • OpenCV在Android上的应用示例

    一. OpenCV 介绍 OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux.Windows.Android和Mac OS操作系统上.它轻量级而且高效--由一系列 C 函数和少量 C++ 类构成,同时提供了Python.Ruby.MATLAB等语言的接口,实现了图像处理和计算机视觉方面的很多通用算法. 在移动端上使用 OpenCV 可以完成一系列图像处理的工作. 二. OpenCV 在 Android 上的配置 我在项目中使用的 OpenCV 版本是 4.x.

  • 基于Android引入IjkPlayer无法播放mkv格式视频的解决方法

    写在前面 项目中直接引用或者直接编译源码得到的ijkplayer在播放mkv文件时出现(-10000)的错误,去项目github查看了才知道,默认是不支持mkv和rmvb格式视频的播放的. 用了一天时间解决(为什么用了一天,因为我蠢啊),这里记录一下解决的方法(官方上面其实已经有了详细的教程,无奈我当时没有很认真看.)这里为我自己这个新手做个记录: 仍然是采用编译源码的方式引入,只是需要按照官方的方法更改一下脚本文件 ijkplayer官方地址:https://github.com/Bilibi

  • Python安装OpenCV的示例代码

    OpenCV介绍 OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux.Windows.Android和Mac OS操作系统上.它轻量级而且高效--由一系列 C 函数和少量 C++ 类构成,同时提供了Python.Ruby.MATLAB等语言的接口,实现了图像处理和计算机视觉方面的很多通用算法. OpenCV用C++语言编写,它的主要接口也是C++语言,但是依然保留了大量的C语言接口.该库也有大量的Python.Java and MATLAB/OCTAVE(版本

  • kotlin android extensions 插件实现示例详解

    目录 前言 原理浅析 总体结构 源码分析 插件入口 配置编译器插件传参 编译器插件接收参数 注册各种Extension IrGenerationExtension ExpressionCodegenExtension StorageComponentContainerContributor ClassBuilderInterceptorExtension PackageFragmentProviderExtension 总结 前言 kotlin-android-extensions 插件是 Ko

  • ImageView 实现Android colorPikcer 选择器的示例代码

    本文介绍了ImageView 实现Android colorPikcer 选择器的示例代码,分享给大家,具体如下: Android colorPikcer 选择器 环形的ColorPicker,主要思路是: Color 选在放在ImageView 的background上面,根据点击的位置判断选择的颜色. 重写onTouch,在onTouch 里面判断点击点的颜色. 根据当前选择的颜色设置图片的src. 获取Bitmap 在 ColorPickerView 构造函数中初始化 Bitmap.因为g

  • Android 中 ThreadLocal使用示例

    Android 中 ThreadLocal使用示例 概要: Demo描述:  ThreadLocal使用示例. 关于ThreadLocal的官方文档描述 Implements a thread-local storage, that is, a variable for which each thread has its own value. All threads share the same ThreadLocal object, but each sees a different valu

  • Android 曲线图的绘制示例代码

    本文介绍了Android 曲线图的绘制示例代码,分享给大家,具体如下: 效果展示 效果展示.gif 使用方式 // 初始化数据表格相关 with(mTableView) { // 配置坐标系 setupCoordinator("日", "人", /*这里是横坐标的值*/0f, 5f, 10f, 15f, 20f, 25f, 30f) // 添加曲线, 确保纵坐标的数值位数相等 addWave(ContextCompat.getColor(this@MainActiv

  • Android进程间通信实践的示例代码

    本文介绍了Android进程间通信实践的示例代码,分享给大家,具体如下: 因为线程间的内存是共享的,所以它们之间的通信简单,比如可以通过共享变量等方式实现.而进程间想要通信就要麻烦许多了.要想实现进程间通信,我们需要在不同进程之间定义一套它们可以共同理解的接口描述语言,也即 IDL.比较常用的 IDL 有 JSON.Protocol Buffers 等.而 Android 不同进程之间的通信也有个特别的语言,叫 AIDL(Android Interface Definition Language

  • Android 滚动时间选择的示例代码

    效果图 复制代码直接用!!!! 1.导入依赖 implementation 'com.bigkoo:pickerview:2.1.0' 2.三个bean类 PickerViewData public class PickerViewData implements IPickerViewData { private String content; public PickerViewData(String content) { this.content = content; } public voi

随机推荐