Android can access the OpenCV library in three ways: androidopencv.

Source: Internet
Author: User

Android can access the OpenCV library in three ways: androidopencv.
OpenCV is a cross-platform computer vision Library Based on the BSD license (Open Source). It can run on Linux, Windows, Android, and Mac OS. It is lightweight and efficient-it consists of a series of C functions and a small number of C ++ classes, and provides interfaces for Python, Ruby, MATLAB, and other languages, it implements many common algorithms for image processing and computer vision. We can use it to process images and greatly optimize memory processing. Next I will talk about the following three methods of OpenCV:1. Access the Java SDK package of OpenCV so that you can call most of the Methods of OpenCV directly in Java. The first method is applicable to children's shoes that are not familiar with Opencv c ++. You do not need to directly call the C ++ solution because the SDK has been fully encapsulated with JNI. Assume that you have installed the JDK, AndroidStudio, and NDK environments.First to the official website http://opencv.org/releases.html, download Android package, such as: opencv-3.2.0-android-sdk.zip. The sdk Directory provides the Android API and the Java library sdk/java directory contains an Eclipse project, which provides the Java API of OpenCV and can be imported into the development environment. The sdk/native directory contains the OpenCV C ++ header file (for JNI), and the Android. so dynamic library. a static library. The sdk/etc directory contains the cascading between Haar and the HSV cascades. The apk directory contains the installation File that the user installs on the specified Android device. This File enables the opencv library to manage the opencv API first: File> New Module and then: select Import Eclipse ADT Project to Import the Project under sdk/java to the Project, and then add this modules to the app modules, which is directly built under the app directory. add: compile project (': openCVLibrary') under the dependencies braces in the gradle file and create a jniLibs directory under the app/src/main directory, copy all the files under sdk/native/libs to jniLibs, compile and run the sdk. If some android built-in classes cannot be recognized after the import, it is because the compiled SDK version has an error. Open the build. gradle file in the newly imported module and change compileSdkVersion and targetSdkVersion to your latest SDK version, for example:

apply plugin: 'com.android.library'android {    compileSdkVersion 25    buildToolsVersion "25.0.0"    defaultConfig {        minSdkVersion 15        targetSdkVersion 25    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'        }    }}
Run, and it is successful. I found that no additional opencv apk installation is required. The following link is an example of my successful addition: https://github.com/xiaoxiaoqingyi/NDKDemos OpenCV_java Project Ii. Use the C ++ header file provided by the opencv sdk and. so dynamic library and. a static library encapsulates jni by itself, which is more efficient than the first method and can use the opencv interface 100%. The installation method is as follows:Use AndroidStudio to create a project, create a native method in the Java class, create jni under app/src/main, and create the cpp file for the native method. The above is a simple jni method. You can refer to my previous article: http://www.cnblogs.com/xiaoxiaoqingyi/p/6524165.html. in the basic jni, we add the opencv Library to the project. First: 1. Copy the sdk/native directory to the jni directory of the project, and configure Android. mk file:
LOCAL_PATH:=$(call my-dir)include $(CLEAR_VARS)OpenCV_INSTALL_MODULES := onOpenCV_CAMERA_MODULES := offOPENCV_LIB_TYPE :=STATICifeq ("$(wildcard $(OPENCV_MK_PATH))","")include $(LOCAL_PATH)/native/jni/OpenCV.mkelseinclude $(OPENCV_MK_PATH)endifLOCAL_MODULE := OpenCVLOCAL_SRC_FILES := com_magicing_eigenndk_NDKUtils.cppLOCAL_LDLIBS +=  -lm -lloginclude $(BUILD_SHARED_LIBRARY)

 

Configure the Application. mk file:
APP_STL := gnustl_staticAPP_CPPFLAGS := -frtti -fexceptionsAPP_PLATFORM := android-9
Finally, call the opencv method in the cpp file. First import # include <opencv2/opencv. hpp>, for example:
# Include <jni. h> # include <string> # include <iostream> # include <stdio. h> # include <stdlib. h> # include <opencv2/opencv. hpp> using namespace cv; extern "C" JNIEXPORT jintArray JNICALL values (JNIEnv * env, jclass obj, jintArray buf, int w, int h) {jint * cbuf; cbuf = env-> GetIntArrayElements (buf, JNI_FALSE); if (cbuf = NULL) {return 0;} Mat imgData (h, w, CV_8UC4, (unsigned char *) cbuf); uchar * ptr = imgData. ptr (0); for (int I = 0; I <w * h; I ++) {// formula: Y (brightness) = 0.299 * R + 0.587 * G + 0.114 * B // For an int, the color value is stored in BGRA int grayScale = (int) (ptr [4 * I + 2] * 0.299 + ptr [4 * I + 1] * 0.587 + ptr [4 * I + 0] * 0.114 ); ptr [4 * I + 1] = grayScale; ptr [4 * I + 2] = grayScale; ptr [4 * I + 0] = grayScale ;} int size = w * h; jintArray result = env-> NewIntArray (size); env-> SetIntArrayRegion (result, 0, size, cbuf); env-> ReleaseIntArrayElements (buf, cbuf, 0); return result ;}
The processed image is displayed on the activity page as follows:
public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        NDKUtils ndk = new NDKUtils();        Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(                R.mipmap.pic_test)).getBitmap();        int w = bitmap.getWidth(), h = bitmap.getHeight();        int[] pix = new int[w * h];        bitmap.getPixels(pix, 0, w, 0, 0, w, h);        int [] resultPixes=ndk.gray(pix,w,h);        Bitmap result = Bitmap.createBitmap(w,h, Bitmap.Config.RGB_565);        result.setPixels(resultPixes, 0, w, 0, 0,w, h);        ImageView img = (ImageView)findViewById(R.id.img2);        img.setImageBitmap(result);    }}

After running successfully:

 

My compiled Project Link: https://github.com/xiaoxiaoqingyi/NDKDemos (OpenCV_native project) official site references: http://docs.opencv.org/2.4/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html#native-c 3. recompile the source code of opencv into the Android sdk library. The advantage is that the latest functions can be obtained, the disadvantage is that compilation is a little difficult (for children who do not understand C ++/C), and new code may be incompatible with errors.The link above http://code.opencv.org/projects/opencv/wiki/Building_OpenCV4Android_from_trunk is the compilation process recommended by the official website, I also tried to compile successfully, if Cygwin is installed in windows, then the compilation is not passed, cmake, shell, and other software need to be re-installed. The compilation process is indeed complicated. I compiled it on MAC. I only need to install the cmake software, but also the NDK environment. Probably steps: https://github.com/opencv/opencv download opencv source Package 1, install cmake software under mac, 2, configure NDK environment variables, refer to Baidu: idea directory to get compiled jni directory. If you want to compile opencv_contrib, which is the opencv extra library, you need to download the additional library package for the https://github.com/Itseez/opencv_contrib. I once added the tracking module of opencv_contrib package to the opencv core library and compiled it into the PC CodeBlocks development environment. However, an error occurred while compiling it into the Android environment, this problem I have not solved, and finally I used other methods, I will give some of the relevant information to everyone: https://github.com/alexkarargyris/Caffe_OpenCV_Android_Apphttps://zami0xzami.wordpress.com/2016/03/17/building-opencv-for-android-from-source/ I refer to the above information, when loading the extra library of opencv, the compilation fails several times, and a cmake compilation error is reported. I also asked my C ++ colleagues to try to use cmake to compile for me. Maybe we don't know much about cmake. It is said that the opencv extra library is charged, therefore, Android cannot be compiled directly, but PC is compiled successfully. If the compilation is successful, please kindly advise!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.