首先打开project structure确认你的Gradle version和Android Plugin Version版本。你的版本只能比这个高,我的版本如下。
接下来按照一般jni开发流程走
创建native方法:
public class OpenCVNativeClass {
static {
System.loadLibrary("MyLibs");
}
public static native int Convert2Gray(long matAddrRgba,long matAddrGray);
}
生成头文件。我这里直接写了cpp文件(src\main\jni\xxx.cpp)
//
// Created by lx on 2017/3/21.
//
#include <jni.h>
#include <opencv2/opencv.hpp>
extern "C" {//我在这里被坑了,切记c++文件中在这里要写这个关键字
using namespace cv;
using namespace std;
int toGray(Mat img,Mat &gray);
JNIEXPORT jint JNICALL Java_erg_mylibrary_OpenCVNativeClass_Convert2Gray
(JNIEnv * env, jclass obj, jlong addrRgba, jlong addrGray){
Mat & mRgb = *(Mat *)addrRgba;
Mat & mGray = * (Mat *)addrGray;
int conv;
jint retVal;
conv = toGray(mRgb,mGray);
retVal = (jint)conv;
return retVal;
}
int toGray(Mat img,Mat& gray){
cvtColor(img,gray,CV_RGBA2GRAY);
if(gray.rows==img.rows && gray.cols == img.cols){
return 1;
}
return 0;
}
}
编写Android.mk文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
#opencv
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
#修改为你自己的路径
include C:\Users\lx\Desktop\OpenCV-android-sdk\sdk\native\jni\OpenCV.mk
#your code
LOCAL_SRC_FILES:=erg_mylibrary_OpenCVNativeClass.cpp
LOCAL_LDLIBS += -llog
LOCAL_MODULE := MyLibs
include $(BUILD_SHARED_LIBRARY)
Application.mk
APP_STL := gnustl_shared
APP_CPPFLAGS := -frtti -fexceptions -std=c++11
#这里编译平台选择app支持的最小sdk
APP_PLATFORM := android-15
APP_ABI := armeabi armeabi-v7a
配置gradle
externalNativeBuild {
ndkBuild {
path 'src/main/jni/Android.mk'
}
}
此时Sync Project,然后make project。Make成功后可以在build\intermediates\ndk-build\debug\下考到生成的so文件。
Ok!到这里JNI完成。可以的java中调用了。(在之前的例子上修改)
public class OpenCVNativeClass {
static {
System.loadLibrary("MyLibs");
}
public static native int Convert2Gray(long matAddrRgba,long matAddrGray);
}
package erg.mylibrary;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.SurfaceView;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.InstallCallbackInterface;
import org.opencv.android.JavaCameraView;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
/**
* Created by lx on 2017/3/21.
*/
public class MainActivity extends Activity implements CameraBridgeViewBase.CvCameraViewListener2 {
static String TAG = "MainActivity";
private JavaCameraView JavaCameraView;
//彩色 黑白Mat
private Mat mRgb,Gray;
LoaderCallbackInterface LoaderCallbackInterface = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case org.opencv.android.LoaderCallbackInterface.SUCCESS:
JavaCameraView.enableView();
break;
default: {
super.onManagerConnected(status);
}
}
}
@Override
public void onPackageInstall(int operation, InstallCallbackInterface callback) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JavaCameraView = (org.opencv.android.JavaCameraView) findViewById(R.id.CameraView);
JavaCameraView.setVisibility(SurfaceView.VISIBLE);
JavaCameraView.setCvCameraViewListener(this);
}
@Override
public void onCameraViewStarted(int width, int height) {
mRgb = new Mat(height, width, CvType.CV_8UC4);
Gray = new Mat(height, width, CvType.CV_8UC4);
}
@Override
public void onCameraViewStopped() {
mRgb.release();
Gray.release();
}
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
mRgb = inputFrame.rgba();
//JNI 图片处理,彩色图变为黑白图
OpenCVNativeClass.Convert2Gray(mRgb.getNativeObjAddr(),Gray.getNativeObjAddr());
return Gray;
}
@Override
protected void onStart() {
super.onStart();
if (OpenCVLoader.initDebug()) {
Log.d(TAG, "Opencv Success");
LoaderCallbackInterface.onManagerConnected(BaseLoaderCallback.SUCCESS);
} else {
Log.d(TAG, "Opencv fail");
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_2_0, this, LoaderCallbackInterface);
}
}
@Override
protected void onPause() {
super.onPause();
JavaCameraView.disableView();
}
@Override
protected void onResume() {
super.onResume();
JavaCameraView.enableView();
}
@Override
protected void onDestroy() {
super.onDestroy();
JavaCameraView.disableView();
}
}