Android OpenCV tutorial one environment configuration

Keywords: Android OpenCV Gradle SDK

At the beginning of the course, the environment is arranged to run the program

1. Download resources
https://github.com/opencv/opencv/releases/tag/3.2.0
Download opencv-3.2.0-android-sdk.zip and unzip


2. Build a new project, and then lead the package
(1) First, create a new Project, and then import the extracted folder sdk/java as a module.
(2) Modify the build.gradle of the module to be consistent with the compileSdkVersion, minSdkVersion and targetSdkVersion of the build.gradle of the app
(3) Add the following code to build.gradle of app to import module into app
    implementation project(':openCVLibrary320')
(4) Create a new jniLibs folder under src/main of app, then copy all files under sdk/native/libs to jniLibs folder, add the following code to build.gradle of app, and import so file
sourceSets { main { jni.srcDirs = ['src/main/jni', 'src/main/jni/'] } }
(5) Finally, add the following code to gradle.properties of the project
android.useDeprecatedNdk=true


3. Write code

public class MainActivity extends AppCompatActivity {
    private Button btn;
    private ImageView img;

    private Bitmap srcBitmap;
    private Bitmap grayBitmap;
    private static boolean flag = true;
    private static boolean isFirst = true;
    private static final String TAG = "gao_chun";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        img = (ImageView)findViewById(R.id.img);
        btn = (Button)findViewById(R.id.btn);
        btn.setOnClickListener(new ProcessClickListener());
    }


    @Override
    protected void onResume() {
        super.onResume();
        //load OpenCV engine and init OpenCV library
        if (!OpenCVLoader.initDebug()) {
            Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization");
            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_2_0, this, mLoaderCallback);
        } else {
            Log.d(TAG, "OpenCV library found inside package. Using it!");
            mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
        }
    }


    //Callback function after OpenCV library is loaded and initialized successfully
    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, "Successfully loaded");
                    break;
                default:
                    super.onManagerConnected(status);
                    Log.i(TAG, "Load failed");
                    break;
            }
        }
    };



    public void procSrc2Gray(){
        Mat rgbMat = new Mat();
        Mat grayMat = new Mat();
        srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sample);
        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...");
    }


    public class ProcessClickListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(isFirst){
                procSrc2Gray();
                isFirst = false;
            }
            if(flag){
                img.setImageBitmap(grayBitmap);
                btn.setText("View original");
                flag = false;
            }else{
                img.setImageBitmap(srcBitmap);
                btn.setText("Grayscale");
                flag = true;
            }
        }
    }

}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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="OpenCV"/>

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/sample"/>

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/img"
        android:layout_centerHorizontal="true"
        android:text="Grayscale"/>"

</RelativeLayout>

4. After installing apk

He will remind you that you don't have any package. You need to go to the app store to download and install the app. Then you need to run the app again.


Next, I'm going to explain the examples in samples to complete the course

Posted by mikesheridan5 on Sun, 03 May 2020 00:00:43 -0700