Android Studio-—使用OpenCV的库灰度化图片

1.安装Android Studio(过程略) 版本为3.4.1

2.官网下载OpenCV for Android 网址:http:opencv.org/downloads.html 我下载的是下图的版本

 OpenCV的版本为3.4.1

 3.将下载好的OpenCV for Android解压到固定文件夹 

4.新建一个android项目(就新建一个就好,以后用到)

Android Studio导入OpenCV:

    1.点击File-new-import Module如图:

     Android Studio-—使用OpenCV的库灰度化图片

     2.Source directory中填写内容:找到步骤1中解压OpenCV for Android的位置OpenCV-android-sdk   —>  sdk—>  java直接拷贝,拷贝完毕后会出现Module                     name(我的因为已经导入过了所以会有感叹号正常情况下点击下一步下一步下一步就好)

 Android Studio-—使用OpenCV的库灰度化图片

3.导入成功后,在app 项目中引用  

implementation project(':openCVLibrary341')

4。

以上完成后你的app就可以使开发openCV了

android使用OpenCV将彩图转化为灰图的例子:

  MainActivity.java代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

package com.example.lenovo.myapplication;

 

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.ImageView;

import com.example.lenovo.R;

import org.opencv.android.BaseLoaderCallback;

import org.opencv.android.LoaderCallbackInterface;

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 {

    Button btnProcess;

    Bitmap srcBitmap;

    Bitmap grayBitmap;

    ImageView imgHuaishi;

    private static boolean flag = true;

    //private static boolean isFirst = true;

    private static final String TAG = "MainActivity";

 

    //OpenCV库加载并初始化成功后的回调函数

    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {

 

        @Override

        public void onManagerConnected(int status) {

            // TODO Auto-generated method stub

            switch (status){

                case BaseLoaderCallback.SUCCESS:

                    Log.i(TAG, "成功加载");

                    break;

                default:

                    super.onManagerConnected(status);

                    Log.i(TAG, "加载失败");

                    break;

            }

 

        }

    };

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        initUI();

        btnProcess.setOnClickListener(new ProcessClickListener());

    }

    public void initUI(){

        btnProcess = (Button)findViewById(R.id.btn_gray_process);

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

        Log.i(TAG, "initUI sucess...");

 

    }

 

    public void procSrc2Gray(){

        Mat rgbMat = new Mat();

        Mat grayMat = new Mat();

        srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.text_img);

        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

        Log.i(TAG, "procSrc2Gray sucess...");

    }

 

    private class ProcessClickListener implements View.OnClickListener {

 

        @Override

        public void onClick(View v) {

            // TODO Auto-generated method stub

//            if(isFirst)

//            {

                procSrc2Gray();

//                isFirst = false;

//            }

            if(flag){

                imgHuaishi.setImageBitmap(grayBitmap);

                btnProcess.setText("查看原图");

                flag = false;

            }

            else{

                imgHuaishi.setImageBitmap(srcBitmap);

                btnProcess.setText("灰度化");

                flag = true;

            }

        }

 

    }

 

    @Override

    public void onResume()

    {

        super.onResume();

        if (!OpenCVLoader.initDebug()) {

            Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization");

            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);

        } else {

            Log.d(TAG, "OpenCV library found inside package. Using it!");

            mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);

        }

    }

}

  对应的xml文件:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

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

<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"

    android:paddingLeft="16dp"

    android:paddingRight="16dp"

    android:paddingTop="16dp"

    android:paddingBottom="16dp"

    tools:context="com.example.mytest.MainActivity">

 

    <ImageView

        android:id="@+id/img_huaishi"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        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>

     这样就够了,然后点击运行。。。。。

运行失败,打印onResume方法,发现运行的是没有加载成功

@Override
protected void onResume() {
    super.onResume();
    if (!OpenCVLoader.initDebug()){
        Log.e("---------", "Internal OpenCV library not found. Using OpenCV Manager for initialization");
    }else {
        Log.e("---------", "OpenCV library found inside package. Using it!");
        mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
    }
}

方法如下:

     1.把OpenCV sdk for Android文件下F:\OpenCV-android-sdk\sdk\native下的libs文件夹拷贝到你的安卓项目下,即自己的项目\src\main下面,并且将libs改名为         jniLibs

     2.将OpenCV-android-sdk\samples\image-manipulations\res\layout下的xml文件拷贝到自己的项目\src\main\res下面

     3.将OpenCV-android-sdk\samples\image-manipulations\src\org\opencv\samples\imagemanipulations下的java文件拷到自己的项目\src\main\java\你                       MainActivity所在的包名,即和MainActivity同级目录

     4.在项目清单文件中为刚才导入的java文件进行配置,加上相应的权限,如图:

       Android Studio-—使用OpenCV的库灰度化图片

     接下来运行你的项目,就妥妥的了