opencv

Android 通过JNI调用opencv

环境:adt windows + NDK +  opencv

https://opencv.org/ 下载opencv-3.4.0-android-sdk.zip

配置基本的android 调用jni环境(这里省略),然后在此基础上修改

// Android.mk
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

OpenCV_INSTALL_MODULES := on
OpenCV_CAMERA_MODULES := off
OPENCV_LIB_TYPE :=STATIC

include $(LOCAL_PATH)/native/jni/OpenCV.mk

LOCAL_MODULE    := daemon
LOCAL_SRC_FILES := daemon_process.cpp
LOCAL_LDLIBS    += -lm -llog

include $(BUILD_SHARED_LIBRARY)

 

#Application.mk
APP_ABI := all
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions

 

//daemon_process.cpp
#include <string.h>
#include <android/log.h>
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/select.h>
#include <pthread.h>
#include <dlfcn.h>
#include <opencv2/opencv.hpp>

using namespace cv;

#define TAG "Test"

#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,TAG ,__VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,TAG ,__VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,TAG ,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,TAG ,__VA_ARGS__)
#define LOGF(...) __android_log_print(ANDROID_LOG_FATAL,TAG ,__VA_ARGS__)

#ifdef __cplusplus
extern "C" {
#endif

void Java_com_example_opencv_MainActivity_test(JNIEnv* env, jobject thiz) {
    LOGD("----------native----test()");
}

jintArray Java_com_example_opencv_MainActivity_gray(JNIEnv* env, jobject thiz,
        jintArray buf, int w, int h) {
    LOGD("----------native----gray()");

    jint *cbuf;
    cbuf = env->GetIntArrayElements(buf, JNI_FALSE);
    if (cbuf == NULL) {
        return 0;
    }

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

    uchar* ptr = imgData.ptr(0);
    for (int i = 0; i < w * h; i++) {
        int grayScale = (int) (ptr[4 * i + 2] * 0.299 + ptr[4 * i + 1] * 0.587
                + ptr[4 * i + 0] * 0.114);
        ptr[4 * i + 1] = grayScale;
        ptr[4 * i + 2] = grayScale;
        ptr[4 * i + 0] = grayScale;
    }

    int size = w * h;
    jintArray result = env->NewIntArray(size);
    env->SetIntArrayRegion(result, 0, size, cbuf);
    env->ReleaseIntArrayElements(buf, cbuf, 0);
    return result;
}

#ifdef __cplusplus
}
#endif

 

package com.example.opencv;

public class MainActivity extends Activity {

    private static final String TAG = "Test";

    static {
        System.loadLibrary("daemon");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate");
        test();

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic_test_1);
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        int[] pix = new int[w * h];
        bitmap.getPixels(pix, 0, w, 0, 0, w, h);
        int[] resultPixes = gray(pix, w, h);
        if (resultPixes == null) {
            Log.d(TAG, "resultPixes == null");
        }
        
        Bitmap result = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
        result.setPixels(resultPixes, 0, w, 0, 0, w, h);

        ImageView img = (ImageView) findViewById(R.id.img2);
        img.setImageBitmap(result);
        Log.d(TAG, "--------------END");

    }

    public native void test();

    public native int[] gray(int[] buf, int w, int h);
}

将OpenCV-android-sdk\sdk\native 拷贝到jni目录(这一步可以优化)

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="原图:" />

    <ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:src="@drawable/pic_test_1" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="处理后的图:" />

    <ImageView
        android:id="@+id/img2"
        android:layout_width="match_parent"
        android:layout_height="200dp" />

</LinearLayout>

 

最后的效果是图片处理后变灰色

 

posted @ 2018-01-10 20:30  牧 天  阅读(222)  评论(0)    收藏  举报