阅读并显示WebP图像

时间:2012-07-18 23:13:28

标签: android jar bitmap webp

我知道在某些地方已经提到过这个问题(比如here),但有没有一种简单的方法可以读取WebP图像,比如使用jar文件?

即使在较旧的设备上,最小化应用图像的大小也会很棒。

是否可以将WebP inputStream读取到普通位图实例,就像使用其他格式一样,即使在较旧的Android版本(早于ICS)上也是如此?

2 个答案:

答案 0 :(得分:2)

您提供的示例非常好,总结一下,您需要遵循以下步骤:

  • 下载网址库,当前版本here
  • 在您的Android“jni”文件夹中创建,将libwep内容复制到其中
  • 在jni / Android.mk中添加:

    swig / libwebp_java_wrap.c \

    LOCAL_SRC_FILES := \  并删除include $(BUILD_STATIC_LIBRARY),改为include $(BUILD_SHARED_LIBRARY)

  • 转到项目目录并运行

    ./路径到机器人-NDK / NDK-构建

  • 如果您不知道如何使用ndk,请尝试here

  • 如果您成功编译,则获取libwebp.jar,将其包含在项目构建路径中

  • 在你的活动中,你先说:

    static {     的System.loadLibrary( “WEBP”);   }

  • 然后你可以通过InputStream读取.webp文件(即来自assets文件夹):

    InputStream is = getAssets()。open(“image.webp”);

  • here下载apache库,将其包含在项目构建路径中

  • 然后您可以将InputStream读取为字节数组:

    byte [] encoded = IOUtils.toByteArray(is);

  • 通过libwebp解码文件:

    int [] width = new int [] {0};

    int [] height = new int [] {0};

    byte [] decoding = libwebp.WebPDecodeARGB(encoded,encoded.length,width,height);

  • 转换为位图:

    int [] pixels = new int [decoding.length / 4];

    ByteBuffer.wrap(解码).asIntBuffer()得到(像素);

    位图bitamp = Bitmap.createBitmap(pixels,width [0],height [0],Bitmap.Config.ARGB_8888);

  • 最后一行来自here

  • 无论你想要什么,都可以使用你的位图。

答案 1 :(得分:0)

如果您的webp图像存储在资产文件夹中,请使用此代码读取并在imageView中显示

try {
            InputStream inputStream = context.getAssets().open("yourfilename.webp");
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
            Bitmap bmp = BitmapFactory.decodeStream(bufferedInputStream);
            imageView.setImageBitmap(bmp);
           
        } catch (IOException e) {
            e.printStackTrace();
        }