Android Studio OpenCV環境的搭建

下載opencv庫

OpenCV 下載驛站(百度雲盤下載,同步更新)
https://blog.csdn.net/yanzi1225627/article/details/47668021
下載了版本opencv310版本。

導入源碼工程

新建一個初始工程後,eclipse上的導入project,並引用爲庫,在Studio就是導入Module而已。

點擊File–>New–>Import Module,導入從OpenCV官網下載的SDK for Android中的java工程,/sdk/java路徑下有一個openCVLibraray310的工程,然後next–>finish。
在這裏插入圖片描述
在這裏插入圖片描述

遇到錯誤,是因爲建立的openCVLibrary310使用版本不對,所以做下面修改。
ERROR: Module ‘openCVLibrary310’: platform ‘android-14’ not found.
在這裏插入圖片描述

在這裏插入圖片描述

刪除下面行
#
同步成功
在這裏插入圖片描述

導入的module

還要在app工程裏引用導入的module,如圖。這一步可能無效(反正我這裏無效,可能是studio的bug),需要手動在app的gradle.build裏的dependencie節點添加該module的引用。

在這裏插入圖片描述

在這裏插入圖片描述

在這裏插入圖片描述

按照上圖步驟第5步後,在彈窗中選擇OpenCVLibrary310,點擊OK即可。

在這裏插入圖片描述
在這裏插入圖片描述

測試

使用下面代碼,如果報錯使用Alf +enter導入包

import android.util.Log;
import org.opencv.android.OpenCVLoader;


public class MainActivity extends AppCompatActivity {
    static {
        if(!OpenCVLoader.initDebug())
        {
            Log.d("opencv","初始化失敗");
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

圖像處理參考代碼

在sdk–》samples下面得到源碼都可以拿來測試的,能成功運行即說明環境配置完成。

MainActivity.java文件

package com.example.test;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;

public class MainActivity extends AppCompatActivity {
    static {
        if(!OpenCVLoader.initDebug())
        {
            Log.d("opencv","初始化失敗");
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView imgHuaishi = (ImageView)findViewById(R.id.img_huaishi);

        Mat rgbMat = new Mat();
        Mat grayMat = new Mat();
        Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.face);
        Bitmap grayBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), Bitmap.Config.RGB_565);
        Utils.bitmapToMat(srcBitmap, rgbMat);//convert original bitmap to Mat, R G B.
        Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY);//rgbMat to gray grayMat
        Utils.matToBitmap(grayMat, grayBitmap); //convert mat to bitmap
        imgHuaishi.setImageBitmap(grayBitmap);
        Log.i("main", "procSrc2Gray sucess...");

    }
}

佈局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="dddddd" />
    <ImageView
        android:id="@+id/img_huaishi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/face"
        android:layout_centerInParent="true"/>
    <Button
        android:id="@+id/btn_gray_process"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/img_huaishi"
        android:layout_centerHorizontal="true"
        android:text="灰度化"/>"

</RelativeLayout>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章