美文网首页
Android NDK开发配置OpenCV

Android NDK开发配置OpenCV

作者: Rruop旋转 | 来源:发表于2018-12-20 18:10 被阅读0次
image

一、配置环境

1、Android studio 3.1

2、Android NDK

3、Java 8.0

4、opencv-android 3.4.4https://opencv.org/releases.html(下载地址)

二、创建opencv项目

创建一个名为OpencvTest的android project

image

这里需要添加 c++ 支持 关于android ndk 配置https://www.jianshu.com/p/8b67ba7ed598

image

三、opencv配置

        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11 -frtti -fexceptions"
                //添加支持cpu架构
                abiFilters'x86','x86_64','armeabi','arm64-v8a','armeabi-v7a','mips','mips64'
            }
        }
    }

    sourceSets{
        main{
            //当前这个目录下的库文件会被调用并且被打包进apk中
            jniLibs.srcDirs = ['D:/opencv/OpenCV-android-sdk/sdk/native/libs']
        }
    }

CMake讲解https://www.jianshu.com/p/6332418b12b1

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.

#该变量为真时会创建完整版本的Makefile

set(CMAKE_VERBOSE_MAKEFILE on)

#定义变量ocvlibs使后面的命令可以使用定位具体的库文件

set(ocvlibs "D:/opencv/OpenCV-android-sdk/sdk/native/libs")

#调用头文件的具体路径

include_directories(D:/opencv/OpenCV-android-sdk/sdk/native/jni/include)

#增加我们的动态库

add_library(libopencv_java3 SHARED IMPORTED )

#建立链接

set_target_properties(libopencv_java3 PROPERTIES

IMPORTED_LOCATION

        "${ocvlibs}/${ANDROID_ABI}/libopencv_java3.so")

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  libopencv_java3  android log

        # Links the target library to the log library

# included in the NDK.

        ${log-lib} )

budiler project


native-lib.cpp


#include

#include

#include

#include

using namespace cv;

using namespace std;

extern "C"

JNIEXPORT jintArray JNICALL

Java_test_opencv_com_openctbitmapgay_MainActivity_BitmapToGay(JNIEnv *env, jobject instance,

                                                              jintArray p_, jint w, jint h) {

jint *cbuf;

    jboolean ptfalse =false;

    cbuf = env->GetIntArrayElements(p_, &ptfalse);

    if(cbuf ==NULL){

return 0;

    }

Mat imgData(h, w, CV_8UC4, (unsigned char*)cbuf);

    // 注意,Android的Bitmap是ARGB四通道,而不是RGB三通道

    cvtColor(imgData,imgData,CV_BGRA2GRAY);

    cvtColor(imgData,imgData,CV_GRAY2BGRA);

    int size=w * h;

    jintArray result = env->NewIntArray(size);

    env->SetIntArrayRegion(result, 0, size, (jint*)imgData.data);

    env->ReleaseIntArrayElements(p_, cbuf, 0);

    return result;

}

MianActivity.java


public class MainActivityextends AppCompatActivity {

// Used to load the 'native-lib' library on application startup.

    static {

System.loadLibrary("native-lib");

    }

private Bitmapbitmap;

    private Buttonbtn_1;

    private ImageViewimageView;

    private Buttonbtn_2;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        btn_1 = findViewById(R.id.button_1);

        btn_2 = findViewById(R.id.button_2);

        imageView = findViewById(R.id.image);

        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.meinv);

        btn_1.setOnClickListener(new View.OnClickListener() {

@Override

            public void onClick(View view) {

imageView.setImageResource(R.drawable.meinv);

            }

});

        btn_2.setOnClickListener(new View.OnClickListener() {

@Override

            public void onClick(View view) {

int w =bitmap.getWidth();

                int h =bitmap.getHeight();

                int[] piexls =new int[w * h];

                bitmap.getPixels(piexls, 0, w, 0, 0, w, h);

                int[] resultData = BitmapToGay(piexls, w, h);

                Bitmap resultImage = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

                resultImage.setPixels(resultData, 0, w, 0, 0, w, h);

                imageView.setImageBitmap(resultImage);

            }

});

    }

public native int[]BitmapToGay(int[] p, int w, int h);

}

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    tools:context=".MainActivity">

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_weight="1"

        android:orientation="vertical">

            android:id="@+id/image"

            android:layout_width="match_parent"

            android:layout_height="200dp"

            android:layout_weight="1"

            android:src="@drawable/meinv" />

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_weight="1"

        android:orientation="horizontal">

            android:id="@+id/button_2"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="灰度图" />

            android:id="@+id/button_1"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="原图" />

相关文章

网友评论

      本文标题:Android NDK开发配置OpenCV

      本文链接:https://www.haomeiwen.com/subject/gwkukqtx.html