In my previous post I explained
how to build libyuv shared library now I would like to explain how to write a JNI wrapper and access the libyuv functions available.
Please go through the above post before proceeding, as I'll be frequently referring to it.
From the post above, If you have successfully built the libyuv shared library then you should see the .so's generated at the location :-
LRD/libs/armeabi/libyuv_shared.so
LRD/libs/armeabi-v7a/libyuv_shared.so
LRD/libs/mips/libyuv_shared.so
LRD/libs/x86/libyuv_shared.so
We'll be needing the .so's, for the JNI wrapper will be accessing the functions present in it.
Create the following directory structures in your workspace:-
LibYUVDemo/prebuilt/
LibYUVDemo/include/
LibYUVDemo/jni/
Under the
LibYUVDemo/prebuilt/ directoy paste the libyuv_shared.so files in the respective order :-
LibYUVDemo/prebuilt/armeabi/libyuv_shared.so
LibYUVDemo/prebuilt/armeabi-v7a/libyuv_shared.so
LibYUVDemo/prebuilt/mips/libyuv_shared.so
LibYUVDemo/prebuilt/x86/libyuv_shared.so
Copy the contents of include folder from libyuv source and paste it under
LibYUVDemo/include/
Now the actual JNI wrapper, create LibYUVDemo.cpp under
LibYUVDemo/jni/ and write a jni function as follows :-
#include <jni.h>
#include <string.h>
#include <stdlib.h>
#include "libyuv.h"
#include <android/log.h>
#define LOG_TAG "YUVDemo"
#define LOGI(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define printf(...) LOGI(__VA_ARGS__)
using namespace libyuv;
extern "C" {
JNIEXPORT void Java_com_demo_libyuvdemo_MainActivity_callLibYUV(JNIEnv* env, jobject thiz) {
libyuv::RotationMode mode = libyuv::kRotate180;
/*libyuv::ConvertToI420(const unsigned char *, unsigned int, unsigned char *, int, unsigned char *, int, unsigned char *, int, int, int, int, int, int, int, enum libyuv::RotationMode, unsigned int);
*/
printf("rotation mode selected = %d",mode);
}
Create an Android.mk file under
LibYUVDemo/jni/
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := yuv_shared
LOCAL_SRC_FILES := ../prebuilt/$(TARGET_ARCH_ABI)/libyuv_shared.so
ifneq (,$(wildcard $(LOCAL_PATH)/$(LOCAL_SRC_FILES)))
include $(PREBUILT_SHARED_LIBRARY)
endif
include $(CLEAR_VARS)
LOCAL_MODULE := YUVDemo
LOCAL_SRC_FILES := LibYUVDemo.cpp
#LOCAL_SRC_FILES := LibYUVDemo.c
#
# Header Includes
#
LOCAL_C_INCLUDES := \
$(LOCAL_PATH)/../include
#
# Compile Flags and Link Libraries
#
LOCAL_CFLAGS := -DANDROID_NDK
LOCAL_LDLIBS := -llog
LOCAL_SHARED_LIBRARIES := yuv_shared
include $(BUILD_SHARED_LIBRARY)
Finally create an Application.mk file under
LibYUVDemo/jni/
APP_ABI := all
APP_PLATFORM := android-9
APP_STL := stlport_shared
Now navigate to
LibYUVDemo/jni/ from command prompt and trigger
ndk-build, the JNI should get compiled and you must have the new .so geneated at
LibYUVDemo/libs/ for the all the architectures.
A complete working app is available at
github.