OpenCv4Android:How to Start

Open Computer Vision

OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux、Windows、Android和Mac OS操作系统上。

Preface

按照sdk给的tutorials 记录上手实践过程

OpenCv4Android

OpenCv4Android is available as a SDK with a set of samples and Javadoc documentation for OpenCV Java API.

OpenCV4Android Samples

本文基于 opencv-4.5.3-android-sdk

OpenCV provides a set of samples for Android developers. These samples show how OpenCV can be used from both Java and native level of Android.
首先演示的sample如下

Tutorial 1 – Camera Preview – shows the simplest way Android application can use OpenCV, i.e. via OpenCV application helper classes. It displays full screen preview using either Java or Native camera API and allows switching between them.

sdk中的location:OpenCV-android-sdk\samples\tutorial-1-camerapreview
在这里插入图片描述导入sdk
在这里插入图片描述todo: next不亮 无疾而终

在这里插入图片描述
sdk中带的project没有提供
所以切换到了新source 继续探索:https://github.com/floe/opencv-tutorial-1-camerapreview
这个demo需要OpenCv manager, 对新人不友好

建议使用如下sample进行实践:

Sample – face-detection – is the simplest implementation of the face detection functionality on Android. It supports 2 modes of execution: available by default Java wrapper for the cascade classifier, and manually crafted JNI call to a native class which supports tracking. Even Java version is able to show close to the real-time performance on a Google Nexus One device.

自己造轮子

决定自己写一个Demo 调通调用关系
在这里插入图片描述修改build.gradle

plugins {
    id 'com.android.application'
}

android {
    compileSdk 30

    defaultConfig {
        applicationId "com.example.opencvdemo"
        minSdk 23
        targetSdk 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags '-std=c++11'
                abiFilters'arm64-v8a','armeabi-v7a'
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets.main {
        jniLibs.srcDir "src/main/jniLibs"
        jni.srcDirs = []
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    externalNativeBuild {
        cmake {
            path file('src/main/cpp/CMakeLists.txt')
            version '3.10.2'
        }
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

cmake:

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.10.2)

# Declares and names the project.

set(opencvlibs "${CMAKE_SOURCE_DIR}/../jniLibs")

include_directories(${CMAKE_SOURCE_DIR}/../jni/include)



project("opencvdemo")

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library(libopencv_java4 SHARED IMPORTED)

set_target_properties(libopencv_java4 PROPERTIES IMPORTED_LOCATION
        "${opencvlibs}/${ANDROID_ABI}/libopencv_java4.so")

add_library( # Sets the name of the library.
        opencvdemo

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        native-lib.cpp)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
        opencvdemo
        libopencv_java4
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

在这里插入图片描述
在这里插入图片描述
将如上资源添加进project 如下所示:
在这里插入图片描述

新轮子问题解决

编译时遇到问题

Execution failed for task ':app:mergeDebugNativeLibs'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
   > More than one file was found with OS independent path 'lib/arm64-v8a/libopencv_java4.so'. If you are using jniLibs and CMake IMPORTED targets, see https://developer.android.com/studio/preview/features#automatic_packaging_of_prebuilt_dependencies_used_by_cmake

方案:
build.gralde中新增

    packagingOptions {
        pickFirst 'lib/arm64-v8a/libopencv_java4.so'
        pickFirst 'lib/armeabi-v7a/libopencv_java4.so'
    }

运行时的强制关闭

2021-07-16 16:45:13.369 4923-4923/com.example.opencvdemo E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.opencvdemo, PID: 4923
    java.lang.UnsatisfiedLinkError: dlopen failed: library "libc++_shared.so" not found
        at java.lang.Runtime.loadLibrary0(Runtime.java:1016)
        at java.lang.System.loadLibrary(System.java:1669)
        at com.example.opencvdemo.MainActivity.<clinit>(MainActivity.java:14)
        at java.lang.Class.newInstance(Native Method)
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1216)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2870)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3087)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1817)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6746)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

解决方案:
修改build.gradle

        externalNativeBuild {
            cmake {
                cppFlags '-std=c++11'
                abiFilters'arm64-v8a','armeabi-v7a'
                arguments  "-DANDROID_STL=c++_shared" //解决"libc++_shared.so" not found
                
            }

分析app包可以看到
opencv已经导入
在这里插入图片描述

尝试新增功能

彩虹原图
在这里插入图片描述

图像处理后
在这里插入图片描述
代码已共享

https://github.com/wnwtest/start-open-cv

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值