安卓中webp格式图片转换格式上传服务器(包含.jar和.so文件)


/**
 * Created by pc on 2017/8/24.
 * 转换图片格式为webp的图片、这种格式要么服务器处理、要么客户端本地处理、否则上传不支持
http://download.csdn.net/download/dsb2008dsb/9813264(.jar和.so下载地址)
 */
public class WebpImageToPngImageUtils {

    static {
        System.loadLibrary("webp");
    }

    public static byte[] loadFileAsByteArray(String filePath) {
        File file = new File(filePath);
        byte[] data = new byte[(int) file.length()];
        try {
            FileInputStream inputStream;
            inputStream = new FileInputStream(file);
            inputStream.read(data);
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }

    public static Bitmap webpToBitmap(byte[] encoded) {
        int[] width = new int[]{0};
        int[] height = new int[]{0};
        byte[] decoded = libwebp.WebPDecodeARGB(encoded, encoded.length, width, height);
        //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);
    }

    public static File changeImageType(File file) {
        File newFile;
        //webp图片格式判断代码:
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file.getAbsolutePath(), options);
        String type = options.outMimeType;
        if (type.equals("image/webp")) {
//        Bitmap转png代码:
            Bitmap bp = webpToBitmap(loadFileAsByteArray(file.getAbsolutePath()));

            String c = file.getParent();
            newFile = new File(c + File.separator + System.currentTimeMillis() + ".png");
            if (newFile.exists()) {
                newFile.delete();
            }
            FileOutputStream out;
            try {
                out = new FileOutputStream(newFile);
                if (bp.compress(Bitmap.CompressFormat.PNG, 90, out)) {
                    out.flush();
                    out.close();
                }
                return newFile;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值