最近一直在研究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