androidstudio 生成.so库与调用

本文介绍了一个基于Android Studio的面部检测应用开发过程。该应用通过JNI接口调用C/C++实现的面部检测功能,并详细展示了如何配置build.gradle及CMakeLists.txt文件来构建项目。此外,还提供了关键的C/C++源代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

新建androidstudio工程,如图:这里写图片描述

build.gradle :

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.mediatek.camera.myapplication"
        minSdkVersion 23
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.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.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

CMakeLists.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.4.1)

# 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( # Sets the name of the library.
             jni_FaceDetect

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/c/facedetect_main.c
             src/main/cpp/c/native-lib.cpp
             )

# Specifies a path to native header files.
include_directories(src/main/cpp/include/)

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

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

facedetect_main.c:

/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define TAG "FaceDetect_JNI"
#include <com_android_camera_zf_FaceDetect.h>
#include <facedetect.h>
#include <android/log.h>
#include <stddef.h>
#include <stdio.h>

#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__))
#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, TAG, __VA_ARGS__))
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__))
// Log 开关
#define LOG_NDEBUG 0


/*
 * Class:     com_android_camera_zf_FaceDetect
 * Method:    tiredDetFree
 * Signature: ()V
 */
void JNICALL Java_com_android_camera_zf_FaceDetect_tiredDetFree(JNIEnv *env, jclass obj){
    //LOGI( " jni tiredDetFree() ");
    tiredDetFree();
}

/*
 * Class:     com_android_camera_zf_FaceDetect
 * Method:    tiredDetInit
 * Signature: ()V
 */
void JNICALL Java_com_android_camera_zf_FaceDetect_tiredDetInit(JNIEnv *env, jclass obj){
    LOGI( " jni tiredDetInit() ");
    tiredDetInit();
}

/*
 * Class:     com_android_camera_zf_FaceDetect
 * Method:    tiredDetProcess_new
 * Signature: ([BII[I[I[I[I[I[I[I)I
 */
jint JNICALL Java_com_android_camera_zf_FaceDetect_tiredDetProcess_1new
    (JNIEnv *env, jclass obj, jbyteArray jsrcData, jint jwidth, jint jheight, jintArray jopeneyes_count, jintArray jcloseeyes_count,
    jintArray jfaces_count, jintArray jfaceRect, jintArray joutPts, jintArray jfacePosStatus, jintArray jfaceEdgePos){

    //LOGI( " jni tiredDetProcess_1new() ");

    //srcData数据为图像yv12格式帧数据,如果只取y分量,请从srcData中截取 0---(jwidth*jheight-1)大小
    //其余int*参数, size均为int[1]
    jbyte* srcData = (jbyte*) (*env)->GetPrimitiveArrayCritical(env, jsrcData, 0);
    jint* openeyes_count = (jint*) (*env)->GetPrimitiveArrayCritical(env, jopeneyes_count, 0);
    jint* closeeyes_count = (jint*) (*env)->GetPrimitiveArrayCritical(env, jcloseeyes_count, 0);
    jint* faces_count = (jint*) (*env)->GetPrimitiveArrayCritical(env, jfaces_count, 0);
    jint* facePosStatus = (jint*) (*env)->GetPrimitiveArrayCritical(env, jfacePosStatus, 0);
    jint* faceEdgePos = (jint*) (*env)->GetPrimitiveArrayCritical(env, jfaceEdgePos, 0);

    int result =  tiredDetProcess_new(srcData,jwidth,jheight,
                         openeyes_count, closeeyes_count,
                         faces_count,
                         NULL,//CvRect* faceRect,
                         NULL, //FacePts *outPts,
                         facePosStatus,
                         faceEdgePos);
    (*env)->ReleasePrimitiveArrayCritical(env, jsrcData, srcData, 0);
    (*env)->ReleasePrimitiveArrayCritical(env, jopeneyes_count, openeyes_count, 0);
    (*env)->ReleasePrimitiveArrayCritical(env, jcloseeyes_count, closeeyes_count, 0);
    (*env)->ReleasePrimitiveArrayCritical(env, jfaces_count, faces_count, 0);
    (*env)->ReleasePrimitiveArrayCritical(env, jfacePosStatus, facePosStatus, 0);
    (*env)->ReleasePrimitiveArrayCritical(env, jfaceEdgePos, faceEdgePos, 0);
    return result;

}

//内存释放函数,只调用一次
void tiredDetFree(){
    //LOGI( " tiredDetFree() ");
}

//初始化函数,在程序启动前只调用一次
void tiredDetInit(){
    LOGI( " tiredDetInit() 111");
}

//主处理函数
int tiredDetProcess_new(unsigned char* srcData,int width,int height,
                     int *openeyes_count, int *closeeyes_count,
  					 int *faces_count,
  					 int *faceRect,//CvRect* faceRect,
  					 int *outPts, //FacePts *outPts,
  					 int *facePosStatus,
  					 int *faceEdgePos){
    LOGI( " tiredDetProcess_new() 111");
return 0;
}



其他部分(省略)
将生成一个jni_FaceDetect.so lib文件;并在mainactivity中调用;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gnimey

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值