Hand-in-hand teaching OpenCV-android-sdk configuration and use, learning will not count as I lose

OpenCV is the abbreviation of Open Source Computer Vision Library, mainly written by some big brothers of Intel Corporation, the function is to provide a large number of graphics and graphics processing (computer vision) libraries, and provide iOS and Android SDK, because my main work is with Camera It is related to algorithm integration, so this tool is often used, and the use and configuration of the Android SDK are recorded here

OpenCV-android-sdk download

Download address (official website): https://opencv.org/releases.html The
latest version is 4.0.0 beta, we download the stable version 3.4.3, directly click on the Android pack to download, and unzip after downloading.

Import Java SDK to Android Studio

  1. After opening Android Studio, create a new project OpencvDemo. After it is built, click
    File-> New- > Import Module in turn, as shown in the figure:

  1. In the window that opens, select the sdk directory under the Opencv-android-sdk folder, as shown in the figure:

image

Name the Module name opencvSdk, and click Finish.

  1. During the import process, Android Studio will prompt an error in the sdkVersion setting in the Manifest . We need to directly delete the minSdkVersion and targetSdkVersion of AndroidManifest.xml in opencvSdk. The reason is that the sdkVersion setting is now placed in the gradle configuration file, delete the uses-sdk, as shown in the figure Show:

  1. Change the relevant settings of build.gradle about android sdk in opencvSdk to be the same as the current project, just modify compileSdkVersion minSdkVersion and targeSdkVersion, if shown:

  1. Recompile after making changes, the OpenCV import is complete, and the next step is to use it in our own project.

Use Gaussian Blur in Opencv

Next we write a Demo OpenCV to use the Gaussian blur, first add in our project opencvSdk as we import dependence, adding build.gradle in implementation project(':opencvSdk')as shown:

Write Demo, the method is an ImageView to display a clear picture, after clicking the button, the picture is Gaussian blurred, and redisplayed, the Gaussian blur code is as follows:

import org.opencv.android.Utils;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
//部分代码省略
public class MainActivity extends AppCompatActivity {

    private ImageView mImageView;
    // opencv相关功能实现所需要的so库
    static {
        System.loadLibrary("opencv_java3");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mImageView = findViewById(R.id.iv_image);
        final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_bg);
        mImageView.setImageBitmap(bitmap);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                blurImage(bitmap);
                mImageView.setImageBitmap(bitmap);
            }
        });
    }
    //传入任意bitmap, 返回模糊过后的bitmap
    private Bitmap blurImage(Bitmap origin) {
        Mat mat = new Mat();
        Utils.bitmapToMat(origin, mat);
        Imgproc.GaussianBlur(mat, mat, new Size(15, 15), 0);
        Utils.matToBitmap(mat, origin);
        return origin;
    }
}

Click the button repeatedly to make the picture more and more blurred, the effect picture:

Explanation

  • As you can see from the demo above, it is called System.loadLibrary("opencv_java3");. In fact, the core functions of OpenCV are implemented through C ++. Android SDK basically encapsulates the call of JNI, so the core library must be loaded at runtime libopencv_java3.so. The location of the so library is OpenCV-android-sdk\sdk\native\libsin this directory. there are multiple CPU architecture library of algorithms, all of the above Demo default libraries are packaged in the apk, apk after compilation volume will be about 80M, but normally we just need armeabi-v7a 和 arm64-v8alibraries of these two directories, because now the phone side Almost all CPUs are of ARM architecture.

  • About OpencvManager, OpencvManager an official launch of the App, OpencvManager is a separate app, after installation, we do not need to pack our libopencv_java3.so in the apk simply by OpenCVLoader.initAsync()initialize method can be equivalent to the relevant The algorithm library is packaged separately into another App, which can reduce the apk size, but this method is actually very impractical, so few people use it.

As can be seen from the SDK using OpenCV Java above, it is very simple to use, but the biggest problem is that it will increase the apk size.Since we only use some of these functions, we have to package the entire algorithm library into the apk. Is there any other Can the volume of the apk be reduced? The
answer is yes. In the next article, I will explain how to call the OpenCV SDK through JNI without using the Java SDK, and minimize the volume occupied by the apk.

Remarks: Test environment Android Studio 3.2.1 compileSdkVersion 28 Android 8.1

The article is not easy, if you like this article, or it is helpful to you, I hope everyone will like it and forward it. The article will be updated continuously. Absolutely dry goods! ! !

Published 34 original articles · Like1 · Visits 756

Guess you like

Origin blog.csdn.net/Android725/article/details/105301542