• AndroidOpenCv配置

    2019-07-01 16:32:34
    cmake_minimum_required(VERSION 3.4.1) include_directories(src/main/cpp/include/) set( CMAKE_BUILD_TYPE Release ON...include_directories(${ANDROID_SYSROOT}/usr/include/arm-linux-androideabi) SET( CMAKE...
    cmake_minimum_required(VERSION 3.4.1)
    include_directories(src/main/cpp/include/)
    set( CMAKE_BUILD_TYPE Release ON)
    include_directories(${ANDROID_SYSROOT}/usr/include/arm-linux-androideabi)
    SET( CMAKE_C_FLAGS  "-O3 -pipe -fPIC -w -fmessage-length=0")
    SET( CMAKE_CXX_FLAGS "-O3 -pipe -fPIC -w -fmessage-length=0 -std=c++11")
    set( CMAKE_SHARED_LINKER_FLAGS "-Wl,-Bsymbolic" )
    add_library(libcpufeatures STATIC IMPORTED)
    set_target_properties(libcpufeatures PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libcpufeatures.a)
    add_library(libjpeg-turbo STATIC IMPORTED)
    set_target_properties(libjpeg-turbo PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/liblibjpeg-turbo.a)
    add_library(libtiff STATIC IMPORTED)
    set_target_properties(libtiff PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/liblibtiff.a)
    add_library(libwebp STATIC IMPORTED)
    set_target_properties(libwebp PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/liblibwebp.a)
    add_library(libjasper STATIC IMPORTED)
    set_target_properties(libjasper PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/liblibjasper.a)
    add_library(libpng STATIC IMPORTED)
    set_target_properties(libpng PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/liblibpng.a)
    add_library(IlmImf STATIC IMPORTED)
    set_target_properties(IlmImf PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libIlmImf.a)
    add_library(libprotobuf STATIC IMPORTED)
    set_target_properties(libprotobuf PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/liblibprotobuf.a)
    #add_library(carotene_objs STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    add_library(tegra_hal STATIC IMPORTED)
    set_target_properties(tegra_hal PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libtegra_hal.a)
    add_library(opencv_core STATIC IMPORTED)
    set_target_properties(opencv_core PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_core.a)
    add_library(opencv_imgproc STATIC IMPORTED)
    set_target_properties(opencv_imgproc PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_imgproc.a)
    add_library(opencv_imgcodecs STATIC IMPORTED)
    set_target_properties(opencv_imgcodecs PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_imgcodecs.a)
    add_library(opencv_videoio STATIC IMPORTED)
    set_target_properties(opencv_videoio PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_imgcodecs.a)
    add_library(opencv_highgui STATIC IMPORTED)
    set_target_properties(opencv_highgui PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_highgui.a)
    add_library(opencv_ts STATIC IMPORTED)
    set_target_properties(opencv_ts PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_ts.a)
    #add_library(opencv_test_core STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    #add_library(opencv_perf_core STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    add_library(opencv_flann STATIC IMPORTED)
    set_target_properties(opencv_flann PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_flann.a)
    #add_library(opencv_test_flann STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    #add_library(opencv_test_imgproc STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    #add_library(opencv_perf_imgproc STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    add_library(opencv_ml STATIC IMPORTED)
    set_target_properties(opencv_ml PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_ml.a)
    #add_library(opencv_test_ml STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    add_library(opencv_objdetect STATIC IMPORTED)
    set_target_properties(opencv_objdetect PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_objdetect.a)
    #add_library(opencv_test_objdetect STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    #add_library(opencv_perf_objdetect STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    add_library(opencv_photo STATIC IMPORTED)
    set_target_properties(opencv_photo PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_photo.a)
    #add_library(opencv_test_photo STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    #add_library(opencv_perf_photo STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    add_library(opencv_dnn STATIC IMPORTED)
    set_target_properties(opencv_dnn PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_dnn.a)
    #add_library(opencv_test_dnn STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    #add_library(opencv_perf_dnn STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    #add_library(opencv_test_imgcodecs STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    #add_library(opencv_perf_imgcodecs STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    #add_library(opencv_test_videoio STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    #add_library(opencv_perf_videoio STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    #add_library(opencv_test_highgui STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    add_library(opencv_features2d STATIC IMPORTED)
    set_target_properties(opencv_features2d PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_features2d.a)
    #add_library(opencv_test_features2d STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    #add_library(opencv_perf_features2d STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    add_library(opencv_calib3d STATIC IMPORTED)
    set_target_properties(opencv_calib3d PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_calib3d.a)
    #add_library(opencv_test_calib3d STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    #add_library(opencv_perf_calib3d STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    add_library(opencv_shape STATIC IMPORTED)
    set_target_properties(opencv_shape PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_shape.a)
    #add_library(opencv_test_shape STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    add_library(opencv_stitching STATIC IMPORTED)
    set_target_properties(opencv_stitching PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_stitching.a)
    #add_library(opencv_test_stitching STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    #add_library(opencv_perf_stitching STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    add_library(opencv_video STATIC IMPORTED)
    set_target_properties(opencv_video PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_video.a)
    #add_library(opencv_test_video STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    #add_library(opencv_perf_video STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    add_library(opencv_videostab STATIC IMPORTED)
    set_target_properties(opencv_videostab PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_videostab.a)
    #add_library(opencv_test_videostab STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    add_library(opencv_superres STATIC IMPORTED)
    set_target_properties(opencv_superres PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_superres.a)
    #add_library(opencv_test_superres STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    #add_library(opencv_perf_superres STATIC src/main/jniLibs/${ANDROID_ABI}/libcpufeatures)
    
    add_library(native-lib SHARED
        src/main/cpp/native-lib.cpp)
    find_library(log-lib log )
    target_link_libraries(native-lib
        opencv_superres
        opencv_videostab
        opencv_video
        opencv_stitching
        opencv_shape
        opencv_calib3d
        opencv_features2d
        opencv_dnn
        opencv_photo
        opencv_objdetect
        opencv_ml
        opencv_flann
        opencv_ts
        opencv_highgui
        opencv_videoio
        opencv_imgcodecs
        opencv_imgproc
        opencv_core
        tegra_hal
        libprotobuf
        IlmImf
        libpng
        libjasper
        libwebp
        libtiff
        libjpeg-turbo
        libcpufeatures
     ${log-lib})
    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 28
        defaultConfig {
            applicationId "com.wjs.rxjava.opencvscan"
            minSdkVersion 22
            targetSdkVersion 28
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
            externalNativeBuild {
                cmake {
                    abiFilters "armeabi-v7a"
                    cppFlags "-std=c++11","-frtti", "-fexceptions"
                }
            }
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
        externalNativeBuild {
            cmake {
                path "CMakeLists.txt"
                version "3.10.2"
            }
        }
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:28.0.0'
        implementation 'com.android.support.constraint:constraint-layout:1.1.3'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    }
    

     

    展开全文
  • 原博文:前言:最近在做项目移植,项目较大,在Eclipse中配置的Jni及OpenCV环境没任何问题,但是迁移到Studio中就问题一大堆,网上也找了一些资料参考及学习,很感谢前人留下的总结及经验。关于在AS中使用jni及配置...

    原博文

    前言:最近在做项目移植,项目较大,在Eclipse中配置的Jni及OpenCV环境没任何问题,但是迁移到Studio中就问题一大堆,网上也找了一些资料参考及学习,很感谢前人留下的总结及经验。关于在AS中使用jni及配置OpenCV也研究了一段时间,怕日后忘记便在此记录下,另一方面也可以给一些新手提供捷径,少走些弯路。

    1、下载及目录介绍

    进入官网(http://opencv.org/)下载OpenCV4Android并解压(这里是OpenCV-2.4.9-android-sdk)。下面是目录的结构图:

    sdk 目录即是我们开发opencv所需要的类库;

    samples 目录中存放着若干opencv应用示例(包括人脸检测等),可为我们进行android下的opencv开发提供参考;

    doc 目录为opencv类库的使用说明及api文档等;

    apk 目录则存放着对应于各内核版本的OpenCV应用安装包,如下图;


    (用来管理手机设备中的opencv类库,在运行opencv应用之前,必须确保手机中已经安装了OpenCV_2.4.3.2_Manager_2.4_*.apk,否则opencv应用将会因为无法加载opencv类库而无法运行)

     


    2、将OpenCV引入Android Studio

    这里需要注意一点,你可以直接在Android Studio中选择 File-->Import Module,找到OpenCV解压的路径,选择sdk/java文件夹作为Module进行导入。但便于维护,此处本人的建议是,找到sdk下java目录,将其拷贝到你的StudioProject项目目录下,在进行引入。


     

    3、更新build.gradle信息

    导入后会因为Gradle中的配置问题出现错误,在Studio中的左上角选择Project视图,在引入的OpenCVLibrary文件夹下,打开build.gradle(注意是引入的openCVLibrary249下),修改文件中以下信息:

    1)compileSdkVersion

    2)buildToolsVersion

    3)minSdkVersion

    4)targetSdkVersion(将其内容与app文件夹下的build.gradle中信息相一致

    如下图所示:




    然后点击Gradle进行同步(Sync Gradle)。

     

    4、添加Module Dependency

    选择File --->Project Structure,在app module的Dependencies一栏中,点击右上角的绿色加号,将openCVLibrary添加进去,点击确定。


     

    5、复制libs文件夹到项目中

    在OpenCV的解压包中,将sdk-->native-->libs文件夹复制,粘贴在Project视图下app-->src-->main目录下,并将其重命名为jniLibs

     

    到此,OpenCV的环境就配置好了,下面用个示例来测试下。这里参考了一篇博文,在此也感谢博主: http://blog.csdn.net/yanzi1225627/article/details/16917961


    6、示例演示

    OK!先看看OpenCVActivity中是如何写的:

    [java] view plain copy
    1. public class OpenCVActivity extends Activity{  
    2.   
    3.     private Button btn;  
    4.     private ImageView img;  
    5.   
    6.     private Bitmap srcBitmap;  
    7.     private Bitmap grayBitmap;  
    8.     private static boolean flag = true;  
    9.     private static boolean isFirst = true;  
    10.     private static final String TAG = "gao_chun";  
    11.   
    12.   
    13.     @Override  
    14.     protected void onCreate(Bundle savedInstanceState) {  
    15.         super.onCreate(savedInstanceState);  
    16.         setContentView(R.layout.activity_opencv);  
    17.   
    18.         img = (ImageView)findViewById(R.id.img);  
    19.         btn = (Button)findViewById(R.id.btn);  
    20.         btn.setOnClickListener(new ProcessClickListener());  
    21.     }  
    22.   
    23.   
    24.     @Override  
    25.     protected void onResume() {  
    26.         super.onResume();  
    27.         //load OpenCV engine and init OpenCV library  
    28.         OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, getApplicationContext(), mLoaderCallback);  
    29.         Log.i(TAG, "onResume sucess load OpenCV...");  
    30.     }  
    31.   
    32.   
    33.     //OpenCV库加载并初始化成功后的回调函数  
    34.     private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {  
    35.   
    36.         @Override  
    37.         public void onManagerConnected(int status) {  
    38.             // TODO Auto-generated method stub  
    39.             switch (status){  
    40.                 case BaseLoaderCallback.SUCCESS:  
    41.                     Log.i(TAG, "成功加载");  
    42.                     break;  
    43.                 default:  
    44.                     super.onManagerConnected(status);  
    45.                     Log.i(TAG, "加载失败");  
    46.                     break;  
    47.             }  
    48.         }  
    49.     };  
    50.   
    51.   
    52.   
    53.     public void procSrc2Gray(){  
    54.         Mat rgbMat = new Mat();  
    55.         Mat grayMat = new Mat();  
    56.         srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.genie);  
    57.         grayBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), Bitmap.Config.RGB_565);  
    58.         Utils.bitmapToMat(srcBitmap, rgbMat);//convert original bitmap to Mat, R G B.  
    59.         Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY);//rgbMat to gray grayMat  
    60.         Utils.matToBitmap(grayMat, grayBitmap); //convert mat to bitmap  
    61.         Log.i(TAG, "procSrc2Gray sucess...");  
    62.     }  
    63.   
    64.   
    65.     public class ProcessClickListener implements View.OnClickListener{  
    66.   
    67.         @Override  
    68.         public void onClick(View v) {  
    69.             // TODO Auto-generated method stub  
    70.             if(isFirst){  
    71.                 procSrc2Gray();  
    72.                 isFirst = false;  
    73.             }  
    74.             if(flag){  
    75.                 img.setImageBitmap(grayBitmap);  
    76.                 btn.setText("查看原图");  
    77.                 flag = false;  
    78.             }else{  
    79.                 img.setImageBitmap(srcBitmap);  
    80.                 btn.setText("灰度化");  
    81.                 flag = true;  
    82.             }  
    83.         }  
    84.     }  
    85.   
    86. }  

    再来看看简单的布局界面 activity_opencv.xml

    [java] view plain copy
    1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    2.                 android:layout_width="match_parent"  
    3.                 android:layout_height="match_parent">  
    4.   
    5.     <TextView  
    6.         android:layout_width="wrap_content"  
    7.         android:layout_height="wrap_content"  
    8.         android:layout_alignParentTop="true"  
    9.         android:text="OpenCV"/>  
    10.   
    11.     <ImageView  
    12.         android:id="@+id/img"  
    13.         android:layout_width="wrap_content"  
    14.         android:layout_height="wrap_content"  
    15.         android:layout_centerInParent="true"  
    16.         android:background="@drawable/genie"/>  
    17.   
    18.     <Button  
    19.         android:id="@+id/btn"  
    20.         android:layout_width="wrap_content"  
    21.         android:layout_height="wrap_content"  
    22.         android:layout_below="@id/img"  
    23.         android:layout_centerHorizontal="true"  
    24.         android:text="灰度化"/>"  
    25.   
    26. </RelativeLayout>  


    运行后的效果图:

       


    按照步骤来,基本没多大问题,想了想Demo还是上传供大家参考吧,不过由于拷出来后发现整个包有220多M过于恐怖,索性就删掉了一些东西,将app目录下的build目录下内容删除,将src ---> main ---> jniLibs 下的.so库删除了,注意:若下载demo运行,需要在OpenCV的解压包目录下,将sdk-->native-->libs文件夹下的四个目录复制到项目的jniLibs目录下便可。


    下载链接:http://download.csdn.net/download/gao_chun/9206935


    Android中使用OpenCV(二)



    【转载注明gao_chun的Blog:http://blog.csdn.net/gao_chun/article/details/49359535】


    展开全文
  • 前言:最近在做项目移植,项目较大,在Eclipse中配置的Jni及OpenCV环境没任何问题,但是迁移到Studio中就问题一大堆,网上也找了一些资料参考及学习,很感谢前人留下的总结及经验。关于在AS中使用jni及配置OpenCV也...

    Android Studio配置及使用OpenCV



    前言:最近在做项目移植,项目较大,在Eclipse中配置的Jni及OpenCV环境没任何问题,但是迁移到Studio中就问题一大堆,网上也找了一些资料参考及学习,很感谢前人留下的总结及经验。关于在AS中使用jni及配置OpenCV也研究了一段时间,怕日后忘记便在此记录下,另一方面也可以给一些新手提供捷径,少走些弯路。


    1、下载及目录介绍

    进入官网(http://opencv.org/)下载OpenCV4Android并解压(这里是OpenCV-2.4.9-android-sdk)。下面是目录的结构图:

    sdk 目录即是我们开发opencv所需要的类库;

    samples 目录中存放着若干opencv应用示例(包括人脸检测等),可为我们进行android下的opencv开发提供参考;

    doc 目录为opencv类库的使用说明及api文档等;

    apk 目录则存放着对应于各内核版本的OpenCV应用安装包,如下图;


    (用来管理手机设备中的opencv类库,在运行opencv应用之前,必须确保手机中已经安装了OpenCV_2.4.3.2_Manager_2.4_*.apk,否则opencv应用将会因为无法加载opencv类库而无法运行)

     


    2、将OpenCV引入Android Studio

    这里需要注意一点,你可以直接在Android Studio中选择 File-->Import Module,找到OpenCV解压的路径,选择sdk/java文件夹作为Module进行导入。但便于维护,此处本人的建议是,找到sdk下java目录,将其拷贝到你的StudioProject项目目录下,在进行引入。


     

    3、更新build.gradle信息

    导入后会因为Gradle中的配置问题出现错误,在Studio中的左上角选择Project视图,在引入的OpenCVLibrary文件夹下,打开build.gradle(注意是引入的openCVLibrary249下),修改文件中以下信息:

    1)compileSdkVersion

    2)buildToolsVersion

    3)minSdkVersion

    4)targetSdkVersion(将其内容与app文件夹下的build.gradle中信息相一致

    如下图所示:




    然后点击Gradle进行同步(Sync Gradle)。

     

    4、添加Module Dependency

    选择File --->Project Structure,在app module的Dependencies一栏中,点击右上角的绿色加号,将openCVLibrary添加进去,点击确定。


     

    5、复制libs文件夹到项目中

    在OpenCV的解压包中,将sdk-->native-->libs文件夹复制,粘贴在Project视图下app-->src-->main目录下,并将其重命名为jniLibs

     

    到此,OpenCV的环境就配置好了,下面用个示例来测试下。这里参考了一篇博文,在此也感谢博主: http://blog.csdn.net/yanzi1225627/article/details/16917961


    6、示例演示

    OK!先看看OpenCVActivity中是如何写的:

    public class OpenCVActivity extends Activity{
    
        private Button btn;
        private ImageView img;
    
        private Bitmap srcBitmap;
        private Bitmap grayBitmap;
        private static boolean flag = true;
        private static boolean isFirst = true;
        private static final String TAG = "gao_chun";
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_opencv);
    
            img = (ImageView)findViewById(R.id.img);
            btn = (Button)findViewById(R.id.btn);
            btn.setOnClickListener(new ProcessClickListener());
        }
    
    
        @Override
        protected void onResume() {
            super.onResume();
            //load OpenCV engine and init OpenCV library
            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, getApplicationContext(), mLoaderCallback);
            Log.i(TAG, "onResume sucess load OpenCV...");
        }
    
    
        //OpenCV库加载并初始化成功后的回调函数
        private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    
            @Override
            public void onManagerConnected(int status) {
                // TODO Auto-generated method stub
                switch (status){
                    case BaseLoaderCallback.SUCCESS:
                        Log.i(TAG, "成功加载");
                        break;
                    default:
                        super.onManagerConnected(status);
                        Log.i(TAG, "加载失败");
                        break;
                }
            }
        };
    
    
    
        public void procSrc2Gray(){
            Mat rgbMat = new Mat();
            Mat grayMat = new Mat();
            srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.genie);
            grayBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), Bitmap.Config.RGB_565);
            Utils.bitmapToMat(srcBitmap, rgbMat);//convert original bitmap to Mat, R G B.
            Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY);//rgbMat to gray grayMat
            Utils.matToBitmap(grayMat, grayBitmap); //convert mat to bitmap
            Log.i(TAG, "procSrc2Gray sucess...");
        }
    
    
        public class ProcessClickListener implements View.OnClickListener{
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(isFirst){
                    procSrc2Gray();
                    isFirst = false;
                }
                if(flag){
                    img.setImageBitmap(grayBitmap);
                    btn.setText("查看原图");
                    flag = false;
                }else{
                    img.setImageBitmap(srcBitmap);
                    btn.setText("灰度化");
                    flag = true;
                }
            }
        }
    
    }

    再来看看简单的布局界面 activity_opencv.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:text="OpenCV"/>
    
        <ImageView
            android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/genie"/>
    
        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/img"
            android:layout_centerHorizontal="true"
            android:text="灰度化"/>"
    
    </RelativeLayout>


    运行后的效果图:

       


    按照步骤来,基本没多大问题,想了想Demo还是上传供大家参考吧,不过由于拷出来后发现整个包有220多M过于恐怖,索性就删掉了一些东西,将app目录下的build目录下内容删除,将src ---> main ---> jniLibs 下的.so库删除了,注意:若下载demo运行,需要在OpenCV的解压包目录下,将sdk-->native-->libs文件夹下的四个目录复制到项目的jniLibs目录下便可。


    下载链接:http://download.csdn.net/download/gao_chun/9206935


    Android中使用OpenCV(二)



    【转载注明gao_chun的Blog:http://blog.csdn.net/gao_chun/article/details/49359535】


    展开全文
  • Android Studio中配置OpenCV 配置时软件库的版本:Android Studio版本3.1.2,OpenCV版本3.4.0   最近在做数字手势识别的APP,把TensorFlow训练好的模型迁移到Android studio上进行相应APP的开发,因为要把摄像头...

    Android Studio中配置OpenCV

    配置时软件库的版本:Android Studio版本3.1.2,OpenCV版本3.4.0

      最近在做数字手势识别的APP,把TensorFlow训练好的模型迁移到Android studio上进行相应APP的开发,因为要把摄像头拍到的图片压缩到64*64大小,而TensorFlow Lite并没有pc端提供的压缩函数。因为使用的area interpolation(面积插值法),搜了很久并没有找到这个算法的java实现,而且为了最大可能算法实现和TensorFlow中保持一致,因为不一致会导致识别准确率大幅下降,仔细查看TensorFlow官方文档,发现这么一句话:

    Each output pixel is computed by first transforming the pixel’s footprint into the input tensor and then averaging the pixels that intersect the footprint. An input pixel’s contribution to the average is weighted by the fraction of its area that intersects the footprint. This is the same as OpenCV’s INTER_AREA.

      所以开启了在Android Studio中的OpenCV配置之路,先去官网下载OpenCV的AndroidSDK即opencv-3.4.0-android-sdk.zip,然后解压。
      我以我这里的Android project作为示例,*请确保你的Android project的compileSdkVersion版本大于等于21,否则的话会报 package android.hardware.camera2不存在错误,这是因为camera2是在sdkAPI21中引入的。

    第一步,选择file->new->import module 导入刚刚解压的opencv-3.4.0-android-sdk,选择OpenCV-android-sdk\sdk\java,如下图所示,此时能够看到OpenCVLibrary340被导入进来,然后next->finish即可。导入后,我们的project目录下有一个OpenCVLibrary340的目录。

    导入OpenCV模块

    第二步,在project中添加依赖,具体步骤如下图所示:
    添加依赖

    按照上图步骤第5步后,在弹窗中选择OpenCVLibrary340,点击OK即可。

    第三步,把opencv-3.4.0-android-sdk\OpenCV-android-sdk\sdk\native\libs目录下的库复制到我们的project的app\libs目录下,具体如图所示:

    复制库

    这个目录下其实存放的是CPU架构相关的库,可以符合自己CPU架构的复制进去。我这里复制了armeabi和armeabi-v7a两个。(注:如果你是X86的架构就把x86的放进去,对应自己CPU架构。我模拟器设的X86,原来没放X86的导致没法在模拟器中安装apk)

    还要更改下OpenCV的build.gradle里的内容,主要是sdk版本问题,改成和我们app里(build.gradle)的一致就好了,如下图所示:

    OpenCV

    第四步,在 app (别搞错了)的gradle.build里配置引用刚刚复制进来的库,即把下面的代码添加到gradle.build文件里的android结点下:

    sourceSets {
            main {
                jni.srcDirs = []
                jniLibs.srcDirs = ['libs']
            }
        }
    

    gradle配置

    然后点击 sync now同步下。正常情况gradle编译都是一路成功的。
    上面配置过程一路坐下来,如果你的gradle没有报错信息,那么恭喜你,配置成功了。下面就可以调用OpenCV库进行开发了。


    在开发的时候,要先在用到的java文件里先加载OpenCV初始化库,即添加下面代码:

    static {
            if(!OpenCVLoader.initDebug())
            {
                Log.d("opencv","初始化失败");
            }
        }

    初试化

    下面来小试牛刀一下,用OpenCV的area interpolation算法把图片压缩。
    代码:

    package com.example.hc.test;
    
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.ImageView;
    
    import org.opencv.android.OpenCVLoader;
    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 {
        static {
            if(!OpenCVLoader.initDebug())
            {
                Log.d("opencv","初始化失败");
            }
        }
        private ImageView imageView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            imageView = (ImageView)findViewById(R.id.image);
            Bitmap bitmap = BitmapFactory.decodeResource(this.getApplicationContext().getResources(), R.drawable.aomei1);
            Mat src = new Mat();
            Mat dst = new Mat();
            Utils.bitmapToMat(bitmap, src);
            //new Size(width, height)
            Imgproc.resize(src, dst, new Size(400,600),0,0,Imgproc.INTER_AREA);
            Bitmap bitmap1 = Bitmap.createBitmap(dst.cols(),dst.rows(),Bitmap.Config.RGB_565);
            Utils.matToBitmap(dst, bitmap1);
            imageView.setImageBitmap(bitmap1);
        }
    }

    拿张奥妹的照片,来看看效果,原图:

    奥妹

    压缩图:

    奥妹压缩



    感谢 拍码屁 的博客 Android Studio OpenCV环境的简单搭建,当时配置的时候参考了很多。

    展开全文
  • Android Studio上安装Opencv配置环境 首先我们需要下载一个android-opencv-sdk 这里是下载地址https://sourceforge.net/projects/opencvlibrary/files/?source=navbar 点击opencv-android下载你需要的版本,...

    Android Studio上安装Opencv并配置环境

    首先我们需要下载一个android-opencv-sdk  这里是下载地址https://sourceforge.net/projects/opencvlibrary/files/?source=navbar

    点击opencv-android下载你需要的版本,这里以3.3为例

    下载完成后进行环境的配置

    1、新建一个android工程

    2、导入一个Module

    File----》new----》Import Module

    选择你刚刚下载sdk的路径如下

    然后一直点击next到完成。

    然后导入到app工程中 选中app右键,点击Open Moudle Settings

    选择第3个添加Moudle

     

    3、配置build.gradle文件

    opencv3.3里面的build.gradle要和app里的build.gradle的四个版本信息要一样

    compileSdkVersion、buildToolsVersion、minSdkVersion 、targetSdkVersion (一定要一致才行)

    修改完 ,点击上方的Sync now同步信息

     

    4、将sdk--》native---》libs整个文件夹复制 到工程的app--》src--》main下面并更名为jniLibs

     

    7、还有一步是dnk的配置(dnk去百度上搜索下载)

    File---》Projecr Structure

     

     

    6、测试

    sdk里面有源码大家可以直接拿来测试

    sdk--》samples下面得到源码都可以拿来测试的。能成功运行即说明环境配置完成。

    展开全文
  • 软件最低配置Android 6.0版本以上.2 Android中使用OpenCV的环境配置下载Android Studio2.2版本下载OpenCV for Android SDK, 使用正常流程安装好Android Studio.解压OpenCV for Android SDK...
  • OpenCV android sdk配置OpenCV android NDK开发实例  【尊重原创,转载请注明出处】http://blog.csdn.net/guyuealian/article/details/78374708  在Android应用中调用OpenCV进行图像处理的方法有很多种,考虑到...
  • OPENCV4Android配置

    2020-04-02 22:27:23
    OPENCV4Android配置 首先根据这个链接走一遍 配置流程 然后我的电脑会显示一个问题,说是无法打开库文件 这个时候就需要修改 opencv的gradle 再下一步就是会报错,dependencies冲突,修改为 然后重启就不会报错了...
  • 要想在Android使用OpenCV,那么首先得搭建好开发环境,其实搭建开发环境并不难,只要理解其中的原理就自然手到擒来。开发环境搭建好后才能大展拳脚! (之前一直是在VS中使用OpenCV,VS中的配置很简单;在Android...
  • 我为什么要写《OpenCV Android 开发实战》这本书 2015年我出版了个人第一本关于图像处理方面的书籍《Java图像处理-编程技巧与应用实践》,这本书主要是从理论与编码上面详细阐述了图像处理基础算法以及它们在编码...
  • OpenCV是著名的跨平台计算机视觉开源库,广泛应用于计算机视觉相关领域。OpenCV 已经发布 Android 平台下的 SDK,可以直接导入 ...Android Studio项目中配置使用OpenCV Android SDK 在项目根目录下创建 libraries
  • Android配置OpenCv

    2019-10-22 11:41:47
    一.OpenCv下载 OpenCV安卓版本下载:https://www.opencv.org/releases.html下载版本3.4.1
  • 本文是从本人简书上搬运而来,属本人原创,如有转载,请注明出处:...通过对Android Studio里OpenCV环境配置讲解,快速帮新人解决入门配置问题。 本文内容是本人经过多次踩坑,并参考网上众多OpenCV On
  • Android Studio导入OpenCV Android SDK
  • 环境: ...3.opencv-3.4.2-android-sdk.zip 下载地址: https://sourceforge.net/projects/opencvlibrary/files/opencv-android/ 1.新建android项目 注意:选择Include C++ support ...
  • Android配置OpenCV

    2016-04-15 11:14:24
    手把手教opencvandroid配置,简单粗暴好用无副作用,我们直接开始吧:)
  • 配置OpenCV for Android

    2019-01-15 14:59:37
    参考: https://blog.csdn.net/u010097644/article/details/56849758 (环境构建) ... 环境介绍: Android Studio:3.2.0 Android SDK Tools:26.1.1 An...
  • 3 OpenCV for Android配置 3.1 创建项目 3.2 OpenCV相关文件 3.3 配置文件 3.3.1 build.gradle文件 3.3.2 CMakeList.txt文件 3.3.3 资源文件 4 测试Demo:灰度化图片 4.1 代码 4.2 程序...
  • 引言: OpenCV的Java接口是使用JNI的方式调用c++函数,所以首先需要添加opencv的dependencies作为Java的调用接口函数,然后拷贝jniLibs,...下载OpenCV-android-sdk,解压。 Step1:在project下新建文件夹libraries,
  • Android Studio 配置 OpenCV for Android 环境参考: Win10 64位 Android Studio 3.5.1 官网 OpenCV for Android 3.4.4 官网 下载好 OpenCV for Android 后直接解压即可(最好放在一个有意义的文件夹下),解压后...
1 2 3 4 5 ... 20
收藏数 6,495
精华内容 2,598