Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebP for Android [closed]

Tags:

Are there any examples on how to use WebP for Android? Im trying to get a list of webp images and show them in a listview with an imageview.

I know theres a libwebp api and I have integrated it into my Android project using the NDK, but how do I excatly use the api to take my binary webp and show it in an imageview?

Any help would be amazing!

like image 234
Faisal Abid Avatar asked Aug 11 '11 20:08

Faisal Abid


People also ask

Is WebP supported by Android?

WebM video format on Android Browser is fully supported on 97-103, partially supported on 2.3-4, and not supported on 2.2-2.1 Android Browser versions.

How do I open a WebP file on Android?

If you are an Android, iOS, or Chrome OS user, you can open WEBP files with Google Photos. You can also upload WebP images to Google Photos to view them with the web application.

Is WebP supported by all browsers 2022?

BROWSER SUPPORT FOR WebP Image FormatChrome 4 to 8 does not support for WebP Image Format. Chrome 9 to 22 partially supports WebP Image Format. Partial support in older Chrome, Opera and Android refers to browsers not supporting lossless and alpha versions of WebP. Chrome 23 to 67 supports for WebP Image Format.

Why is WebP not used?

It has several drawbacks and they're pretty significant. One of the drawbacks is that animation saved as WebP requires more processing power during playback compared to a good old GIF animation. Practically, it may take several more years until WebP becomes the successor of JPEGs and dominate the internet.


2 Answers

Use libwebp with NDK. libwebp-0.1.3 already comes with Android.mk file (outdated and with syntax errors, but still). It also got generated JNI bindings in /swig/ directory.

Here's how I got it working:

  1. Download NDK, put it in system PATH.
  2. download libwebp-0.1.3.tar.gz, place it in your_project_dir/jni
  3. Replace Android.mk with the one below.
  4. Create jni/src/libwebp_java_wrap.c with content from below.
  5. create jni/Application.mk, with content from below.
  6. run ndk-build from project directory. This generates .so files in /libs/. You can inspect them with nm -D libs/armeabi/libwebp.so. In the list you'll see both the native library functions (like WebPDecodeRGB) and their JNI counterparts (like Java_com_google_webp_libwebpJNI_WebPDecodeRGB)
  7. Add /jni/swig/libwebp.jar to build path of your Android project
  8. See below for example how to use it in Java code

Here's content for Android.mk. Changed from original: removed encoder bits as I don't need these, added libwebp_java_wrap.c, changed include $(BUILD_STATIC_LIBRARY) to include $(BUILD_SHARED_LIBRARY).

LOCAL_PATH:= $(call my-dir)  include $(CLEAR_VARS) LOCAL_SRC_FILES := \     src/dec/alpha.c \     src/dec/frame.c \     src/dec/idec.c \     src/dec/layer.c \     src/dec/quant.c \     src/dec/tree.c \     src/dec/vp8.c \     src/dec/webp.c \     src/dec/io.c \     src/dec/buffer.c \     src/dsp/yuv.c \     src/dsp/upsampling.c \     src/dsp/cpu.c \     src/dsp/dec.c \     src/dsp/dec_neon.c \     src/dsp/enc.c \     src/utils/bit_reader.c \     src/utils/bit_writer.c \     src/utils/thread.c \     src/libwebp_java_wrap.c \  LOCAL_CFLAGS := -Wall -DANDROID -DHAVE_MALLOC_H -DHAVE_PTHREAD -DWEBP_USE_THREAD \                 -finline-functions -frename-registers -ffast-math \                 -s -fomit-frame-pointer -Isrc/webp  LOCAL_C_INCLUDES += $(LOCAL_PATH)/src  LOCAL_MODULE:= webp  include $(BUILD_SHARED_LIBRARY) 

Content for libwebp_java_wrap.c is here, it's basically the same as bundled in libwebp tarball, except encoder bits removed.

Content for Application.mk:

# The ARMv7 is significanly faster due to the use of the hardware FPU APP_ABI := armeabi armeabi-v7a APP_PLATFORM := android-9 

Here's how to use in Java code. Notice how it converts byte[] array to int[] color array--this will break if endianness changes, right? Also notice how it uses arrays instead of single integers for width and height so they are passed by reference.

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);  } 
like image 136
Pēteris Caune Avatar answered Sep 23 '22 22:09

Pēteris Caune


WebP is supported for Android 4.0+, a.k.a. API level 14. You can check using android.os.Build.VERSION.SDK_INT >= 14.

like image 32
beetstra Avatar answered Sep 23 '22 22:09

beetstra