NDK+OPENCV installation + various error analysis (new installation, compilation does not require Cygwin and Sequoyah)

Source: Internet
Author: User

After two or three days of tinkering, I finally managed to run through a simple program. Here is the specific installation:

Because eclipse from my classmates has a plugin for ADT CDT. So the two don't have to be installed. (Install yourself if needed)

Specifically, the installation process: The download from the official website "Android-ndk-r10c-windows-x86_64.exe" in the "D:\" directory, and then double-click on the installation, will be automatically extracted to "D:\". It doesn't matter where you unzip it (after that), unzip it as a folder, and then put the folder in the same file directory as the Android SDK.

Then open eclipse, select [Window->perferences->android->ndk] on the menu bar to set the NDK loaction to

D:\androidEclipse\Android\android-ndk-r10e

When the NDK was installed,

Next is the installation of the OPENCV. Download OpenCV down from the official website. After decompression is a package:

The SDK here is a project on eclipse (in fact, there are some projects in sanmples, but not here), this project is actually a OPENCV library project. Then we need to import this library project into eclipse:

Copy the SDK directory from OPENCV-2.4.3.2-ANDROID-SDK to your workspace and rename it to OPENCV-SDK (whether to change the name doesn't matter, it's my personal habit)

The Eclipse display is:

Then the next use C++api to develop Android (source code is a reference to the http://blog.csdn.net/pwh0996/article/details/8957764):

Create a new project GRAYPROCESS2 (in order to not appear appcompat_v7_, I changed the version of Android created to 4.0, actually more than 4.0 can), add rr1.jpg to the resource file, and add the OpenCV class library to the project as shown above.

Note the new GrayProcess2 Android Project's package is called Com.iron.grayprocess2. The project name is GrayProcess2. A new activity called mainactivity. This activity's layout file is called Main.xml.

(1) Stings.xml

[HTML] view Plaincopy

    1. <resources>
    2. <string name= "App_name" >GrayProcess2</string>
    3. <string name= "Hello_world" >hello world!</string>
    4. <string name= "Menu_settings" >Settings</string>
    5. <string name= "Title_activity_main" >GrayProcess2</string>
    6. <string name= "Str_proc" >gray process</string>
    7. <string name= "Str_desc" >image description</string>
    8. </resources>

Because here <string name= "Menu_settings" >Settings</string> so this need to be in the construction of the Res/menu/main.xml in the project to change to android:title= "@string/menu_settings. Otherwise it does not correspond.

(2) Main.xml

[HTML] view Plaincopy

    1. <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
    2. Xmlns:tools= "Http://schemas.android.com/tools"
    3. android:orientation= "Vertical"
    4. Android:layout_width= "Match_parent"
    5. android:layout_height= "Match_parent" >
    6. <button
    7. Android:id= "@+id/btn_gray_process"
    8. Android:layout_width= "Fill_parent"
    9. android:layout_height= "Wrap_content"
    10. android:text= "@string/str_proc"/>
    11. <imageview
    12. Android:id= "@+id/image_view"
    13. Android:layout_width= "Wrap_content"
    14. android:layout_height= "Wrap_content"
    15. android:contentdescription= "@string/str_proc"/>
    16. </LinearLayout>

(3) Mainactivity.java (inside the Com.iron.grayprocess2 bag)

[Java] view plaincopy

  1. Package com.iron.grayprocess2;
  2. Import Org.opencv.android.BaseLoaderCallback;
  3. Import Org.opencv.android.LoaderCallbackInterface;
  4. Import Org.opencv.android.OpenCVLoader;
  5. Import Android.os.Bundle;
  6. Import android.app.Activity;
  7. Import Android.graphics.Bitmap;
  8. Import Android.graphics.BitmapFactory;
  9. Import Android.graphics.Bitmap.Config;
  10. Import Android.view.View;
  11. Import Android.view.View.OnClickListener;
  12. Import Android.widget.Button;
  13. Import Android.widget.ImageView;
  14. public class Mainactivity extends Activity implements onclicklistener{
  15. Private Button Btnproc;
  16. Private ImageView ImageView;
  17. Private Bitmap bmp;
  18. The OpenCV class library loads and initializes the successful callback function, where we do nothing
  19. Private Baseloadercallback Mloadercallback = new Baseloadercallback (this) {
  20. @Override
  21. public void onmanagerconnected (int status) {
  22. Switch (status) {
  23. Case loadercallbackinterface.success:{
  24. System.loadlibrary ("Image_proc");
  25. } break;
  26. default:{
  27. super.onmanagerconnected (status);
  28. } break;
  29. }
  30. }
  31. };
  32. @Override
  33. public void OnCreate (Bundle savedinstancestate) {
  34. Super.oncreate (savedinstancestate);
  35. Setcontentview (R.layout.main);
  36. Btnproc = (Button) Findviewbyid (r.id.btn_gray_process);
  37. ImageView = (ImageView) Findviewbyid (R.id.image_view);
  38. To display the Lena image loader in a program
  39. BMP = Bitmapfactory.decoderesource (Getresources (), R.DRAWABLE.RR1);
  40. Imageview.setimagebitmap (BMP);
  41. Btnproc.setonclicklistener (this);
  42. }
  43. @Override
  44. public void OnClick (View v) {
  45. int w = bmp.getwidth ();
  46. int h = bmp.getheight ();
  47. int[] pixels = new INT[W*H];
  48. Bmp.getpixels (pixels, 0, W, 0, 0, W, h);
  49. int[] ResultInt = Imageproc.grayproc (pixels, W, h);
  50. Bitmap resultimg = Bitmap.createbitmap (W, H, config.argb_8888);
  51. Resultimg.setpixels (resultint, 0, W, 0, 0, W, h);
  52. Imageview.setimagebitmap (RESULTIMG);
  53. }
  54. @Override
  55. public void Onresume () {
  56. Super.onresume ();
  57. Loading and initializing the OpenCV class library through the OpenCV engine service, the so-called OPENCV engine service is
  58. OPENCV_2.4.3.2_MANAGER_2.4_*.APK package that exists in the APK directory of the OpenCV installation package
  59. Opencvloader.initasync (Opencvloader.opencv_version_3_0_0, this, mloadercallback);
  60. }
  61. }

Opencvloader.initasync (Opencvloader.opencv_version_3_0_0, this, mloadercallback); This line of code in the previous code is written 2_3_4 I did not look at the beginning of the time, so how to do it is not right, said that there must be remembered when you run your mobile phone on this Grayprocess2 project, the phone to press the previous download of the APK package apk, You have to choose an apk to try the installation to see the line is not OK, mine is the red box that can be. Before the reason is not right, because you see the download good OpenCV Manager is 3-0-0 version, I do not change the code here, or 2-3-4 version of, certainly not AH. So people's code must be carefully read, do not immediately affixed.

(4) Imageproc.java (also within the com.iron.grayprocess2 bag)

[Java] view plaincopy

    1. Package com.iron.grayprocess2;
    2. public class Imageproc {
    3. public static native int[] Grayproc (int[] pixels, int w, int h);
    4. }

Writing JNI and C-related code

Create a new directory JNI in the project, add the file Android.mk,application.mk,imageproc.h,imageproc.cpp separately in the JNI directory, this time the file must have, one of the. h files is compiled. The contents of the remaining three files are as follows.

(1) android.mk

[Plain] view plaincopy

    1. Local_path: = $ (call My-dir)
    2. Include $ (clear_vars)
    3. Include: /opencv-sdk/native/jni/opencv.mk
    4. Local_src_files: = ImageProc.cpp
    5. Local_module: = Image_proc
    6. Include $ (build_shared_library)

Note the include include here:  /opencv-sdk/native/jni/opencv.mk to change to his own address: D:\OpenCV-android-sdk\sdk\native\jni\OpenCV.mk It is important that you include the compiled files in the OpenCV class library so that you can use the OpenCV class library in our project;

I was just casually on the writing, did not change, I found a lot of errors are I did not read their own code, I posted it, in fact, a lot of people and your path Ah, version ah not the same, you have to change a bit. Many mistakes are not big mistakes, they are not too careful. Don't panic, take it slow. sure you can.

Code Description:

First line: Indicates the current compilation path;

Second line: Empty the variable;

The third line: the compilation files in the OpenCV class library are included so that the OpenCV class library can be used in our project;

Line four: Specify the C + + source files that need to be compiled;

Row Five: Specifies the name of the class library generated by the compilation;

Line six: Call the command to compile the source file into a static library.

Note: The path specified in the third row is critical, and the path changes when the OpenCV class library and the project path-related location change.

(2) application.mk (configuration file) (this generally does not need to change)

[Plain] view plaincopy

    1. App_stl: = gnustl_static
    2. App_cppflags: =-frtti-fexceptions
    3. App_abi: = armeabi-v7a
    4. App_platform: = android-8

(3) ImageProc.cpp

[CPP] view plaincopy

  1. #include <ImageProc.h>
  2. #include <opencv2/core/core.hpp>
  3. #include <string>
  4. #include <vector>
  5. using namespace CV;
  6. using namespace Std;
  7. Jniexport jintarray jnicall Java_com_iron_grayprocess2_imageproc_grayproc (jnienv* env, Jclass obj, JintArray buf, Jint W , Jint h) {
  8. Jint *cbuf;
  9. Cbuf = Env->getintarrayelements (buf, false);
  10. if (cbuf = = NULL) {
  11. return 0;
  12. }
  13. Mat imgdata (H, W, CV_8UC4, (unsigned char*) cbuf);
  14. uchar* ptr = imgdata.ptr (0);
  15. for (int i = 0; i < w*h; i + +) {
  16. Calculation formula: Y (brightness) = 0.299*r + 0.587*g + 0.114*b
  17. For an int four bytes, its color value is stored as: BGRA
  18. int grayscale = (int) (ptr[4*i+2]*0.299 + ptr[4*i+1]*0.587 + ptr[4*i+0]*0.114);
  19. PTR[4*I+1] = grayscale;
  20. PTR[4*I+2] = grayscale;
  21. PTR[4*I+0] = grayscale;
  22. }
  23. int size=w * H;
  24. Jintarray result = Env->newintarray (size);
  25. Env->setintarrayregion (result, 0, size, cbuf);
  26. Env->releaseintarrayelements (buf, cbuf, 0);
  27. return result;
  28. }

There is a #include in the code <ImageProc.h> so we have to note that the. h file we compiled later needs to be the same as the <ImageProc.h> name in this. Only the two are the same as each other, the specific name is what, you can decide, and. cpp file name does not require the same. The name of the. cpp file is the same as the Local_src_files value in Android.mk.

And we know that our method in a Java class is public class Imageproc {

Public Static native int [] Grayproc ( int[] pixels, int w, int h);}

In this case, the method name of this class is Grayproc, and our method implementation is written in C + +. We will compile this Java class. class file into an. h file (specifically how to do the following), which is automatically compiled. After compiling we found that the. h file name is Java_com_iron_grayprocess2_imageproc_grayproc. This is a format. We can't change. And we know that we are in the CPP file implemented in the header file method, then we should note that the method we implement in the CPP name should be the same name as defined in the H file should be the same as here, is java_com_iron_grayprocess2_ Imageproc_grayproc.

After you have written all the above, it must be wrong. We'll have to change the mistake at this time.

We should first look at the import of the OpenCV Library project is shown on Eclipse is not the library (because to give the GRAYPROCESS2 project reference, so this OPENCV library project is library), Specific: Right-click OpenCV Library Project, and then the properties "Android will appear below this screen, to see if it is the library, if not, you need to tick."

And then we need to introduce this OPENCV library project into the GRAYPROCESS2 project just now. This step is prone to error. I can only say the problems I have encountered.

First, according to the Internet.

Select the project grayprocess in the Package Explorer, right-click the properties in the pop-up menu, then select Android on the left side of the pop-up Properties window, then click the Add button on the bottom right to select OpenCV Library 2.4.3 and click OK, after the operation is completed, the OpenCV class library will be added to Grayprocess's Android dependencies,

                                                                                                                                                                                                                  "

But at this point, as I said, I should have seen the project show the Android Dependencies that added the OpenCV class library to grayprocess. But at first I didn't know why it didn't show. Several times did not show (including restart), ask others, people say that the OPENCV package into a jar and then add to the Grayprocess Project Libs folder, or not (this may be because the OpenCV of the library and C + + code, cannot be packaged directly into a jar). So I went on to figure out how to get grayprocess project to cite this OPENCV library project. And then looked for it on the Internet.

There are generally two approaches on the Internet (plus the one above is three kinds):

1. (the internet says the most)
Right-grayprocess Project->build path->configure build Path->project tab-> Click Add to add OpenCV Library project.

2.

or right-grayprocess the project--"properties-," project reference--> a reference to the project to be added.

When I'm done, I'll use it.

Select the project grayprocess in the Package Explorer, right-click the properties in the pop-up menu, then select Android on the left side of the pop-up Properties window, then click the Add button on the bottom right to select OpenCV Library 2.4.3 and click OK, after the operation is completed, the OpenCV class library will be added to Grayprocess's Android dependencies,

                                                                                                                                                                                                                  "

This method, and then I found that the OpenCV class library was added to Grayprocess's Android dependencies (the Android dependencies in the Grayprocess project list is displayed, although I don't know why it's shown again).

At this point, if I add the above two methods to remove the hook or something, it seems to be possible. (In the Grayprocess project list or Android dependencies display, do not know why)

There is a drop-down menu with the Project option in the navigation bar. Build automatically this option to tick, do not tick as if there are some errors. I don't know why.

Then I have no problem, do not know if you have any problems (slowly find, do not hurry, I think most of the project right-click on the properties interface, some things are not configured well)

Then my Grayprocess2 can refer to the OPENCV Library project.

Then there is a problem, this problem is my compilation after the problem, but this first said before (or will be in ImageProc.cpp prompt Android unresolved inclusion: <OPENCV2/CORE/CORE.HPP >). The solution is to add something in the Include Directories red box (of course include directories, except for everything else)

The above also said to change:

One of the

    • ${ndkroot}/ndk-build (in Windows it ' s ndk-build.cmd I think), build command, C + + Build
    • Add Ndkroot, Environment, C + +

But I seem to be using the default build command directly.

The next step is to compile:

Before compiling, note that the new directory JNI in the previous project, add the file Android.mk,application.mk,imageproc.cpp separately in the JNI directory, and a ImageProc.h file. The specific build step (excerpt of the http://blog.csdn.net/pwh0996/article/details/8957764 adds a hint):

Generate the. h file

1. First, when you finish Imageproc.java this save eclipse will automatically help you generate Java class file

You can be in the engineering catalogue-"Bin-" classes-"com-------。。。。。 Found in the file.

Like mine, under the E:\work\op\OpenCV-2.4.5-android-sdk\samples\Test2\bin\classes\com\test2\test2 path.

2. Copy the classes file (note that the Classses folder above it, not just the Imageproc.class file) (copy, not cut) to the e-packing directory

Then open cmd

E:

CD classes

Javah Com.test2.test2.ImageProc (If you set the environment variable for Java, you want to set it.) Remember to restart CMD, or it won't work)

Then you will find a XXX. header file in the classes file

You can change this header file to any name, like the ImageProc.h on top of us and put it in the JNI of the engineering catalog.

And then it's compiled:

Because it's a new version, we don't need any Cygwin or Sequoyah anymore. The approach is simple:

Right-click Project Android Tools under Add Native support, I can't (like import project, you import a project, you can no longer import, here you add Native support, you can no longer add , without that option), from the Internet to extract others,

Then you 1, "Ctrl+b" Build project, 2, right->run as. ->android application ... It's going to be all.

Isn't this compilation very simple AH. If want to know specifically, this post is also good, recommend to everybody: http://blog.csdn.net/houshunwei/article/details/17217695

Completed.

Main reference: http://blog.csdn.net/pwh0996/article/details/8957764

Http://jingyan.baidu.com/article/3ea51489e7a9bd52e61bbac7.html

http://blog.csdn.net/houshunwei/article/details/17217695

NDK+OPENCV installation + various error analysis (new installation, compilation does not require Cygwin and Sequoyah)

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.