/**
* 压缩图片
* @param file
* @param destPath
* @return
*/
public static File thirdCompress(File file, String destPath) {
String filename = com.duudu.lib.utils.FileUtils.getFileNameNoSuffix(file);
String thumb = destPath;
String filePath = file.getAbsolutePath();
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) Math.ceil((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) Math.ceil((double) ((float) options.outHeight / (float) height));
widthRatio = (int) Math.ceil((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 = exifInterface.getAttributeInt("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);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), 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 (!result.exists() && !result.mkdirs()) {
return null;
} else {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
int options = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, options, stream);
while ((long) (stream.toByteArray().length / 1024) > size && options > 6) {
stream.reset();
options -= 6;
bitmap.compress(Bitmap.CompressFormat.JPEG, options, stream);
}
bitmap.recycle();
try {
FileOutputStream fos = new FileOutputStream(filePath);
fos.write(stream.toByteArray());
fos.flush();
fos.close();
stream.close();
} catch (IOException var8) {
var8.printStackTrace();
}
return new File(filePath);
}
}
}
Android 压缩图片 方法
猜你喜欢
转载自blog.csdn.net/wl724120268/article/details/80687976
今日推荐
周排行