180 lines
7.5 KiB
Java
180 lines
7.5 KiB
Java
package com.ubt.jimu.utils;
|
|
|
|
import android.graphics.Bitmap;
|
|
import android.graphics.BitmapFactory;
|
|
import android.util.Log;
|
|
import com.ijm.dataencryption.de.DataDecryptTool;
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.IOException;
|
|
|
|
/* loaded from: classes2.dex */
|
|
public class BitmapCompressUtils {
|
|
|
|
public static class BitmapAttriBute {
|
|
public int a;
|
|
public Bitmap.Config b;
|
|
public String c;
|
|
public int d;
|
|
|
|
public void a(int i) {
|
|
this.d = i;
|
|
}
|
|
|
|
public void b(int i) {
|
|
this.a = i;
|
|
}
|
|
|
|
public String c() {
|
|
return this.c;
|
|
}
|
|
|
|
public int d() {
|
|
return this.a;
|
|
}
|
|
|
|
public void a(String str) {
|
|
this.c = str;
|
|
}
|
|
|
|
public int b() {
|
|
return this.d;
|
|
}
|
|
|
|
public Bitmap.Config a() {
|
|
return this.b;
|
|
}
|
|
|
|
public void a(Bitmap.Config config) {
|
|
this.b = config;
|
|
}
|
|
}
|
|
|
|
public static int a(BitmapAttriBute bitmapAttriBute) {
|
|
int d = bitmapAttriBute.d();
|
|
int b = bitmapAttriBute.b();
|
|
double d2 = d;
|
|
if (d2 < 1280.0d && b < 1280.0d) {
|
|
return 0;
|
|
}
|
|
if (d2 <= 1280.0d && b <= 1280.0d) {
|
|
return 0;
|
|
}
|
|
int i = d / b;
|
|
return (d2 <= 1280.0d || ((double) b) <= 1280.0d) ? i > 1 ? i > 2 ? 0 : 1 : ((double) i) < 7.8125E-4d ? 0 : 2 : i > 1 ? 2 : 1;
|
|
}
|
|
|
|
public static BitmapAttriBute b(String str) {
|
|
Bitmap decodeFile = BitmapFactory.decodeFile(str);
|
|
Log.e("comp_file_space", "原图片占用空间:" + ((decodeFile.getByteCount() / DataDecryptTool.DECRYPT_SP_FILE) / DataDecryptTool.DECRYPT_SP_FILE));
|
|
decodeFile.recycle();
|
|
BitmapFactory.Options options = new BitmapFactory.Options();
|
|
options.inJustDecodeBounds = true;
|
|
BitmapFactory.decodeFile(str, options);
|
|
options.inJustDecodeBounds = false;
|
|
BitmapAttriBute bitmapAttriBute = new BitmapAttriBute();
|
|
bitmapAttriBute.a(options.outHeight);
|
|
bitmapAttriBute.b(options.outWidth);
|
|
bitmapAttriBute.a(options.outMimeType);
|
|
bitmapAttriBute.a(options.inPreferredConfig);
|
|
return bitmapAttriBute;
|
|
}
|
|
|
|
public static byte[] a(String str) {
|
|
BitmapAttriBute b = b(str);
|
|
int a = a(b);
|
|
int i = 100;
|
|
if (a == 0) {
|
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
Bitmap decodeFile = BitmapFactory.decodeFile(str);
|
|
decodeFile.compress(b.c().contains("png") ? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
|
|
try {
|
|
byteArrayOutputStream.flush();
|
|
byteArrayOutputStream.close();
|
|
decodeFile.recycle();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return byteArrayOutputStream.toByteArray();
|
|
}
|
|
if (a != 1) {
|
|
if (a == 2) {
|
|
ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream();
|
|
BitmapFactory.Options options = new BitmapFactory.Options();
|
|
options.inJustDecodeBounds = false;
|
|
int ceil = (int) Math.ceil(b.b() / 1280.0d);
|
|
options.inSampleSize = ceil;
|
|
options.outWidth = (int) ((b.d() * 1280.0d) / b.b());
|
|
options.outHeight = 1280;
|
|
options.inDither = false;
|
|
options.inPreferredConfig = null;
|
|
options.inPurgeable = true;
|
|
options.inInputShareable = true;
|
|
Log.e("compress_height", ceil + "");
|
|
Bitmap decodeFile2 = BitmapFactory.decodeFile(str, options);
|
|
Log.e("compress_size_height", (decodeFile2.getByteCount() / DataDecryptTool.DECRYPT_SP_FILE) + "kb");
|
|
decodeFile2.compress(b.c().contains("png") ? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream2);
|
|
Log.e("compress_size_h_byte", (byteArrayOutputStream2.size() / DataDecryptTool.DECRYPT_SP_FILE) + "kb");
|
|
while (byteArrayOutputStream2.size() > 1048576 && i > 50) {
|
|
byteArrayOutputStream2.reset();
|
|
i -= 25;
|
|
decodeFile2.compress(b.c().contains("png") ? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG, i, byteArrayOutputStream2);
|
|
Log.e("compress_heigth_qulity", (byteArrayOutputStream2.size() / DataDecryptTool.DECRYPT_SP_FILE) + "kb");
|
|
}
|
|
byte[] byteArray = byteArrayOutputStream2.toByteArray();
|
|
try {
|
|
byteArrayOutputStream2.flush();
|
|
byteArrayOutputStream2.close();
|
|
decodeFile2.recycle();
|
|
} catch (IOException e2) {
|
|
e2.printStackTrace();
|
|
}
|
|
return byteArray;
|
|
}
|
|
ByteArrayOutputStream byteArrayOutputStream3 = new ByteArrayOutputStream();
|
|
Bitmap decodeFile3 = BitmapFactory.decodeFile(str);
|
|
decodeFile3.compress(b.c().contains("png") ? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream3);
|
|
try {
|
|
byteArrayOutputStream3.flush();
|
|
byteArrayOutputStream3.close();
|
|
decodeFile3.recycle();
|
|
} catch (IOException e3) {
|
|
e3.printStackTrace();
|
|
}
|
|
return byteArrayOutputStream3.toByteArray();
|
|
}
|
|
ByteArrayOutputStream byteArrayOutputStream4 = new ByteArrayOutputStream();
|
|
BitmapFactory.Options options2 = new BitmapFactory.Options();
|
|
options2.inJustDecodeBounds = false;
|
|
int ceil2 = (int) Math.ceil(b.d() / 1280.0d);
|
|
options2.inSampleSize = ceil2;
|
|
options2.inPreferredConfig = Bitmap.Config.ARGB_8888;
|
|
Log.e("compress_width", ceil2 + "");
|
|
options2.outHeight = (int) ((((double) b.b()) * 1280.0d) / ((double) b.d()));
|
|
options2.outWidth = 1280;
|
|
options2.inDither = false;
|
|
options2.inPreferredConfig = b.a();
|
|
options2.inPurgeable = true;
|
|
options2.inInputShareable = true;
|
|
Bitmap decodeFile4 = BitmapFactory.decodeFile(str, options2);
|
|
Log.e("compress_size", "原图片高度:" + b.b() + "原图片宽度:" + b.d());
|
|
Log.e("compress_size", "图片大小" + ((decodeFile4.getByteCount() / DataDecryptTool.DECRYPT_SP_FILE) / DataDecryptTool.DECRYPT_SP_FILE) + "M 图片高度:" + decodeFile4.getHeight() + "图片宽度:" + decodeFile4.getWidth());
|
|
decodeFile4.compress(b.c().contains("png") ? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream4);
|
|
Log.e("compress_size_byte", (byteArrayOutputStream4.size() / DataDecryptTool.DECRYPT_SP_FILE) + "kb");
|
|
while (byteArrayOutputStream4.size() > 1048576 && i > 50) {
|
|
byteArrayOutputStream4.reset();
|
|
i -= 25;
|
|
decodeFile4.compress(b.c().contains("png") ? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG, i, byteArrayOutputStream4);
|
|
Log.e("compress_width_qulity", (byteArrayOutputStream4.size() / DataDecryptTool.DECRYPT_SP_FILE) + "kb");
|
|
}
|
|
byte[] byteArray2 = byteArrayOutputStream4.toByteArray();
|
|
try {
|
|
byteArrayOutputStream4.flush();
|
|
byteArrayOutputStream4.close();
|
|
decodeFile4.recycle();
|
|
} catch (IOException e4) {
|
|
e4.printStackTrace();
|
|
}
|
|
return byteArray2;
|
|
}
|
|
}
|