适用于 Android 的 WebP

发布于 2024-11-28 22:39:11 字数 1539 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

柠檬 2024-12-05 22:39:11

将 libwebp 与 NDK 结合使用。
libwebp-0.1.3 已经附带 Android.mk 文件(已过时且有语法错误,但仍然如此)。它还在 /swig/ 目录中生成了 JNI 绑定。

我的工作方式如下:

  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-build 。这会在 /libs/ 中生成 .so 文件。您可以使用 nm -D libs/armeabi/libwebp.so 来检查它们。在列表中,您将看到本机库函数(例如 WebPDecodeRGB)及其 JNI 对应函数(例如 Java_com_google_webp_libwebpJNI_WebPDecodeRGB
  7. 添加 /jni/swig/libwebp.jar 构建 Android 项目的路径
  8. 请参阅下面的示例,了解如何在 Java 代码中使用它

这里是 Android.mk 的内容。对原始内容进行了更改:删除了编码器位,因为我不需要这些,添加了 libwebp_java_wrap.c,将 include $(BUILD_STATIC_LIBRARY) 更改为 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 tarball 中捆绑的内容基本相同,只是删除了编码器位。

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 代码中使用。注意它如何将 byte[] 数组转换为 int[] 颜色数组——如果字节顺序改变,这将会中断,对吧?另请注意它如何使用数组而不是单个整数来表示宽度和高度,以便它们通过引用传递。

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

}

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

}
与君绝 2024-12-05 22:39:11

WebP 支持 Android 4.0+,即 API 级别 14。可以检查使用
android.os.Build.VERSION.SDK_INT >= 14

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

爱本泡沫多脆弱 2024-12-05 22:39:11

Google 声称从 Android 4.0+ 开始支持 WebP ( http://developer.android .com/guide/appendix/media-formats.html ),但是在我们自己的测试中,webp 图像在标准浏览器以及 Android 4.0 和 4.1 上的 Chrome 中都显示为蓝色问号。
在 Android 4.2 上,WebP 图像在 webview 和 google chrome 中似乎可以正常渲染。

Google claims that WebP is supported starting from Android 4.0+ ( http://developer.android.com/guide/appendix/media-formats.html ), however in our own tests webp images show as blue questionmarks both in the standard browser and Chrome on Android 4.0 and 4.1.
On Android 4.2 WebP images seem to be rendered ok in a webview and in google chrome.

┾廆蒐ゝ 2024-12-05 22:39:11

我们为此编写了一个 Android 库。

https://github.com/EverythingMe/webp-android

webp-android是我们使用的一个库在 EverythingMe,因为我们喜欢 webp。我们用它来节省带宽并缩小 APK 大小。

webp-android 是 chromium 的 webp 解码器的改编版,并添加了 JNI 包装器,以便在 java 代码中轻松使用它。
将 webp 图像从 xml 加载到 ImageView(使用包含的 WebpImageView)也很容易,如下所示:

<me.everything.webp.WebpImageView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  webp:webp_src="@drawable/your_webp_image" />

We've written an Android library just for that.

https://github.com/EverythingMe/webp-android

webp-android is a library we use at EverythingMe since we love webp. We use it to save bandwidth as well as shrinking our APK sizes.

webp-android is an adaptation of chromium's webp decoder, and an addition of a JNI wrapper to easily use it it in your java code.
It's also easy to load webp images from xml to an ImageView (with the included WebpImageView) like so:

<me.everything.webp.WebpImageView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  webp:webp_src="@drawable/your_webp_image" />
萌面超妹 2024-12-05 22:39:11

目前无法在 Android 设备上的任何本机应用程序(包括 Web 浏览器)上显示 webp 图像。您必须研究第三方应用程序才能显示这些图像。

根据 WebP 邮件列表,他们正在努力将 WebP 支持纳入 Android SDK 中。他们没有透露具体计划何时发布,但当他们发布时,你应该能够将位图保存为 WebP 格式以及 JPEG 和 PNG。

编辑:Android 4.0 又名 Ice Cream Sandwich 现在原生支持 WebP 格式。您可以在 Android 开发者网站查看支持的文件类型。

There is currently no way to display a webp image on any native app on an Android device, including the web browser. You will have to look into 3rd party apps to display these images.

According to the WebP mailing list, they are working on incorporating WebP support into the Android SDK. They did not say when exactly they plan to release this but when they do you should be able to save bitmaps as WebP format as well as JPEG and PNG.

EDIT: Android 4.0 aka Ice Cream Sandwich now comes with native support for the WebP format. You can see supported file types at the Android developer site.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文