Android 简单引入x264

Posted 胡刚2021

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 简单引入x264相关的知识,希望对你有一定的参考价值。

引入x264前,需要先编译x264:编译x264
gitee源码链接:android 引入 libx264.so

1.新建一个 native 工程
然后将编译好的armeabi-v7a整个文件夹拷贝到 app\\cpp目录下面


2.配置CMakeLists.txt

cmake_minimum_required(VERSION 3.10.2)

project("myx264test")
add_library(
        myx264test
        SHARED
        native-lib.cpp)
find_library(
        log-lib
        log)
        
#最重要的是下面的几行
add_library(
        x264
        SHARED
        IMPORTED)
set_target_properties(x264 PROPERTIES IMPORTED_LOCATION $CMAKE_SOURCE_DIR/$android_ABI/lib/libx264.so)
include_directories( $CMAKE_SOURCE_DIR/$ANDROID_ABI/include)
target_link_libraries(
        myx264test
        x264
        $log-lib)

其中,以动态库的形式引入 libx264.so 使用了以下代码:

add_library(
        x264
        SHARED
        IMPORTED)
set_target_properties(x264 PROPERTIES IMPORTED_LOCATION $CMAKE_SOURCE_DIR/$ANDROID_ABI/lib/libx264.so)
include_directories( $CMAKE_SOURCE_DIR/$ANDROID_ABI/include)
target_link_libraries(
        myx264test
        x264
        $log-lib)

3.配置build.gradle

externalNativeBuild 
    cmake 
        cppFlags ""
        abiFilters 'armeabi-v7a'
    

ndk
    abiFilters 'armeabi-v7a'


4.编写native-lib.cpp

#include <jni.h>
#include <string>
#include "x264.h"

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_myx264test_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) 
    std::string hello = "Hello from C++";
    x264_nal_t *pp_nals = NULL;
    x264_picture_t *pic_in = NULL;
    return env->NewStringUTF(hello.c_str());


最后,编译运行即可

以上是关于Android 简单引入x264的主要内容,如果未能解决你的问题,请参考以下文章