android之jni开发----引入ffmpeg调用(1)

1.新建一个空项目(环境 android studio4.0 ndk 20.0.+)

android之jni开发----引入ffmpeg调用(1)_第1张图片

2.点击File->project structure... 进行ndk路径的配置

android之jni开发----引入ffmpeg调用(1)_第2张图片

3.在app->src->main下面新建一个jniLibs目录

android之jni开发----引入ffmpeg调用(1)_第3张图片

4.把ffpmeg编译出来的so库导入进去

android之jni开发----引入ffmpeg调用(1)_第4张图片

5.在app->src->main下面新建一个cpp文件夹,在文件夹下面新建CMakeLists.txt

android之jni开发----引入ffmpeg调用(1)_第5张图片

6.把ffmpeg的include放到cpp文件夹下

android之jni开发----引入ffmpeg调用(1)_第6张图片

7.接下来先编辑下CMakeLists.txt

cmake_minimum_required(VERSION 3.10.2)

#设置FFmpeg头文件的路径,共享库,include里面h文件调用其他h文件用到的
include_directories(
        include#因为和CMakeLists.txt在同一级,所以直接写include
)


add_library(native-lib
        SHARED
        native-lib.cpp)

find_library(
        log-lib
        log)

#1.定义一个变量avcodec
add_library(
        avcodec
        SHARED
        IMPORTED
)
#给avcodec这个变量赋值
set_target_properties(avcodec PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libavcodec-58.so)

#2.
add_library(
        avdevice
        SHARED
        IMPORTED
)
set_target_properties(avdevice PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libavdevice-58.so)

#3.
add_library(
        avfilter
        SHARED
        IMPORTED
)
set_target_properties(avfilter PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libavfilter-7.so)

#4.
add_library(
        avformat
        SHARED
        IMPORTED
)
set_target_properties(avformat PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libavformat-58.so)

#5.
add_library(
        avutil
        SHARED
        IMPORTED
)
set_target_properties(avutil PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libavutil-56.so)

#6.
add_library(
        postproc
        SHARED
        IMPORTED
)
set_target_properties(postproc PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libpostproc-55.so)

#7.
add_library(
        swresample
        SHARED
        IMPORTED
)
set_target_properties(swresample PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libswresample-3.so)

#8.
add_library(
        swscale
        SHARED
        IMPORTED
)
set_target_properties(swscale PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libswscale-5.so)

#将avcodec混合编译到native-lib中
target_link_libraries(
        native-lib
        ${log-lib}
        avcodec#1
        avdevice#2
        avfilter#3
        avformat#4
        avutil#5
        postproc#6
        swresample#7
        swscale#8
)

8.配置build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.hh.ffmpegdemo2"
        minSdkVersion 17
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11 -frtti -fexceptions"
                abiFilters 'armeabi-v7a' //指定cpu的架构
            }
        }

        ndk {
            abiFilters  "armeabi-v7a"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }



    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"  //指定CMakeLists.txt的路径,这里表示是在app目录下
            version "3.10.2"
        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

}

 

android之jni开发----引入ffmpeg调用(1)_第7张图片

9.在app->src->main下面新建一个native-lib.cpp(这个文件和CMakeLists.txt那个要一致)的c++文件,然后点击build->Refresh Linked C++ Projects

android之jni开发----引入ffmpeg调用(1)_第8张图片

10.native-lib.cpp添加测试代码,其实com_hh_ffmpegdemo2_JNIUtils是你要要调用的类的路径和类名

#include 
#include 

extern "C" {
#include "include/libavcodec/avcodec.h"
}

extern "C" JNIEXPORT jstring JNICALL
Java_com_hh_ffmpegdemo2_JNIUtils_stringFromJNI(
        JNIEnv *env,
        jobject) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(avcodec_configuration());
}

android之jni开发----引入ffmpeg调用(1)_第9张图片

11.接下来进行编译,编译完之后在下图路径得到libnative-lib.so

android之jni开发----引入ffmpeg调用(1)_第10张图片

android之jni开发----引入ffmpeg调用(1)_第11张图片

12.把so库放在我们之前建好的jniLibs目录下,在JNIUtils.java里面引入

android之jni开发----引入ffmpeg调用(1)_第12张图片

android之jni开发----引入ffmpeg调用(1)_第13张图片

13.调用方法运行

android之jni开发----引入ffmpeg调用(1)_第14张图片

 

你可能感兴趣的:(ffmpeg,for,android,ndk,jni)