新建androidstudio工程,如图:

%title插图%num
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中调用