最近一直在研究libjpeg-turbo运用到android平台,发现libjpeg-turbo库的编译是真的痛苦,用vs出错,不知道是什么原因,用cmake软件也老是出错,不知原因,苦啊(我是window系统,liniu没装!苦)!大量找方法,终于找的了一遍博文,代码地址。接下来,我说一下我的操作过程吧!
首先下载libjpeg-turbo
地址如下 https://github.com/libjpeg-turbo/libjpeg-turbo
编译libjpeg
这是一个难点啊,痛苦的起点啊,方法确实多,但是呢?刚接触这个确实麻烦。但是上面我提到的篇博文观点就比较新颖,他直接用Intellij idea编译,是不是感觉,不会吧,有这么简单。确实就是这么简单。步骤如下。(这里不就不说基础环境配置啊)
步骤1:安装ndk,这个简单,SDK工具里面的ndk,不细说。
步骤2:安装cmake,建议选个低版本的。为什么呢,网上说,低版本编译不出错。当然还是在SDK工具里面安装。
步骤3:安装LLDB,选3.0版本吧,我的是3.0,当然你也可以尝试其他版本。还是SDK工具安装。
步骤4:环境已经配好了,编译开始了,建立一个ndk项目,也就是可以jni调用的,我这里不说步骤吧。
步骤5:把libjpe-turbo全部,对,就是全部复制进jni目录下(有些是cpp),修改cMakeList.txt文件,把libjpeg-turbo包含进来,添加一条语句如下:
add_subdirectory(libjpeg-turbo-2.0.2)
之后,直接build。这已经成功生成各个版本的libturbojpeg.a(静态文件)。
步骤6:把各个版本的静态文件作为静态库供其他ndk项目调用。一般是放在ndk项目的libs中,看个人爱好。我这里就不重新新建项目了,把上一步生成的静态库保存起来,之后把add_subdirectory(libjpeg-turbo-2.0.2)注释。我最终的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)
# add_subdirectory(libjpeg-turbo-2.0.2)
include_directories(include)
# 设置输入路径为项目下
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../../../../jniLibs/${CMAKE_ANDROID_ARCH_ABI})
# 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
jpegcompress.cpp
libjpeg_jni/turbojpeg-jni.c )
# 添加静态导入
add_library(libjpeg STATIC IMPORTED)
set_target_properties(libjpeg
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/libs/${CMAKE_ANDROID_ARCH_ABI}/libturbojpeg.a)
# 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
jnigraphics
libjpeg
# Links the target library to the log library
# included in the NDK.
${log-lib} )
如果没有接触cmake的,确实比较难读懂以下代码,大部分是新建项目的时候自动生成的,你可以每条语句百度,理解其意义。
步骤7:在java中编写native方法。且生成相应的头文件。这里需要学习javah的用法,简单cmd命令窗口中输入javah -h,直接教你怎么用。
步骤8:用c++写jpeg压缩代码,这个我确实不太懂里面原理,我也是模仿别人的。但我知道怎么一个过程。调用c语言里面的方法,首先导入头文件,所以啊,也就是把你需要的头文件放在jni(cpp)中,用c++写压缩逻辑。
步骤9:最后一步,就是整理CmakeList.txt文件,思路就是生成一个动态库so工native方法的类调用。这逻辑是不是顺畅了。
最后我附上源码https://github.com/HUIZHENG940722/libjpegturbo,好晚了,需要睡觉了,别说我草草结束啊!有不懂的qq1797801363