Android OpenCV测试及爬坑

一、准备资源

官网下载OpenCV Android SDK资源:openCV Android SDK

最新版本如下图:

尝试了4.2.0,在手机端貌似不支持,集成后一直提示缺少libc++.so,  选择3.4.9集成测试成功

二、新建AndroidStudio测试项目

打开Android Studio新建Android工程,如:OpenCV

1、xml布局文件如下:

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

    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"

    tools:context=".MainActivity">

        android:id="@+id/image_1"

        android:layout_width="wrap_content"

        android:layout_height="300dp"

        app:layout_constraintTop_toTopOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintEnd_toEndOf="parent"/>

        android:id="@+id/image_2"

        android:layout_width="wrap_content"

        android:layout_height="300dp"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

2、新建module导入opencv sdk

将sdk中的java包中的文件复制到新建module中(不要重新更改包名,避免编译出错得手动更改每个文件的包名)

注意,engine包在aidl文件当中(不在编译出错提示)

将sdk中的lib文件下的arm类三个文件夹复制到module中的jniLibs下

3、MainActivity测试

public class MainActivityextends AppCompatActivity {

ImageViewimageView1;

    ImageViewimageView2;

    Bitmapbitmap1;

    Bitmapbitmap2;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        imageView1 = findViewById(R.id.image_1);

        imageView2 = findViewById(R.id.image_2);

        try {

bitmap1 = BitmapFactory.decodeStream(getAssets().open("test.jpg"));

            bitmap2 = BitmapFactory.decodeStream(getAssets().open("test.jpg"));

            imageView1.setImageBitmap(bitmap1);

        }catch (IOException e) {

e.printStackTrace();

        }

}

@Override

    protected void onResume() {

super.onResume();

        OpenCVLoader.initDebug();

        Mat src =new Mat();

        Mat dst =new Mat();

        Utils.bitmapToMat(bitmap1, src);

        Imgproc.cvtColor(src, dst, Imgproc.COLOR_RGBA2GRAY);

        Utils.matToBitmap(dst, bitmap2);

        imageView2.setImageBitmap(bitmap2);

    }

}

三、编译运行结果如下:(此用例为灰度图像处理demo)

上部原图,底部为灰度处理图

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容