Android Studio 下通过 CMake 配置编译 libyuv 库

举报
ShaderJoy 发表于 2021/12/31 22:20:41 2021/12/31
【摘要】 参考链接 创建 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


  
  1. # For more information about using CMake with Android Studio, read the
  2. # documentation: https://d.android.com/studio/projects/add-native-code.html
  3. # Sets the minimum version of CMake required to build the native library.
  4. cmake_minimum_required(VERSION 3.4.1)
  5. include_directories(/home/lingyun/Projects/AndroidStudioProjects/libyuv/app/src/main/cpp/libyuv/include) # 重点,根据自己的路径进行修改
  6. add_subdirectory(/home/lingyun/Projects/AndroidStudioProjects/libyuv/app/src/main/cpp/libyuv) # 重点
  7. # Creates and names a library, sets it as either STATIC
  8. # or SHARED, and provides the relative paths to its source code.
  9. # You can define multiple libraries, and CMake builds them for you.
  10. # Gradle automatically packages shared libraries with your APK.
  11. add_library( # Sets the name of the library.
  12. native-lib
  13. # Sets the library as a shared library.
  14. SHARED
  15. # Provides a relative path to your source file(s).
  16. native-lib.cpp)
  17. # Searches for a specified prebuilt library and stores the path as a
  18. # variable. Because CMake includes system libraries in the search path by
  19. # default, you only need to specify the name of the public NDK library
  20. # you want to add. CMake verifies that the library exists before
  21. # completing its build.
  22. find_library( # Sets the name of the path variable.
  23. log-lib
  24. # Specifies the name of the NDK library that
  25. # you want CMake to locate.
  26. log)
  27. # Specifies libraries CMake should link to your target library. You
  28. # can link multiple libraries, such as libraries you define in this
  29. # build script, prebuilt third-party libraries, or system libraries.
  30. target_link_libraries( # Specifies the target library.
  31. native-lib
  32. # Links the target library to the log library
  33. # included in the NDK.
  34. ${log-lib}
  35. yuv) # 重点

然后再修改 libyuv 库的 CMakeList.txt (去掉一些编译生成的文件,也可以不改,按自己需要编辑)


  
  1. # CMakeLists for libyuv
  2. # Originally created for "roxlu build system" to compile libyuv on windows
  3. # Run with -DTEST=ON to build unit tests
  4. PROJECT ( YUV C CXX ) # "C" is required even for C++ projects
  5. CMAKE_MINIMUM_REQUIRED( VERSION 2.8 )
  6. OPTION( TEST "Built unit tests" OFF )
  7. SET ( ly_base_dir ${PROJECT_SOURCE_DIR} )
  8. SET ( ly_src_dir ${ly_base_dir}/source )
  9. SET ( ly_inc_dir ${ly_base_dir}/include )
  10. SET ( ly_tst_dir ${ly_base_dir}/unit_test )
  11. SET ( ly_lib_name yuv )
  12. SET ( ly_lib_static ${ly_lib_name} )
  13. SET ( ly_lib_shared ${ly_lib_name}_shared )
  14. FILE ( GLOB_RECURSE ly_source_files ${ly_src_dir}/*.cc )
  15. LIST ( SORT ly_source_files )
  16. FILE ( GLOB_RECURSE ly_unittest_sources ${ly_tst_dir}/*.cc )
  17. LIST ( SORT ly_unittest_sources )
  18. INCLUDE_DIRECTORIES( BEFORE ${ly_inc_dir} )
  19. # this creates the static library (.a)
  20. ADD_LIBRARY ( ${ly_lib_static} STATIC ${ly_source_files} )
  21. # this creates the shared library (.so)
  22. ADD_LIBRARY ( ${ly_lib_shared} SHARED ${ly_source_files} )
  23. SET_TARGET_PROPERTIES ( ${ly_lib_shared} PROPERTIES OUTPUT_NAME "${ly_lib_name}" )
  24. SET_TARGET_PROPERTIES ( ${ly_lib_shared} PROPERTIES PREFIX "lib" )
  25. # this creates the conversion tool
  26. #ADD_EXECUTABLE ( yuvconvert ${ly_base_dir}/util/yuvconvert.cc )
  27. #TARGET_LINK_LIBRARIES ( yuvconvert ${ly_lib_static} )
  28. INCLUDE ( FindJPEG )
  29. if (JPEG_FOUND)
  30. include_directories( ${JPEG_INCLUDE_DIR} )
  31. target_link_libraries( yuvconvert ${JPEG_LIBRARY} )
  32. add_definitions( -DHAVE_JPEG )
  33. endif()
  34. if(TEST)
  35. find_library(GTEST_LIBRARY gtest)
  36. if(GTEST_LIBRARY STREQUAL "GTEST_LIBRARY-NOTFOUND")
  37. set(GTEST_SRC_DIR /usr/src/gtest CACHE STRING "Location of gtest sources")
  38. if(EXISTS ${GTEST_SRC_DIR}/src/gtest-all.cc)
  39. message(STATUS "building gtest from sources in ${GTEST_SRC_DIR}")
  40. set(gtest_sources ${GTEST_SRC_DIR}/src/gtest-all.cc)
  41. add_library(gtest STATIC ${gtest_sources})
  42. include_directories(${GTEST_SRC_DIR})
  43. include_directories(${GTEST_SRC_DIR}/include)
  44. set(GTEST_LIBRARY gtest)
  45. else()
  46. message(FATAL_ERROR "TEST is set but unable to find gtest library")
  47. endif()
  48. endif()
  49. add_executable(libyuv_unittest ${ly_unittest_sources})
  50. target_link_libraries(libyuv_unittest ${ly_lib_name} ${GTEST_LIBRARY})
  51. find_library(PTHREAD_LIBRARY pthread)
  52. if(NOT PTHREAD_LIBRARY STREQUAL "PTHREAD_LIBRARY-NOTFOUND")
  53. target_link_libraries(libyuv_unittest pthread)
  54. endif()
  55. if (JPEG_FOUND)
  56. target_link_libraries(libyuv_unittest ${JPEG_LIBRARY})
  57. endif()
  58. if(NACL AND NACL_LIBC STREQUAL "newlib")
  59. target_link_libraries(libyuv_unittest glibc-compat)
  60. endif()
  61. find_library(GFLAGS_LIBRARY gflags)
  62. if(NOT GFLAGS_LIBRARY STREQUAL "GFLAGS_LIBRARY-NOTFOUND")
  63. target_link_libraries(libyuv_unittest gflags)
  64. add_definitions(-DLIBYUV_USE_GFLAGS)
  65. endif()
  66. endif()
  67. # install the conversion tool, .so, .a, and all the header files
  68. #INSTALL ( PROGRAMS ${CMAKE_BINARY_DIR}/yuvconvert DESTINATION bin )
  69. #INSTALL ( TARGETS ${ly_lib_static} DESTINATION lib )
  70. #INSTALL ( TARGETS ${ly_lib_shared} LIBRARY DESTINATION lib RUNTIME DESTINATION bin )
  71. INSTALL ( DIRECTORY ${PROJECT_SOURCE_DIR}/include/ DESTINATION include )
  72. # create the .deb and .rpm packages using cpack
  73. #INCLUDE ( CM_linux_packages.cmake )

YuvUtils.java


  
  1. package com.example.libyuv;
  2. public class YuvUtils {
  3. static {
  4. System.loadLibrary("native-lib");
  5. }
  6. // 创建一个YuvUtils,有三个常用的方法,第一个就是NV21转I420,然后旋转I420,最后一个是NV21转换I420并顺时针旋转90度,可以替换前两个方法
  7. public static native void NV21ToI420(byte[] input, byte[] output, int width, int height);
  8. public static native void RotateI420(byte[] input, byte[] output, int width, int height, int rotation);
  9. public static native void NV21ToI420andRotate90Clockwise(byte[] input, byte[] output, int width, int height);
  10. }

native-lib.cpp


  
  1. #include <jni.h>
  2. #include <string>
  3. #include <cstring>
  4. #include <android/log.h>
  5. #include "libyuv/include/libyuv.h"
  6. #define LOG_TAG "libyuv"
  7. #define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)
  8. #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
  9. #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
  10. #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
  11. using namespace libyuv;
  12. extern "C" JNIEXPORT jstring JNICALL
  13. Java_com_example_libyuv_MainActivity_stringFromJNI(
  14. JNIEnv *env,
  15. jobject /* this */) {
  16. std::string hello = "Hello from C++";
  17. return env->NewStringUTF(hello.c_str());
  18. }
  19. extern "C"
  20. JNIEXPORT void JNICALL
  21. Java_com_example_libyuv_YuvUtils_NV21ToI420(JNIEnv *env, jobject instance, jbyteArray input_,
  22. jbyteArray output_, jint in_width, jint in_height) {
  23. jbyte *srcData = env->GetByteArrayElements(input_, NULL);
  24. jbyte *dstData = env->GetByteArrayElements(output_, NULL);
  25. NV21ToI420((const uint8_t *) srcData, in_width,
  26. (uint8_t *) srcData + (in_width * in_height), in_width,
  27. (uint8_t *) dstData, in_width,
  28. (uint8_t *) dstData + (in_width * in_height), in_width / 2,
  29. (uint8_t *) dstData + (in_width * in_height * 5 / 4), in_width / 2,
  30. in_width, in_height);
  31. env->ReleaseByteArrayElements(input_, srcData, 0);
  32. env->ReleaseByteArrayElements(output_, dstData, 0);
  33. }
  34. extern "C"
  35. JNIEXPORT void JNICALL
  36. Java_com_example_libyuv_YuvUtils_RotateI420(JNIEnv *env, jobject type, jbyteArray input_,
  37. jbyteArray output_, jint in_width, jint in_height,
  38. jint rotation) {
  39. jbyte *srcData = env->GetByteArrayElements(input_, NULL);
  40. jbyte *dstData = env->GetByteArrayElements(output_, NULL);
  41. RotationMode rotationMode = kRotate0;
  42. switch (rotation) {
  43. case 90:
  44. rotationMode = kRotate90;
  45. break;
  46. case 180:
  47. rotationMode = kRotate180;
  48. break;
  49. case 270:
  50. rotationMode = kRotate270;
  51. break;
  52. }
  53. I420Rotate((const uint8_t *) srcData, in_width,
  54. (uint8_t *) srcData + (in_width * in_height), in_width / 2,
  55. (uint8_t *) srcData + (in_width * in_height * 5 / 4), in_width / 2,
  56. (uint8_t *) dstData, in_height,
  57. (uint8_t *) dstData + (in_width * in_height), in_height / 2,
  58. (uint8_t *) dstData + (in_width * in_height * 5 / 4), in_height / 2,
  59. in_width, in_height,
  60. rotationMode);
  61. env->ReleaseByteArrayElements(input_, srcData, 0);
  62. env->ReleaseByteArrayElements(output_, dstData, 0);
  63. }
  64. extern "C"
  65. JNIEXPORT void JNICALL
  66. Java_com_example_libyuv_YuvUtils_NV21ToI420andRotate90Clockwise(JNIEnv *env, jobject type,
  67. jbyteArray input_,
  68. jbyteArray output_,
  69. jint in_width, jint in_height) {
  70. jbyte *srcData = env->GetByteArrayElements(input_, NULL);
  71. jbyte *dstData = env->GetByteArrayElements(output_, NULL);
  72. jsize size = env->GetArrayLength(input_);
  73. NV21ToI420((const uint8_t *) srcData, in_width,
  74. (uint8_t *) srcData + (in_width * in_height), in_width,
  75. (uint8_t *) dstData, in_width,
  76. (uint8_t *) dstData + (in_width * in_height), in_width / 2,
  77. (uint8_t *) dstData + (in_width * in_height * 5 / 4), in_width / 2,
  78. in_width, in_height);
  79. I420Rotate((const uint8_t *) dstData, in_width,
  80. (uint8_t *) dstData + (in_width * in_height), in_width / 2,
  81. (uint8_t *) dstData + (in_width * in_height * 5 / 4), in_width / 2,
  82. (uint8_t *) srcData, in_height,
  83. (uint8_t *) srcData + (in_width * in_height), in_height / 2,
  84. (uint8_t *) srcData + (in_width * in_height * 5 / 4), in_height / 2,
  85. in_width, in_height,
  86. kRotate90);
  87. memcpy(dstData, srcData, size);
  88. // fixme can't work
  89. // ConvertToI420((const uint8_t *) srcData, size,
  90. // (uint8_t *)dstData, in_width,
  91. // (uint8_t *)dstData + (in_width * in_height), in_width / 2,
  92. // (uint8_t *)dstData + (in_width * in_height * 5 / 4), in_width / 2,
  93. // 0, 0,
  94. // in_width, in_height,
  95. // in_width, in_height,
  96. // kRotate90,
  97. // FOURCC_NV21);
  98. //
  99. // fixme can't work
  100. // NV12ToI420Rotate((const uint8_t *) srcData, in_width,
  101. // (uint8_t *) srcData + (in_width * in_height), in_width,
  102. // (uint8_t *)dstData, in_width,
  103. // (uint8_t *)dstData + (in_width * in_height * 5 / 4), in_width / 2,
  104. // (uint8_t *)dstData + (in_width * in_height), in_width / 2,
  105. // in_width, in_height,
  106. // kRotate90);
  107. env->ReleaseByteArrayElements(input_, srcData, 0);
  108. env->ReleaseByteArrayElements(output_, dstData, 0);
  109. }

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

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。