package com.seewo.vcommons.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.math.BigDecimal;

/* JADX INFO: loaded from: classes.dex */
public class BitmapUtils {
    private static final String TAG = "BitmapUtils";

    public static Bitmap compressBitmap(Bitmap bitmap, int i, int i2) {
        if (bitmap == null) {
            return null;
        }
        return cropCenter(scaleBitmapToContainScreen(bitmap, i, i2), i, i2);
    }

    public static Bitmap compressBitmap(String str, int i, int i2) {
        File file = new File(str);
        if (!file.exists()) {
            return null;
        }
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(fileInputStream, null, options);
            int iCalculateInSampleSize = calculateInSampleSize(options, i, i2);
            options.inJustDecodeBounds = false;
            options.inSampleSize = iCalculateInSampleSize;
            fileInputStream.close();
            return cropCenter(scaleBitmapToContainScreen(BitmapFactory.decodeFile(str, options), i, i2), i, i2);
        } catch (Exception e) {
            Log.d(TAG, "compressBitmap e:" + e);
            return null;
        }
    }

    private static Bitmap cropCenter(Bitmap bitmap, int i, int i2) {
        int height = bitmap.getHeight();
        int width = bitmap.getWidth();
        if (width > i) {
            return Bitmap.createBitmap(bitmap, (width - i) / 2, 0, i, i2);
        }
        return Bitmap.createBitmap(bitmap, 0, (height - i2) / 2, i, i2);
    }

    private static Bitmap scaleBitmapToContainScreen(Bitmap bitmap, int i, int i2) {
        double dDeciMal = deciMal(bitmap.getHeight(), bitmap.getWidth());
        if (dDeciMal < deciMal(i2, i)) {
            i = (int) (((double) i2) / dDeciMal);
        } else {
            i2 = (int) (((double) i) * dDeciMal);
        }
        return Bitmap.createScaledBitmap(bitmap, i, i2, true);
    }

    private static double deciMal(int i, int i2) {
        return new BigDecimal(i / i2).setScale(4, 4).doubleValue();
    }

    private static int calculateInSampleSize(BitmapFactory.Options options, int i, int i2) {
        int i3 = options.outHeight;
        int i4 = options.outWidth;
        int i5 = 1;
        if (i3 > i2 || i4 > i) {
            int i6 = i3 / 2;
            int i7 = i4 / 2;
            while (i6 / i5 >= i2 && i7 / i5 >= i) {
                i5 *= 2;
            }
        }
        return i5;
    }
}
