使用原因
最近项目中用到ndk camera,因此,学习了一下google所提供的ndk sample。
sample中有两个module,分别是basic和texture-view。texture-view module将textureview作为传参,传入ndk camera。而basic module通过android_native_app_glue框架绑定对应Activity,无需传入控件作为参数。
两者各有优点,texture-view更通俗易懂。但最终由于项目接口定义的原因(无控件参数传入),选用了android_native_app_glue框架进行开发。
如何使用
使用android_native_app_glue框架时,需要在Native和Java两端分别实现部分功能。
JAVA
上层需要实现的较为简单。仅需要创建一个继承于NativeActivity的Activity,并在Manifest.xml中将其与SDK中的某一动态库进行绑定即可。
(1)创建Activity
public class MainActivity extends NativeActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
(2)绑定
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.breakloop.ndkcamera">
<uses-permission android:name="android.permission.CAMERA" />
...
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:screenOrientation="sensorLandscape"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<meta-data android:name="android.app.lib_name"
android:value="native-lib" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
....
</application>
</manifest>
其中,android.app.lib_name指定了sdk中动态库的名称。
Native
(1)实现extern "C" void android_main(struct android_app* state)方法
在任意cpp文件中实现该方法,本文在android_main1.cpp中实现。android_main1的大致框架如下。
#include <android_native_app_glue.h>
extern "C" void android_main(struct android_app* state) {
state->userData = state;
//(1)指定cmd处理方法
state->onAppCmd = ProcessAndroidCmd;
// loop waiting for stuff to do.
while (1) {
// Read all pending events.
int events;
struct android_poll_source* source;
while (ALooper_pollAll(0, NULL, &events, (void**)&source) >= 0) {
// Process this event.
if (source != NULL) {
source->process(state, source);
}
// Check if we are exiting.
if (state->destroyRequested != 0) {
LOGI("CameraEngine thread destroy requested!");
//(2)TODO 析构
return;
}
}
//(3)TODO:画帧
}
}
static void ProcessAndroidCmd(struct android_app* app, int32_t cmd) {
switch (cmd) {
case APP_CMD_INIT_WINDOW:
//TODO INIT WINDOW
break;
case APP_CMD_TERM_WINDOW:
//TODO TERMINATE WINDOW
break;
case APP_CMD_CONFIG_CHANGED:
//TODO CONFIG CHANGED
break;
case APP_CMD_LOST_FOCUS:
//TODO FOR LOST FOCUS
break;
}
}
画帧操作可以放在android_main主循环中,ProcessAndroidCmd用于处理NativeActivity的不同事件。
(2)引入android_native_app_glue框架
在CMakelist.txt中配置完成,无需编程。先看一下整体的CMakelist内容。
cmake_minimum_required(VERSION 3.4.1)
//(1)引入native_app_glue框架头文件所在目录
include_directories(${ANDROID_NDK}/sources/android/native_app_glue
${COMMON_SOURCE_DIR})
//(2)将android_native_app_glue.c封装成app_glue静态库,否则在创建activity时,会crash
add_library(app_glue STATIC
${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)
//(3)暴露ANativeActivity_onCreate方法,否则在创建activity时,会crash
set(CMAKE_SHARED_LINKER_FLAGS
"${CMAKE_SHARED_LINKER_FLAGS} -u ANativeActivity_onCreate")
//(4)将android_main1封装至动态库native-lib中,而native-lib将在Manifest中被指定。android_main1实现了方法android_main。
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
...
android_main1.cpp
)
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)
//(5)一定要引入步骤(2)中封装的app_glue,否则创建activity时,将crash
target_link_libraries( # Specifies the target library.
native-lib
-landroid
camera2ndk
mediandk
GLESv2
app_glue
# Links the target library to the log library
# included in the NDK.
${log-lib})
结尾
遵循以上步骤,android_native_app_glue框架便可正常使用了。
若在NativieActivity创建完成前需要初始化其它内容(耗时的话),整个界面为黑色。
接下来便是NDK camera2接口的使用了。