Android Studio 接入OpenCV 3.2.0 并不使用OpenCV_Manager_3.2.0的APK包来进行图像处理

来源:互联网 发布:淘宝怎样隐藏订单 编辑:程序博客网 时间:2024/06/09 21:09

2016年,公司让我做个手机号码识别功能,主要是快递运单上面的机打手机号码,当时研究了好长时间的OpenCV 也没有接入成功,主要是想把手机号码那块区域提取出来直接让Google的开源Tesseract OCR库来进行识别.因为OpenCV这个玩意儿里面的代码全是C++写的,当时的进度又比较赶,我一看C++的代码看都看不懂就没仔细研究,只把拍出来的照片进行了宽高限制,所以最终对准备号码就比较麻烦了,导致被领导说了一通,显得我这一年里面没什么进步,工资也没加多少。所以心情不开心!大哭

为了能让自己的工作能得到领导的认可,在过完年后的工作里,我特意抽时间再研究了下OpenCV,终于把这个难题解决了。


上面都是我说的废话,看下面:


网上找了很多办法,大多在Activity中OnResume这个方法中写的是下面这串代码:


[java] view plain copy
  1. OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_2_0, getApplicationContext(), mLoaderCallback);  


经过我的测试,这句代码是要让你在你的手机上装一个对应你CPU处理器的Manager APK。但是在正常的工作中,你总不会让使用你APP的用户也装一个Manager apk包吧~~惊讶所以还是要使用一个编译好了的so库。

不用担心,这个SO库就在你从OpenCV官网上下载的包中:C:\Users\kb82\Desktop\open_cv\OpenCV-Android-sdk\sdk\native\libs 当然了,这个只是我存放在电脑上的目录大笑

---【图一】

--【图二】这个就是你需要的.so库了

好了,废话不多说了!下面是重点:

Step1:下载OpenCV下载地址 http://opencv.org/点击打开链接

【图三】

点击进入后等待几秒便开始下载了。下载到哪儿需要自己设置找一下,一般在系统下载目录里可以找到。

由于下载的包里存在【图二】中的.so库我们就不配置NDK了~

Step2:导入OpenCV的sdk

2-1 AS中执行-->File->New->Inport Module,找出OpenCV-android-sdk目录下sdk中的Java文件夹

导入成功以后如下图:

2-2导入libopencv_java3在项目app/src/main目录下新建文件夹jniLibs,然后将OpenCV-android-sdk目录下的

\sdk\native\libs对应的各个平台.so文件放进去。

导入成功以后如下图:

2-3到这里,我们还没有结束-需要在app-build.gradle中的android节点中加入自定义 jni的地址。(我的地址如下:)

[html] view plain copy
  1. sourceSets {  
  2.         main {  
  3.             jniLibs.srcDirs = ['src/main/jniLibs']  
  4.         }  
  5.     }  

2-4在我们的项目Module下的build.gradle中添加依赖目录,只有这样我们才能在代码中使用Opencv包中的代码

[html] view plain copy
  1. dependencies {  
  2.     androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {  
  3.         exclude group: 'com.android.support', module: 'support-annotations'  
  4.     })  
  5.     compile fileTree(include: ['*.jar'], dir: 'libs')  
  6.     compile 'com.android.support:appcompat-v7:25.1.1'  
  7.     compile project(':openCVLibrary320')// 就是这行代码  
  8. }  

这样,我们的工作就算完成了。这样我们就算配置好了该有的资源了。


那么有的人又会问,我这样配置好了为什么系统还是存在需要安装manager 的APK文件呢?原因出在我们初始化openCV的时候!

Opencv 在初始化的时候需要执行这样一个API:

[java] view plain copy
  1. /** 
  2.  * Loads and initializes OpenCV library using OpenCV Engine service. 
  3.  * @param Version OpenCV library version. 
  4.  * @param AppContext application context for connecting to the service. 
  5.  * @param Callback object, that implements LoaderCallbackInterface for handling the connection status. 
  6.  * @return Returns true if initialization of OpenCV is successful. 
  7.  */  
  8. public static boolean initAsync(String Version, Context AppContext, LoaderCallbackInterface Callback){  
  9.     return AsyncServiceHelper.initOpenCV(Version, AppContext, Callback);  
  10. }  

进入到initOpenCV方法中我们可以看他是如何处理的:

[java] view plain copy
  1. public static boolean initOpenCV(String Version, final Context AppContext,final LoaderCallbackInterface Callback){  
  2.     AsyncServiceHelper helper = new AsyncServiceHelper(Version, AppContext, Callback);  
  3.     // 下面两句Intent就是启动OpenCV Manager中相关服务的  
  4.     Intent intent = new Intent("org.opencv.engine.BIND");  
  5.     intent.setPackage("org.opencv.engine");  
  6.     if (AppContext.bindService(intent, helper.mServiceConnection, Context.BIND_AUTO_CREATE)){  
  7.         return true;  
  8.     } else {  
  9.         AppContext.unbindService(helper.mServiceConnection);  
  10.         InstallService(AppContext, Callback);  
  11.         return false;  
  12.     }  
  13. }  
  14. // 由上面我们知道OpenCV Manager是如何给OpenCV Api提供动太库的。那我们如何不使用这个方法呢~OpencvLoader 所在的类有两个方法一直没有被使用过。我们可以找到看一下  
  15.   
  16. /** 
  17. *Loads and initializes OpenCV library from current application package. Roughly, it's an analog of system.loadLibrary("opencv_java"). 
  18. *@return Returns true is initialization of OpenCV was successful. 
  19. */  
  20.     public static boolean initDebug(){  
  21.         return StaticHelper.initOpenCV(false);  
  22.     }  
  23. /** 
  24. * Loads and initializes OpenCV library from current application package. Roughly, it's an analog of system.loadLibrary("opencv_java"). 
  25. * @param InitCuda load and initialize CUDA runtime libraries. 
  26. * @return Returns true is initialization of OpenCV was successful. 
  27. */  
  28.     public static boolean initDebug(boolean InitCuda){  
  29.         return StaticHelper.initOpenCV(InitCuda);  
  30.     }  


我们看注释Loads and initializes OpenCV library from current applicationpackage.Roughly,it's an analog of system.loadLibrary("opencv_java").

这说明,我们可以把所需要的包放到本地,但是是哪个包呢,从注释里可以大概知道应该是libopencv_java3.so这个包,即我们之前放在jniLibs中对应的.so。

我们跟进代码看一下

[java] view plain copy
  1. private static boolean initOpenCVLibs(String Libs){  
  2.     Log.d(TAG, "Trying to init OpenCV libs");  
  3.     boolean result = true;  
  4.     if ((null != Libs) && (Libs.length() != 0)){  
  5.         Log.d(TAG, "Trying to load libs by dependency list");  
  6.         StringTokenizer splitter = new StringTokenizer(Libs, ";");  
  7.         while(splitter.hasMoreTokens()){  
  8.         result &= loadLibrary(splitter.nextToken());  
  9.         }  
  10.     }else{  
  11.         // If dependencies list is not defined or empty.  
  12.         result &= loadLibrary("opencv_java3");  
  13.     }  
  14.     return result;  
  15. }  


Step3:最后一步

说到这里,还差最后一步了,就是使用本地so库。

在你使用界面的OnResume界面直接加一句下面的代码:

[java] view plain copy
  1. if (!OpenCVLoader.initDebug()){// 默认加载opencv_java.so库  
  2. }  

上面那两个基本相似的initDebug()一个是使用cuda利用gpu跑算法的,一个是不用的,从他的名字可以看出来.

这样不装Manager也就可以使用了。


结束~

[java] view plain copy
  1. <pre code_snippet_id="2225090" snippet_file_name="blog_20170224_4_2718679"></pre>  
  2. <pre></pre>  
  3. <pre></pre>  
  4. <pre></pre>  
  5. <pre></pre>  
  6. <pre></pre>  
  7. <pre></pre>  
  8.      
阅读全文
0 0
原创粉丝点击