赞
踩
可以从fftw3的官网下载,另外这里提供了一个下载链接
https://download.csdn.net/download/cyz_2014/87863156
将ffw3文件夹复制到app->src->main->cpp路径中
fftw3中的CMakeLists.txt无需修改,只需要修改外层cpp中的CMakeLists.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.10.2)
-
- # Declares and names the project.
-
- option (ENABLE_FLOAT "single-precision" ON)
-
- SET (ENABLE_FLOAT ON)
-
- project("myapplication")
-
- # 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_subdirectory(fftw3)
-
- 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} )
-
- target_link_libraries(native-lib fftw3f)
上面的配置文件只添加了以下4条语句:
option (ENABLE_FLOAT "single-precision" ON)
SET (ENABLE_FLOAT ON)
add_subdirectory(fftw3)
target_link_libraries(native-lib fftw3f)
前两条语句是设置fftw3的编译选项,将其编译为单精度浮点库,生成的库的名称是libfftw3f.so
后两条语句是用于编译ffw3,并将其与native-lib链接。
编译以后在app->build->intermediates->merged_native_libs->debug->out->lib中有生成的对应的so库
在MainActivity 类中加载生成的fftw3f:
static { System.loadLibrary("fftw3f"); System.loadLibrary("native-lib"); }
编写native函数
- #include <jni.h>
- #include <string>
-
- #include <android/log.h>
- #include <math.h>
- #include "fftw3/api/fftw3.h"
-
- #define TAG "Jonathan"
-
- #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__);
-
- //以下是native函数部分
- jfloat a[8] = {1, 0, 2, 0, 3, 0, 4, 0};
- fftwf_complex *fftwa = reinterpret_cast<fftwf_complex *>((fftwf_complex *) a);
-
- fftwf_plan p;
- p = fftwf_plan_dft_1d(4, fftwa, fftwa, FFTW_FORWARD, FFTW_ESTIMATE);
- fftwf_execute(p);
- fftwf_destroy_plan(p);
- LOGD("fftwa: %f + %fi, %f + %fi, %f + %fi, %f + %fi,\n", fftwa[0][0], fftwa[0][1],
- fftwa[1][0], fftwa[1][1], fftwa[2][0], fftwa[2][1], fftwa[3][0], fftwa[3][1]);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。