Androidstudio4 集成dlib

Androidstudio4 集成dlib,第1张

Androidstudio4 集成dlib
下面介绍在AS中集成dlib,通过CMakeList.txt方式

建议看这个GitHub - bookzhan/bzdlib: dlib demo for android,face tracker,face landmark

一、新建工程

1. 在创建工程的对话框中选择Include C++ support,如下图所示:

创建工程

2. 一直点击Next,直到最后一步。选择Exception Support(-fexceptions)和Runtime Type Information Support(-frtti),便于出错时的调试。如下图所示:

 调试支持

为了防止错误提示:Installed Build Tools revision 32.0.0 is corrupted. Remove and install again using the SDK Manager.

SDK选择30

 Project/build.gradle增加阿里云

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        maven{url "https://maven.aliyun.com/repository/central"}
        maven{url "https://maven.aliyun.com/repository/public"}
        maven{url "https://maven.aliyun.com/repository/google"}
        maven{url "https://maven.aliyun.com/repository/gradle-plugin"}
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.2.1"
    }
}

allprojects {
    repositories {
        maven{url "https://maven.aliyun.com/repository/central"}
        maven{url "https://maven.aliyun.com/repository/public"}
        maven{url "https://maven.aliyun.com/repository/google"}
        maven{url "https://maven.aliyun.com/repository/gradle-plugin"}
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Module/build.gradle设置编译的sdk

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.hc.facetest2"
        minSdkVersion 23
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ''
                abiFilters "armeabi-v7a", "arm64-v8a", "x86_64", "x86"
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    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.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

  • 配置文件

将opencv或dlib-19.22解压后整个文件,拷贝到main/cpp文件夹下面

AndroidManifest.xml文件,打开摄像头,和文件读写权限

          package="com.tzutalin.dlibtest">

    

    

    

    

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        android:supportsRtl="true"

        android:theme="@style/AppTheme">

        

            android:name=".MainActivity_"

            android:configChanges="orientation|screenSize"

            android:label="@string/app_name"

            android:theme="@style/AppTheme.NoActionBar">

            

                

                

            

        

    

  • 配置CMakeLists文件

Main/cpp/CMakeList.txt文件

# 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)

project("cmaketest2")

# 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.


include(./dlib-19.22/dlib/cmake)

# Include headers
add_library( # Sets the name of the library.
             native-lib
             # 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.
                       native-lib

                       # links the target library to the log library
                       # included in the NDK.
                       dlib ${log-lib} )

增加的dlib库名在dlib-19.22dlibCMakeList.txt文件夹找

project(dlib)  这个文件夹名就是

四、配置app/build.gradle

1. 添加CPU架构的支持

abiFilters  "armeabi-v7a", "arm64-v8a",  "x86", "x86_64"

如下:

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.hc.facetest2"
        minSdkVersion 23
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ''
                abiFilters "armeabi-v7a", "arm64-v8a"
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    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.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

五、测试文件 main/cpp/native-lib.cpp内容如下:
#include 
#include 
#include "dlib/dnn.h"
#include "dlib/clustering.h"
#include "dlib/string.h"
#include "dlib/image_io.h"
#include "dlib/image_processing/frontal_face_detector.h"


using namespace dlib;
using namespace std;


template