Android Studio 下通过 CMake 配置编译 libyuv 库
【摘要】
参考链接
创建 AS 工程,选择 Native C++,之后默认配置就可以了 (ps: 我工程的名字也取名为 libyuv 了,你们随意)
libyuv 的源码下载下来以后,放到 src/main/cpp 目录下
修改 src/main/cpp 目录下的 CMakeList.txt
# For mo...
创建 AS 工程,选择 Native C++,之后默认配置就可以了 (ps: 我工程的名字也取名为 libyuv 了,你们随意)
libyuv 的源码下载下来以后,放到 src/main/cpp 目录下
修改 src/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.4.1)
-
-
include_directories(/home/lingyun/Projects/AndroidStudioProjects/libyuv/app/src/main/cpp/libyuv/include) # 重点,根据自己的路径进行修改
-
add_subdirectory(/home/lingyun/Projects/AndroidStudioProjects/libyuv/app/src/main/cpp/libyuv) # 重点
-
-
# 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.
-
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.
-
${log-lib}
-
yuv) # 重点
然后再修改 libyuv 库的 CMakeList.txt (去掉一些编译生成的文件,也可以不改,按自己需要编辑)
-
# CMakeLists for libyuv
-
# Originally created for "roxlu build system" to compile libyuv on windows
-
# Run with -DTEST=ON to build unit tests
-
-
PROJECT ( YUV C CXX ) # "C" is required even for C++ projects
-
CMAKE_MINIMUM_REQUIRED( VERSION 2.8 )
-
OPTION( TEST "Built unit tests" OFF )
-
-
SET ( ly_base_dir ${PROJECT_SOURCE_DIR} )
-
SET ( ly_src_dir ${ly_base_dir}/source )
-
SET ( ly_inc_dir ${ly_base_dir}/include )
-
SET ( ly_tst_dir ${ly_base_dir}/unit_test )
-
SET ( ly_lib_name yuv )
-
SET ( ly_lib_static ${ly_lib_name} )
-
SET ( ly_lib_shared ${ly_lib_name}_shared )
-
-
FILE ( GLOB_RECURSE ly_source_files ${ly_src_dir}/*.cc )
-
LIST ( SORT ly_source_files )
-
-
FILE ( GLOB_RECURSE ly_unittest_sources ${ly_tst_dir}/*.cc )
-
LIST ( SORT ly_unittest_sources )
-
-
INCLUDE_DIRECTORIES( BEFORE ${ly_inc_dir} )
-
-
# this creates the static library (.a)
-
ADD_LIBRARY ( ${ly_lib_static} STATIC ${ly_source_files} )
-
-
# this creates the shared library (.so)
-
ADD_LIBRARY ( ${ly_lib_shared} SHARED ${ly_source_files} )
-
SET_TARGET_PROPERTIES ( ${ly_lib_shared} PROPERTIES OUTPUT_NAME "${ly_lib_name}" )
-
SET_TARGET_PROPERTIES ( ${ly_lib_shared} PROPERTIES PREFIX "lib" )
-
-
# this creates the conversion tool
-
#ADD_EXECUTABLE ( yuvconvert ${ly_base_dir}/util/yuvconvert.cc )
-
#TARGET_LINK_LIBRARIES ( yuvconvert ${ly_lib_static} )
-
-
-
INCLUDE ( FindJPEG )
-
if (JPEG_FOUND)
-
include_directories( ${JPEG_INCLUDE_DIR} )
-
target_link_libraries( yuvconvert ${JPEG_LIBRARY} )
-
add_definitions( -DHAVE_JPEG )
-
endif()
-
-
if(TEST)
-
find_library(GTEST_LIBRARY gtest)
-
if(GTEST_LIBRARY STREQUAL "GTEST_LIBRARY-NOTFOUND")
-
set(GTEST_SRC_DIR /usr/src/gtest CACHE STRING "Location of gtest sources")
-
if(EXISTS ${GTEST_SRC_DIR}/src/gtest-all.cc)
-
message(STATUS "building gtest from sources in ${GTEST_SRC_DIR}")
-
set(gtest_sources ${GTEST_SRC_DIR}/src/gtest-all.cc)
-
add_library(gtest STATIC ${gtest_sources})
-
include_directories(${GTEST_SRC_DIR})
-
include_directories(${GTEST_SRC_DIR}/include)
-
set(GTEST_LIBRARY gtest)
-
else()
-
message(FATAL_ERROR "TEST is set but unable to find gtest library")
-
endif()
-
endif()
-
-
add_executable(libyuv_unittest ${ly_unittest_sources})
-
target_link_libraries(libyuv_unittest ${ly_lib_name} ${GTEST_LIBRARY})
-
find_library(PTHREAD_LIBRARY pthread)
-
if(NOT PTHREAD_LIBRARY STREQUAL "PTHREAD_LIBRARY-NOTFOUND")
-
target_link_libraries(libyuv_unittest pthread)
-
endif()
-
if (JPEG_FOUND)
-
target_link_libraries(libyuv_unittest ${JPEG_LIBRARY})
-
endif()
-
-
if(NACL AND NACL_LIBC STREQUAL "newlib")
-
target_link_libraries(libyuv_unittest glibc-compat)
-
endif()
-
-
find_library(GFLAGS_LIBRARY gflags)
-
if(NOT GFLAGS_LIBRARY STREQUAL "GFLAGS_LIBRARY-NOTFOUND")
-
target_link_libraries(libyuv_unittest gflags)
-
add_definitions(-DLIBYUV_USE_GFLAGS)
-
endif()
-
endif()
-
-
-
# install the conversion tool, .so, .a, and all the header files
-
#INSTALL ( PROGRAMS ${CMAKE_BINARY_DIR}/yuvconvert DESTINATION bin )
-
#INSTALL ( TARGETS ${ly_lib_static} DESTINATION lib )
-
#INSTALL ( TARGETS ${ly_lib_shared} LIBRARY DESTINATION lib RUNTIME DESTINATION bin )
-
INSTALL ( DIRECTORY ${PROJECT_SOURCE_DIR}/include/ DESTINATION include )
-
-
# create the .deb and .rpm packages using cpack
-
#INCLUDE ( CM_linux_packages.cmake )
-
YuvUtils.java
-
package com.example.libyuv;
-
-
-
public class YuvUtils {
-
-
static {
-
System.loadLibrary("native-lib");
-
}
-
-
// 创建一个YuvUtils,有三个常用的方法,第一个就是NV21转I420,然后旋转I420,最后一个是NV21转换I420并顺时针旋转90度,可以替换前两个方法
-
public static native void NV21ToI420(byte[] input, byte[] output, int width, int height);
-
public static native void RotateI420(byte[] input, byte[] output, int width, int height, int rotation);
-
public static native void NV21ToI420andRotate90Clockwise(byte[] input, byte[] output, int width, int height);
-
}
native-lib.cpp
-
#include <jni.h>
-
#include <string>
-
#include <cstring>
-
#include <android/log.h>
-
-
#include "libyuv/include/libyuv.h"
-
-
#define LOG_TAG "libyuv"
-
#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)
-
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
-
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
-
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
-
-
using namespace libyuv;
-
-
extern "C" JNIEXPORT jstring JNICALL
-
Java_com_example_libyuv_MainActivity_stringFromJNI(
-
JNIEnv *env,
-
jobject /* this */) {
-
std::string hello = "Hello from C++";
-
return env->NewStringUTF(hello.c_str());
-
}
-
-
extern "C"
-
JNIEXPORT void JNICALL
-
Java_com_example_libyuv_YuvUtils_NV21ToI420(JNIEnv *env, jobject instance, jbyteArray input_,
-
jbyteArray output_, jint in_width, jint in_height) {
-
jbyte *srcData = env->GetByteArrayElements(input_, NULL);
-
jbyte *dstData = env->GetByteArrayElements(output_, NULL);
-
-
NV21ToI420((const uint8_t *) srcData, in_width,
-
(uint8_t *) srcData + (in_width * in_height), in_width,
-
(uint8_t *) dstData, in_width,
-
(uint8_t *) dstData + (in_width * in_height), in_width / 2,
-
(uint8_t *) dstData + (in_width * in_height * 5 / 4), in_width / 2,
-
in_width, in_height);
-
env->ReleaseByteArrayElements(input_, srcData, 0);
-
env->ReleaseByteArrayElements(output_, dstData, 0);
-
}
-
-
extern "C"
-
JNIEXPORT void JNICALL
-
Java_com_example_libyuv_YuvUtils_RotateI420(JNIEnv *env, jobject type, jbyteArray input_,
-
jbyteArray output_, jint in_width, jint in_height,
-
jint rotation) {
-
jbyte *srcData = env->GetByteArrayElements(input_, NULL);
-
jbyte *dstData = env->GetByteArrayElements(output_, NULL);
-
-
-
RotationMode rotationMode = kRotate0;
-
switch (rotation) {
-
case 90:
-
rotationMode = kRotate90;
-
break;
-
case 180:
-
rotationMode = kRotate180;
-
break;
-
case 270:
-
rotationMode = kRotate270;
-
break;
-
}
-
I420Rotate((const uint8_t *) srcData, in_width,
-
(uint8_t *) srcData + (in_width * in_height), in_width / 2,
-
(uint8_t *) srcData + (in_width * in_height * 5 / 4), in_width / 2,
-
(uint8_t *) dstData, in_height,
-
(uint8_t *) dstData + (in_width * in_height), in_height / 2,
-
(uint8_t *) dstData + (in_width * in_height * 5 / 4), in_height / 2,
-
in_width, in_height,
-
rotationMode);
-
-
env->ReleaseByteArrayElements(input_, srcData, 0);
-
env->ReleaseByteArrayElements(output_, dstData, 0);
-
}
-
-
extern "C"
-
JNIEXPORT void JNICALL
-
Java_com_example_libyuv_YuvUtils_NV21ToI420andRotate90Clockwise(JNIEnv *env, jobject type,
-
jbyteArray input_,
-
jbyteArray output_,
-
jint in_width, jint in_height) {
-
jbyte *srcData = env->GetByteArrayElements(input_, NULL);
-
jbyte *dstData = env->GetByteArrayElements(output_, NULL);
-
jsize size = env->GetArrayLength(input_);
-
-
NV21ToI420((const uint8_t *) srcData, in_width,
-
(uint8_t *) srcData + (in_width * in_height), in_width,
-
(uint8_t *) dstData, in_width,
-
(uint8_t *) dstData + (in_width * in_height), in_width / 2,
-
(uint8_t *) dstData + (in_width * in_height * 5 / 4), in_width / 2,
-
in_width, in_height);
-
-
I420Rotate((const uint8_t *) dstData, in_width,
-
(uint8_t *) dstData + (in_width * in_height), in_width / 2,
-
(uint8_t *) dstData + (in_width * in_height * 5 / 4), in_width / 2,
-
(uint8_t *) srcData, in_height,
-
(uint8_t *) srcData + (in_width * in_height), in_height / 2,
-
(uint8_t *) srcData + (in_width * in_height * 5 / 4), in_height / 2,
-
in_width, in_height,
-
kRotate90);
-
memcpy(dstData, srcData, size);
-
-
// fixme can't work
-
// ConvertToI420((const uint8_t *) srcData, size,
-
// (uint8_t *)dstData, in_width,
-
// (uint8_t *)dstData + (in_width * in_height), in_width / 2,
-
// (uint8_t *)dstData + (in_width * in_height * 5 / 4), in_width / 2,
-
// 0, 0,
-
// in_width, in_height,
-
// in_width, in_height,
-
// kRotate90,
-
// FOURCC_NV21);
-
//
-
// fixme can't work
-
// NV12ToI420Rotate((const uint8_t *) srcData, in_width,
-
// (uint8_t *) srcData + (in_width * in_height), in_width,
-
// (uint8_t *)dstData, in_width,
-
// (uint8_t *)dstData + (in_width * in_height * 5 / 4), in_width / 2,
-
// (uint8_t *)dstData + (in_width * in_height), in_width / 2,
-
// in_width, in_height,
-
// kRotate90);
-
-
env->ReleaseByteArrayElements(input_, srcData, 0);
-
env->ReleaseByteArrayElements(output_, dstData, 0);
-
}
build 生成的 so:
不过这个方法有个问题,就是没有把 neon 优化的代码编译进来,需要自己手动修改 CMakeList.txt,或者使用 .mk 的方式(参考0, 参考1,参考2),亦或者通过 libyuv 的官网的方法进行编译。
根据 CMakeList 的方式(启用了 neon )编译出来的大小为 200 多 KB (只测试了 Debug)
根据 mk 的方式编译出来的 libyuv.so 的大小也为 200多 KB (只测试了 Debug)
文章来源: panda1234lee.blog.csdn.net,作者:panda1234lee,版权归原作者所有,如需转载,请联系作者。
原文链接:panda1234lee.blog.csdn.net/article/details/89216200
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)