OpenCV for android as环境搭建

下载opencv库文件

首先下载opencv-3.4.1-android-sdk.zip并解压

网址 https://opencv.org/opencv-3-4-1.html

新建安卓app工程

改写activity_main.xml布局文件加入一个按钮和图片

图片需放入drawable目录



<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:text="测试opencv"/>

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/lena"/>


导入opencev

file->new->import module

填写opencv路径我的是

E:\opencv android\opencv-3.4.1-android-sdk\OpenCV-android-sdk\sdk\java

添加依赖

file->project structure

加号添加module dependency

更改opencv的build.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 14
    buildToolsVersion "26.0.2"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 21
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

改成

apply plugin: 'com.android.library'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 26
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

复制文件

E:\opencv android\opencv-3.4.1-android-sdk\OpenCV-android-sdk\sdk\native\libs

将上面目录下的所有文件复制到libs文件夹内


修改mainactivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        staticLoadCVLibrary();
        Button btn=findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                convert2gray();
            }
        });
    }

    private void staticLoadCVLibrary(){
        boolean load= OpenCVLoader.initDebug();
        if (load){
            Log.i("cv","Open CV Libraries loaded...");
        }
    }
    private void convert2gray(){
        Mat src= new Mat();
        Mat temp=new Mat();
        Mat dst=new Mat();
        Bitmap image= BitmapFactory.decodeResource(this.getResources(),R.drawable.lena);
        Utils.bitmapToMat(image,src);
        Imgproc.cvtColor(src,temp,Imgproc.COLOR_RGBA2BGR);
        Log.i("cv","image type:"+(temp.type() == CvType.CV_8UC3));
        Imgproc.cvtColor(temp,dst,Imgproc.COLOR_BGR2GRAY);
        Utils.matToBitmap(dst,image);
        ImageView imageView=findViewById(R.id.imageview);
        imageView.setImageBitmap(image);

        src.release();
        temp.release();
        dst.release();

    }
}

修改app的build.gradel 加入下面代码 注意不要忘记dependencies第二行

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation fileTree(dir: "$buildDir/native-libs",includes: 'native-libs.jar')
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation project(':openCVLibrary341')
}
task nativeLibsToJar(type: Jar, description: 'create a jar archive of the native libs') {
    destinationDir file("$buildDir/native-libs")
    baseName 'native-libs'
    from fileTree(dir: 'libs', include: '**/*.so')
    into 'lib/'
}

tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn(nativeLibsToJar)
}

到此已经可以编译工程进行测试了,效果图如下



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: OpenCV for Android 是一个开源的计算机视觉库,它提供了一系列用于计算机视觉应用的高效算法。OpenCV 是一个流行的计算机视觉库,它提供了许多用于图像和视频处理的功能。如果你想在 Android 应用程序中使用 OpenCV,你可以使用 OpenCV for Android。 在使用 OpenCV for Android 之前,你需要先安装 Android Studio 和 OpenCV for Android。安装完成后,你可以创建一个新的 Android 应用程序项目,并将 OpenCV for Android 添加到你的项目中。 要使用 OpenCV for Android,你需要在你的代码中添加 OpenCV 库的引用。你可以使用 OpenCV 提供的 Java API,或者使用 JNI 将 C++ 代码嵌入到你的应用程序中。 在使用 OpenCV for Android 时,你需要确保你的应用程序具有足够的权限,以便访问设备上的相机或图库等资源。你还需要测试你的应用程序,以确保它可以正确地使用 OpenCV 库来处理图像和视频数据。 总的来说,OpenCV for Android 是一个强大的工具,可以帮助你快速开发出高质量的 Android 应用程序,从而实现各种计算机视觉任务。 ### 回答2: OpenCV for Android是一个专门设计用于移动平台的图像处理库,它给Android系统提供了处理数字图像和视频的丰富功能和工具。OpenCV for Android可以作为一个Android应用中的库(.so文件),也可以通过Java API直接调用。它可以完成图像预处理、特征提取、物体检测、视频分析等多种图像处理功能。 OpenCV for Android用C++语言开发,但也提供了Java API,这使得开发者可以使用Java语言调用C++函数,而避免了直接使用NDK编写Native代码的复杂性。它的Java API设计良好,比较容易理解和使用。与此同时,OpenCV for Android提供了一个完整的示例应用程序,方便开发者学习和使用。 OpenCV for Android还具有高度的可裁剪性。开发者可以根据自己的需求,选择只使用OpenCV for Android库中的特定模块,而不必将整个库作为一个整体引入项目。这使得OpenCV for Android在应用中的集成更为灵活和轻量。 OpenCV for Android有一些特性,使其在处理大规模的图像和视频上更为高效和可靠。它使用了Java虚拟机 (JVM) 来缓存和管理内存,而OpenCV核心库会自动进行并行优化,以使程序在Android设备上运行得更快。此外,OpenCV for Android支持多种图像和视频格式,如JPG、PNG、BMP、AVI、MP4等。 总之,OpenCV for AndroidAndroid平台上提供了强大的图像和视频处理能力,为开发者带来了巨大的方便,并且可以快速集成到Android应用程序中。作为一个开源库,它的源代码和详细的文档也是公开可用的,这使得学习和使用OpenCV for Android更为容易和方便。 ### 回答3: OpenCV for Android是一款开源的计算机视觉库,它可以轻松地在Android平台上进行图像和视频处理。这个库具有广泛的功能,包括图像处理、目标识别、运动跟踪、面部识别和机器学习等等。 OpenCV for Android的使用非常简单和直接。可供选择的API包括Java API和C++ API。你可以使用Java API来编写易于理解和易于维护的代码,或者使用C++ API来获得更高的性能和更完整的OpenCV函数库。 OpenCV for Android也提供了丰富的开发工具。开发者可以使用Android Studio来编写和调试他们的OpenCV应用程序。同时,该库还提供了大量的示例代码和文档,以帮助开发者更快地开始开发他们的应用程序。 除此之外,OpenCV for Android还可以与其他库和工具进行集成,例如TensorFlow,Caffe和Git等。这些工具加强了OpenCV的功能,为开发者提供了更多的选择和灵活性。 总体而言,OpenCV for Android是一种强大而且易于使用的计算机视觉库,它适用于广泛的应用程序和项目。它的不断发展和完善也使其成为了学习计算机视觉技术的理想选择。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值