热门标签 | HotTags
当前位置:  开发笔记 > 前端 > 正文

Android引入OpenCV的示例

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

简介

在移动开发中,如果我们要实现一些图像处理相关的功能,难免要用到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 
#include 
#include 
#include 
#include 
#include
#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 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方法时,我们需要路径名匹配。

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


推荐阅读
  • SvpplyTable: 实现可扩展和可折叠的菜单动画
    SvpplyTable 是一个示例项目,旨在实现类似 Svpply 应用程序中的可扩展和可折叠的菜单动画效果。该项目托管在 GitHub 上,地址为 https://github.com/liuminqian/SvpplyTable。 ... [详细]
  • 阿里云 Aliplayer高级功能介绍(八):安全播放
    如何保障视频内容的安全,不被盗链、非法下载和传播,阿里云视频点播已经有一套完善的机 ... [详细]
  • 本文介绍了 Confluence 6 中使用的其他 Cookie,这些 Cookie 主要用于存储产品的基本持久性和用户偏好设置,以提升用户体验。 ... [详细]
  • 2017年5月9日学习总结
    本文记录了2017年5月9日的学习内容,包括技术分享和相关知识点的深入探讨。 ... [详细]
  • 本文章提供了适用于 Cacti 的多核 CPU 监控模板,支持 2、4、8、12、16、24 和 32 核配置。请注意,0.87g 版本的 Cacti 需要手动修改哈希值为 0021 才能使用,而 0.88 及以上版本则可直接导入。 ... [详细]
  • Python函数的高级用法[python基础]
    Python的函数也是一种值:所有函数都是function对象,这意味着可以把函数本身赋值给变量,就像把整数、浮点数、列表、元组赋值给变量一样;同样可以使用函数作为函数的形参,也可 ... [详细]
  • 解决Jenkins编译过程中ERROR: Failed to Parse POMs的问题
    在使用Jenkins进行自动化构建时,有时会遇到“ERROR: Failed to parse POMs”的错误。本文将详细分析该问题的原因,并提供有效的解决方案。 ... [详细]
  • SSM框架下的在线教育平台源代码与文档
    本文介绍了基于SSM(Spring, Spring MVC, MyBatis)框架开发的教务网络管理系统。该系统旨在有效管理学生各学年的成绩,支持快速查询学生成绩和班级信息。系统具备完善的需求分析、设计和实现方案,确保数据的安全性和系统的易用性。 ... [详细]
  • 本文介绍了如何在 Qt 应用程序中实现状态栏、浮动窗口(铆接部件)和中心部件。通过简单的代码示例,详细解释了每个组件的创建和设置方法。 ... [详细]
  • centos 7.0 lnmp成功安装过程(很乱)
    下载nginx[rootlocalhostsrc]#wgethttp:nginx.orgdownloadnginx-1.7.9.tar.gz--2015-01-2412:55:2 ... [详细]
  • 解决MAC远程桌面连接证书无效问题
    本文介绍如何解决在使用MAC远程桌面连接时遇到的“证书或相关链无效”错误。通过详细步骤和专业建议,帮助用户顺利连接远程桌面。 ... [详细]
  • 使用Tkinter构建51Ape无损音乐爬虫UI
    本文介绍了如何使用Python的内置模块Tkinter来构建一个简单的用户界面,用于爬取51Ape网站上的无损音乐百度云链接。虽然Tkinter入门相对简单,但在实际开发过程中由于文档不足可能会带来一些不便。 ... [详细]
  • MongoDB核心概念详解
    本文介绍了NoSQL数据库的概念及其应用场景,重点解析了MongoDB的基本特性、数据结构以及常用操作。MongoDB是一个高性能、高可用且易于扩展的文档数据库系统。 ... [详细]
  • 第七卷《英才》强调,即使有良药,若不对症,效果也不如低等药物;即使有贤才,若不适用,效果也不如普通人。文中通过多个比喻,阐述了人才使用的重要性,指出合适的人才在适当的时间和场合才能发挥最大效用。 ... [详细]
  • packagecom.panchan.tsmese.utils;importjava.lang.reflect.ParameterizedType;importjava.lang. ... [详细]
author-avatar
IDC小林
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有