的WebP为Android [英] WebP for Android

查看:213
本文介绍了的WebP为Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有关于如何使用的WebP为Android任何例子吗?我试着去获得的WebP图像的列表,并显示他们在一个ImageView的列表视图。

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.

我知道那里有一个libwebp API和我已经将它变成我使用NDK Android项目,但我怎么excatly使用API​​来把我的二进制的WebP,并显示在一个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?

任何帮助将是惊人的!

推荐答案

使用libwebp与NDK。 libwebp-0.1.3已经自带了Android.mk文件(过时和语法错误,但仍然)。这也得到了产生JNI绑定在 /痛饮/ 目录。

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. 在下载NDK,把它放在系统 PATH
  2. 下载libwebp-0.1.3.tar.gz,将其放置在 your_project_dir / JNI
  3. 替换 Android.mk 与下面的之一。
  4. 创建 JNI / src目录/ libwebp_java_wrap.c 与下面的内容。
  5. 创建 JNI / Application.mk ,请用下面的内容。
  6. 运行 NDK建造从项目目录。这会产生在 的.so 文件/库/ 。您可以检查它们与纳米-D库/ armeabi / libwebp.so 。在列表中,你会看到两个本机库函数(如 WebPDe codeRGB )和它们的JNI同行(如 Java_com_google_webp_libwebpJNI_WebPDe codeRGB
  7. 添加 /jni/swig/libwebp.jar 来建立你的Andr​​oid项目的路径
  8. 如见下文如何使用它在Java中code
  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

下面是对于 Android.mk 内容。从原来的改变:去掉连接codeR位,因为我不需要这些,加入 libwebp_java_wrap.c ,一改包括$(BUILD_STATIC_LIBRARY)包括$(BUILD_SHARED_LIBRARY)

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)

内容的 libwebp_java_wrap.c 这里,它基本上是一样捆绑在libwebp压缩包,除了恩codeR位删除。

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

有关内容的 Application.mk

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

下面是如何在Java中code使用。请注意它转换byte []数组为int []数组的颜色 - 如果字节顺序的变化,这将打破,对吧?还要注意它是如何使用的阵列,而不是单一的整数的宽度和高度,使他们通过引用传递。

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);

}

这篇关于的WebP为Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆