NDK compiles OPENCV for Android and refers to the general steps in Unity3d games

Source: Internet
Author: User

This article uses: Unity3d + OpenCV + Androidunity3d to invoke the OpenCV compiled. So dynamic library that works correctly when you build an Android apk. Because the Android system is Linux-based, you cannot use Windows to compile connected DLL dynamic libraries, you must use the. So dynamic library, and the CPU architecture is ARMV7. A. Package your own Test.cpp source code 1) First download with your own OPENCV version of the same opencv for Android copy sdk/native directory to your own working directory. Like E:/arso. In Sdk/native/libs, there are already compiled. A files, all CPU architectures have opencv.mk in Sdk/native/jni, with the. A static library packaged as a. So dynamic library configuration file 2) Create a new directory in the working directory E:/arso Jni

3) Create a new two configuration file in Jni, and copy the source files that you need to compile into. So  4) android.mk as follows: local_path:= $ (call My-dir)  include $ (clear_vars)  opencv_lib_type=static  include e:/work/arso/sdk/native/jni/opencv.mk local_module   : = aropencvlocal_cflags   : =-werror local_src_files: = ar.cpp  #LOCAL_LDLIBS     + =-llog-landroid-legl-lglesv1_cm local_ldlibs    + =-llog    #build dynamic library  Include $ (build_shared_library)  5) application.mk as follows: App_stl: = gnustl_static#pp_cppflags: =-frtti- Fexceptionsapp_abi:=  armeabi Armeabi-v7a#app_platform: = android-8 6) The Ar.cpp code is as follows://Ar.cpp: Defines the exported function of the DLL application.   //#include <iostream>  #include <opencv2\opencv.hpp> using namespace cv;    extern "C" { //int addf (int x, int y)//{//    return x + Y;//} int show () {    Ip limage* img1 = Cvcreateimage (Cvsize (), ipl_depth_8u, 3);    UCHAR R1, G1, b1;     for (int i = 0; i < img1->height; i++)     {        UCHAR *ptrimage = (uchar*) (Img1->imagedata + i * img1->widthstep);       //uchar *PTRDST = (uchar*) (i Mg->imagedata + i * img->widthstep);          for (int j = 0; J < img1->width; J + +)         {            PTRIMAGE[3 * j + 0] = 255;      & nbsp     PTRIMAGE[3 * j + 1] = 0;            PTRIMAGE[3 * j + 2] = 0;    &nbs P  }   }     Mat img2 = Cvarrtomat (IMG1);    //Display the game's original picture in the window    &nbsp ; Imshow ("Img2", Img2);   //wait for 6000 ms after window auto close      return img2.rows;    }  }&NBSP;5) Use the Ndk-build command to compile the ar.cpp to. so files. You need to configure environment variables before using the NDK. Run Ndk-build in the E:/arso/jni directory

Describes the successful compilation, which generates two CPU architectures in the Arso/libs directory. So dynamic library libopencv.so 6) is used in Unity3d. Copy the libopencv.so to the Unity3d: Create a new directory plugins/android in the assets directory, and copy to add it to the code:  [DllImport( "Aropencv")]     private static extern int Show (); Call Show () directly on call to 7) Unity Build for Android package. Use ADB logcat-s Unity to see if there are error log print errors and Debug.Log information.

NDK compiles OPENCV for Android and refers to the general steps in Unity3d games

Related Article

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.