/** * 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; } }