Android中将webp格式图片转换为png和jpg(续)

    上一篇《Android将webp格式图片转换为png格式方法》在app上线后,部分机型报出了bug,比如华为手机,会在包含静态库的页面中直接报错奔溃,经过反复调试发现网上下载的libweb.so 之前打包的时间有点早,对高版本的系统和cpu类型支持不好,所以在github上找了一个项目,配置到gradle:2.3.1,APP_ABI := all全CPU类型,相关SO压缩文件已经放到我的下载资源里了,有需要的同学可以自行下载,或者直接git:webp-android-master项目自己编译也行,直接上代码:..

WebPDecoder类定义如下:

package me.everything.webp;

import android.graphics.Bitmap;

import java.nio.ByteBuffer;

public class WebPDecoder {
    private static WebPDecoder instance = null;

    private WebPDecoder() {
        System.loadLibrary("webp_evme");
    }

    public static WebPDecoder getInstance() {
        if (instance == null) {
            synchronized (WebPDecoder.class) {
                if (instance == null) {
                    instance = new WebPDecoder();
                }
            }
        }

        return instance;
    }

    public Bitmap decodeWebP(byte[] encoded) {
        return decodeWebP(encoded, 0, 0);
    }

    public Bitmap decodeWebP(byte[] encoded, int w, int h) {
        int[] width = new int[]{w};
        int[] height = new int[]{h};

        byte[] decoded = decodeRGBAnative(encoded, encoded.length, width, height);
        if (decoded.length == 0) return null;

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

    public static native byte[] decodeRGBAnative(byte[] encoded, long encodedLength, int[] width, int[] height);
}

使用方法:

Bitmap bp = WebPDecoder.getInstance().decodeWebP(loadFileAsByteArray(file.getAbsolutePath()));

就一句话,loadFileAsByteArray方法定在上一篇博客里,现在华为、中兴、vivo、小米手机调试无问题,测试组测试后发现问题,为再补充,先写到这里。




  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尚俊飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值