Reprint-Support Android4.0 use of the following WEBP

Source: Internet
Author: User
Tags webp

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" >
    1. static {
    2. 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.
    3. }
2. Declare the method corresponding to native.

The native method is:

[CPP]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. Swigexport jint jnicall java_com_google_webp_libwebpjni_webpgetdecoderversion (jnienv *jenv,
    2. Jclass jcls) {
    3. Jint jresult = 0;
    4. int result;
    5. (void) jenv;
    6. (void) jcls;
    7. result = (int) webpgetdecoderversion ();
    8. Jresult = (jint) result;
    9. return jresult;
    10. }
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
    1. 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
  1. static {
  2. System.loadlibrary ("WEBP");
  3. }
  4. Private Bitmap Webptobitmap (byte[] encoded) {
  5. int[] width = new int[] { 0};
  6. int[] height = new int[] { 0};
  7. byte[] decoded = libwebp. WEBPDECODEARGB (encoded, encoded.length, width,
  8. height);
  9. int[] pixels = new int[decoded.length/ 4];
  10. Bytebuffer.wrap (decoded). Asintbuffer (). get (pixels);
  11. return Bitmap.createbitmap (pixels, width[0], height[0],
  12. Bitmap.Config.ARGB_8888);
  13. }
  14. Public Static boolean ISWEBP (byte[] data) {
  15. return Data! = null && data.length > && data[0] = = ' R '
  16. && data[1] = = ' I ' && data[2] = = ' F ' && data[3] = = ' F '
  17. && data[8] = = ' W ' && data[9] = = ' E ' && data[] = = ' B '
  18. && data[] = = ' P ';
  19. }
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.