Android 压缩图片 方法

阅读: 评论:0

Android 压缩图片 方法

Android 压缩图片 方法

/*** 压缩图片* @param file* @param destPath* @return*/public static File thirdCompress(File file, String destPath) {String filename = com.duudu.lib.FileNameNoSuffix(file);String thumb = destPath;String filePath = AbsolutePath();int angle = getImageSpinAngle(filePath);int width = getImageSize(filePath)[0];int height = getImageSize(filePath)[1];int thumbW = width % 2 == 1 ? width + 1 : width;int thumbH = height % 2 == 1 ? height + 1 : height;width = thumbW > thumbH ? thumbH : thumbW;height = thumbW > thumbH ? thumbW : thumbH;double scale = (double) width / (double) height;double size;int multiple;if (scale <= 1.0D && scale > 0.5625D) {if (height < 1664) {if (file.length() / 1024L < 150L) {return file;}size = (double) (width * height) / Math.pow(1664.0D, 2.0D) * 150.0D;size = size < 60.0D ? 60.0D : size;} else if (height >= 1664 && height < 4990) {thumbW = width / 2;thumbH = height / 2;size = (double) (thumbW * thumbH) / Math.pow(2495.0D, 2.0D) * 300.0D;size = size < 60.0D ? 60.0D : size;} else if (height >= 4990 && height < 10240) {thumbW = width / 4;thumbH = height / 4;size = (double) (thumbW * thumbH) / Math.pow(2560.0D, 2.0D) * 300.0D;size = size < 100.0D ? 100.0D : size;} else {multiple = height / 1280 == 0 ? 1 : height / 1280;thumbW = width / multiple;thumbH = height / multiple;size = (double) (thumbW * thumbH) / Math.pow(2560.0D, 2.0D) * 300.0D;size = size < 100.0D ? 100.0D : size;}} else if (scale <= 0.5625D && scale > 0.5D) {if (height < 1280 && file.length() / 1024L < 200L) {return file;}multiple = height / 1280 == 0 ? 1 : height / 1280;thumbW = width / multiple;thumbH = height / multiple;size = (double) (thumbW * thumbH) / 3686400.0D * 400.0D;size = size < 100.0D ? 100.0D : size;} else {multiple = (int) il((double) height / (1280.0D / scale));thumbW = width / multiple;thumbH = height / multiple;size = (double) (thumbW * thumbH) / (1280.0D * (1280.0D / scale)) * 500.0D;size = size < 100.0D ? 100.0D : size;}return compress(filePath, thumb, thumbW, thumbH, angle, (long) size);}public static int[] getImageSize(String imagePath) {int[] res = new int[2];BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;options.inSampleSize = 1;BitmapFactory.decodeFile(imagePath, options);res[0] = options.outWidth;res[1] = options.outHeight;return res;}private static Bitmap compress(String imagePath, int width, int height) {BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeFile(imagePath, options);int outH = options.outHeight;int outW = options.outWidth;int inSampleSize = 1;int heightRatio;int widthRatio;if (outH > height || outW > width) {heightRatio = outH / 2;for (widthRatio = outW / 2; heightRatio / inSampleSize > height && widthRatio / inSampleSize > width; inSampleSize *= 2) {;}}options.inSampleSize = inSampleSize;options.inJustDecodeBounds = false;heightRatio = (int) il((double) ((float) options.outHeight / (float) height));widthRatio = (int) il((double) ((float) options.outWidth / (float) width));if (heightRatio > 1 || widthRatio > 1) {if (heightRatio > widthRatio) {options.inSampleSize = heightRatio;} else {options.inSampleSize = widthRatio;}}options.inJustDecodeBounds = false;return BitmapFactory.decodeFile(imagePath, options);}private static int getImageSpinAngle(String path) {short degree = 0;try {ExifInterface exifInterface = new ExifInterface(path);int orientation = AttributeInt("Orientation", 1);switch (orientation) {case 3:degree = 180;break;case 6:degree = 90;break;case 8:degree = 270;}} catch (IOException var4) {logger.e(var4);}return degree;}private static File compress(String largeImagePath, String thumbFilePath, int width, int height, int angle, long size) {Bitmap thbBitmap = compress(largeImagePath, width, height);thbBitmap = rotatingImage(angle, thbBitmap);return saveImage(thumbFilePath, thbBitmap, size);}private static Bitmap rotatingImage(int angle, Bitmap bitmap) {Matrix matrix = new Matrix();matrix.postRotate((float) angle);ateBitmap(bitmap, 0, 0, Width(), Height(), matrix, true);}private static File saveImage(String filePath, Bitmap bitmap, long size) {if (bitmap == null) {throw new NullPointerException(String.valueOf("bitmap cannot be null"));} else {File result = new File(filePath.substring(0, filePath.lastIndexOf("/")));if (!ists() && !result.mkdirs()) {return null;} else {ByteArrayOutputStream stream = new ByteArrayOutputStream();int options = 100;bitmappress(Bitmap.CompressFormat.JPEG, options, stream);while ((long) (ByteArray().length / 1024) > size && options > 6) {set();options -= 6;bitmappress(Bitmap.CompressFormat.JPEG, options, stream);}le();try {FileOutputStream fos = new FileOutputStream(filePath);fos.ByteArray());fos.flush();fos.close();stream.close();} catch (IOException var8) {var8.printStackTrace();}return new File(filePath);}}}

本文发布于:2024-02-01 18:29:32,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170678337438608.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:方法   图片   Android
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23