1.下载OpenCV4Android安装包,本实例中使用OpenCV3.1。解压后问价目录如下:
2.新建project,点击file->new modular,选择OpenCV-android-sdk\sdk\java目录,确认。
3.点击file->Project Structure,选择app,添加dependencies->modular dependency,选择OpenCV3.1,确认
4.最后修改openCVLibrary310下build.gradle中配置,使其与app目录下build.gradle相同。
//openCVLibrary310下build.gradle
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
}
至此AS下OpenCV配置已经结束,使用样例中的代码进行测试,此不赘述。
1.本示例中对图像进行轮廓提取,程序主体使用c++实现。
在MainActivity中添加如下函数public static native int[] ImgFun(int []buf,int w,int h);
3.在main目录下新建jni文件夹,将生成的头文件放入该文件夹下,向其中添加ImgFun.cpp Android.mk Application.mk文件。分别写入如下代码:
#Android.mk中添加代码:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_LIB_TYPE:=STATIC
ifeq ("$(wildcard $(OPENCV_MK_PATH))","")
# include指向自己OpenCV-android-sdk\sdk\native\jni\OpenCV.mk对应位置
include D:\download\OpenCV-3.1.0-android-sdk\OpenCV-android-sdk\sdk\native\jni\OpenCV.mk
else
include $(OPENCV_MK_PATH)
endif
LOCAL_MODULE := ImgFun
LOCAL_SRC_FILES := ImgFun.cpp
LOCAL_LDLIBS += -lm -llog
include $(BUILD_SHARED_LIBRARY)
# Application.mk写入代码如下:
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
4.向app目录下build.gradle android{}中加入如下代码:
sourceSets.main{
jniLibs.srcDir 'src/main/libs' //set .so files directory to libs
jni.srcDirs = [] //disable automatic ndk-build call
}
// call regular ndk-build(.cmd) script from app directory
task ndkBuild(type: Exec) {
commandLine 'D:\\Users\\XXX\\AppData\\Local\\Android\\sdk\\ndk-bundle\\ndk-build.cmd', '-C', file('src/main').absolutePath//windows系统下的操作;将路径替换为自己NDK所在位置;
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
记得在gradle.property中加上:android.useDeprecatedNdk=true
;
并且将NDK目录加入到local.properties中:
ndk.dir=D\:\\Users\\kangdekai\\AppData\\Local\\Android\\sdk\\ndk-bundle
sdk.dir=D\:\\Users\\kangdekai\\AppData\\Local\\Android\\sdk
此时build project便可以看到生成的ImgFun.so文件(第一次竟然因为项目名称有特殊符号没有成功)
5.定义ImgFun.cpp如下:
#include "com_example_jniopencv_MainActivity.h"
#include
#include
#include
using namespace cv;
IplImage * change4channelTo3InIplImage(IplImage * src);
extern "C" {
JNIEXPORT jintArray JNICALL Java_com_example_jniopencv_MainActivity_ImgFun
(JNIEnv *, jclass, jintArray, jint, jint);
JNIEXPORT jintArray JNICALL Java_com_example_jniopencv_MainActivity_ImgFun
(JNIEnv *env, jclass obj, jintArray buf, jint w, jint h) {
jint *cbuf;
cbuf = env->GetIntArrayElements(buf, false);
if (cbuf == NULL) {
return 0;
}
Mat myimg(h, w, CV_8UC4, (unsigned char*) cbuf);
IplImage image=IplImage(myimg);
IplImage* image3channel = change4channelTo3InIplImage(&image);
IplImage* pCannyImage=cvCreateImage(cvGetSize(image3channel),IPL_DEPTH_8U,1);
cvCanny(image3channel,pCannyImage,50,150,3);
int* outImage=new int[w*h];
for(int i=0;iint)pCannyImage->imageData[i];
}
int size = w * h;
jintArray result = env->NewIntArray(size);
env->SetIntArrayRegion(result, 0, size, outImage);
env->ReleaseIntArrayElements(buf, cbuf, 0);
return result;
}
}
IplImage * change4channelTo3InIplImage(IplImage * src) {
if (src->nChannels != 4) {
return NULL;
}
IplImage * destImg = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 3);
for (int row = 0; row < src->height; row++) {
for (int col = 0; col < src->width; col++) {
CvScalar s = cvGet2D(src, row, col);
cvSet2D(destImg, row, col, s);
}
}
return destImg;
}
定义MainActivity如下:
static {
System.loadLibrary("ImgFun");
}
public static native int[] ImgFun(int []buf,int w,int h);
ImageView mIv;
Button btnNdk,btnRtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView mtv=(TextView)findViewById(R.id.tv1);
// Test mytest=new Test();
// mtv.setText(mytest.myFun());
mtv.setText("你好!");
btnNdk=(Button)findViewById(R.id.button1);
btnRtn=(Button)findViewById(R.id.button2);
btnRtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mIv=(ImageView)findViewById(R.id.iV);
Bitmap img = ((BitmapDrawable) getResources().getDrawable(R.drawable.image)).getBitmap();
mIv.setImageBitmap(img);
}
});
btnNdk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
long current = System.currentTimeMillis();
Bitmap img1 = ((BitmapDrawable) getResources().getDrawable(
R.drawable.image)).getBitmap();
int w = img1.getWidth(), h = img1.getHeight();
int[] pix = new int[w * h];
img1.getPixels(pix, 0, w, 0, 0, w, h);
int[] resultInt = ImgFun(pix, w, h);
Bitmap resultImg = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
resultImg.setPixels(resultInt, 0, w, 0, 0, w, h);
long performance = System.currentTimeMillis() - current;
mIv.setImageBitmap(resultImg);
}
});
}
参考博文:
1.android studio 使用 jni 编译 opencv 完整实例 之 图像边缘检测!从此在andrid中自由使用 图像匹配、识别、检测
2.Android Studio 配置OpenCV、NDK,并用Jni调用OpenCV
3.在Android中使用JNI调用Opencv本地代码 配置方式 边缘检测 范例代码
4.OpenCV在Android环境下的使用方法
5.OpenCV4Android Samples