基于基于Android studio3.6的的JNI教程之教程之opencv
基本环境:
Android studio3.6
NDK:r14b(尽量使用该版本)
Opencv3.4.1 android sdk
(1)新建工程OpenCVDemo,选择,一定要选择Native c++类型,最后要选c++14支持。
(2)File->Project Structure->SDK Location,设置这3个路径,NDK选择r14b。
(3)任意找一张图片,复制到res/drawable。
(4)修改布局文件res/layout/ activity_main.xml
(5)修改java文件,app/src/main/java/ com.example.opencvdemo/ MainActivity
主要修改包括修改
继承OnClickListener类,
修改onCreate方法
增加c++的接口函数,getEdge
实现点击按钮的方法,
整体代码如下,
package com.example.opencvdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
findViewById(R.id.show).setOnClickListener(this);
findViewById(R.id.process).setOnClickListener(this);
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
//获得Canny边缘
public native void getEdge(Object bitmap);
private ImageView imageView;
@Override
public void onClick(View v) {
if (v.getId() == R.id.show) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
imageView.setImageBitmap(bitmap);
} else {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test);