Original Address:http://blog.csdn.net/jiwangkailai02/article/details/17015451 support Android4.0 The use of the following WEBPCategory: Android related 2013-11-30 16:53 1658 people read reviews (2) favorite reports Androidwebp
Recent projects need to use the WEBP, the main purpose is to reduce network traffic (the same picture, the WEBP format can be less than JPG format of about 30%). But android in more than 4.0 bitmapfactory only support WEBP, today's operation is to make Android4.0 the following version can also be WEBP decompression.
First, you need to build an environment for NDK development
1. Download and install Cygwin
cygwin:http://www.cygwin.com/
Follow the wizard all the way down, (using the default mirror path http://www.mirrors.163.com/, this looks relatively fast).
To arrive at this step:
Select Devel, and tap to open. Select the 5 components we have to install: binutils, GCC (with core and g++), GCC-MINGW (including core and g++), Gdb,make. (Sometimes you need more components, you need to install them)
When you are finished selecting, click Next until the installation is complete.
Run Cygwin, enter make-v and gcc-v if the version is displayed, the installation is successful.
2. Download and configure the NDK
Ndk:http://developer.android.com/tools/sdk/ndk/index.html
The NDK installation is simple, and after the download is complete, unzip the file to the specified location.
NDK Environment Configuration:
Modify the. bash_profile file under Cygwin directory/home/<username> by adding the following code at the end of the file:
android_ndk_root=/cygdrive/the path to the NDK file/* (for example:/cygdrive/d/android-ndk-r9b) */
Export Android_ndk_root
At this point, the preparation has been completed.
Second, we need to download and edit the WEBP source file
We use the latest 0.3.1 version of the WEBP source file for: http://code.google.com/p/webp/downloads/detail?name=libwebp-0.3.1.tar.gz&can=2 &q=
After the download is complete, extract the Libwebp.jar files and libwebp_java_wrap.c files from the android.mk file, the SRC folder, and the Swig file. And move the libwebp_java_wrap.c file to the SRC folder.
Create a new folder named JNI under the project directory. Then move the android.mk file, src folder to the Jni folder. Finally, the Libwebp.jar is introduced into the project.
Edit the Android.mk folder, in the include $ (clear_vars)
Local_src_files: = \ add: src/libwebp_java_wrap.c \
And will include $ (build_static_library) that is include $ (build_shared_library)
As follows:
(The difference between build_static_library and Build-shared_library reference: http://stackoverflow.com/questions/2649334/ Difference-between-static-and-shared-libraries)
Then create the Application.mk file under the Jni folder, and edit the following:
# The ARMV7 is significanly faster due to the use of the hardware FPU
App_abi: = Armeabi armeabi-v7a
App_platform: = android-8
Where App_platform is set to the supported SDK minimum version.
After saving, we start Cygwin, and then through the CD command into our project folder, execute instructions: $NDK/ndk-build.
Wait until the compilation is complete:
So the library has been generated (viewing the Libs folder will find the corresponding. so file. At the same time you will find that there is an obj folder in the project directory, and it is not small. Rest assured, it just generates an intermediate file of so files that will not be packaged into the APK).
The next task is to use JNI to invoke the so library for the development of the application layer.
The development of the application layer is generally as follows:
1. Load the so library.
[Java]View Plaincopy < param name= "wmode" value= "Transparent" >
- static {
- System.loadlibrary ("WEBP"); The definition of local_module:= * * in LoadLibrary and ANDROID.MK is related, we define WEBP in Android.mk, here we write WEBP.
- }
2. Declare the method corresponding to native.
The native method is:
[CPP]View Plaincopy < param name= "wmode" value= "Transparent" >
- Swigexport jint jnicall java_com_google_webp_libwebpjni_webpgetdecoderversion (jnienv *jenv,
- Jclass jcls) {
- Jint jresult = 0;
- int result;
- (void) jenv;
- (void) jcls;
- result = (int) webpgetdecoderversion ();
- Jresult = (jint) result;
- return jresult;
- }
Native method naming rules are: Java_ Package Name _ Class Name _ Application layer method name. Where the points in the package name are underlined instead.
The method of life in the application layer is:
[Java]View Plaincopy
- Public static final native int webpgetdecoderversion ();
The application layer is declared only, not defined, but is prefixed with the native keyword.
3. Use the method that the application layer declares.
Let's follow the above method to use the so library we just generated at the application layer
Remember there is a Libwebp.jar file No, this jar has put the application layer declaration of the native method done well, and to help us encapsulate a layer, we just call its method is OK, but we still have to load so library, because it does not help us to implement.
We write two methods:
[Java]View Plaincopy
- static {
- System.loadlibrary ("WEBP");
- }
- Private Bitmap Webptobitmap (byte[] encoded) {
- int[] width = new int[] { 0};
- int[] height = new int[] { 0};
- byte[] decoded = libwebp. WEBPDECODEARGB (encoded, encoded.length, width,
- height);
- int[] pixels = new int[decoded.length/ 4];
- Bytebuffer.wrap (decoded). Asintbuffer (). get (pixels);
- return Bitmap.createbitmap (pixels, width[0], height[0],
- Bitmap.Config.ARGB_8888);
- }
- Public Static boolean ISWEBP (byte[] data) {
- return Data! = null && data.length > && data[0] = = ' R '
- && data[1] = = ' I ' && data[2] = = ' F ' && data[3] = = ' F '
- && data[8] = = ' W ' && data[9] = = ' E ' && data[] = = ' B '
- && data[] = = ' P ';
- }
Based on these two methods, we can decompress the WEBP image.
Here is a demo, you can refer to the following:
WEBP Unzip Demo
Resources:
Android Official Introduction to NDK development: http://developer.android.com/tools/sdk/ndk/index.html
StackOverflow the previous person's experience Introduction: http://stackoverflow.com/questions/7032695/webp-for-android
Reprint-Support Android4.0 use of the following WEBP