package com.benq.platform.utils;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/* JADX INFO: loaded from: classes.dex */
public class BitmapUtils {
    public static final int JPEG_FULL_QUALITY = 100;
    public static final int MAX_HEIGHT_BITMAP = 2560;
    public static final int MAX_WIDTH_BITMAP = 1440;
    private static final String TAG = "BitmapUtils";
    private static final int THUMBNAIL_UTILS_OPTIONS_NONE = 0;

    private BitmapUtils() {
    }

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

    public static boolean compressBySize(Bitmap bitmap, String str, int i2, int i3) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(byteArrayOutputStream.toByteArray(), 0, byteArrayOutputStream.toByteArray().length, options);
        int i4 = options.outWidth;
        int i5 = options.outHeight;
        int iCeil = (int) Math.ceil(i4 / i2);
        int iCeil2 = (int) Math.ceil(i5 / i3);
        if (iCeil > iCeil2) {
            options.inSampleSize = iCeil;
        } else {
            options.inSampleSize = iCeil2;
        }
        options.inJustDecodeBounds = false;
        return saveBitmapToSDcard(BitmapFactory.decodeByteArray(byteArrayOutputStream.toByteArray(), 0, byteArrayOutputStream.toByteArray().length, options), str, 100);
    }

    private static int computeInitialSampleSize(BitmapFactory.Options options, int i2, int i3) {
        int iMin;
        double d2 = options.outWidth;
        double d3 = options.outHeight;
        int iCeil = i3 == -1 ? 1 : (int) Math.ceil(Math.sqrt((d2 * d3) / ((double) i3)));
        if (i2 == -1) {
            iMin = 128;
        } else {
            double d4 = i2;
            iMin = (int) Math.min(Math.floor(d2 / d4), Math.floor(d3 / d4));
        }
        if (iMin < iCeil) {
            return iCeil;
        }
        if (i3 == -1 && i2 == -1) {
            return 1;
        }
        return i2 == -1 ? iCeil : iMin;
    }

    private static int computeSampleSize(BitmapFactory.Options options, int i2, int i3) {
        int iComputeInitialSampleSize = computeInitialSampleSize(options, i2, i3);
        if (iComputeInitialSampleSize > 8) {
            return 8 * ((iComputeInitialSampleSize + 7) / 8);
        }
        int i4 = 1;
        while (i4 < iComputeInitialSampleSize) {
            i4 <<= 1;
        }
        return i4;
    }

    public static boolean createThumbnailToPath(Bitmap bitmap, String str, int i2, int i3, int i4) {
        Bitmap bitmapExtractThumbnail = ThumbnailUtils.extractThumbnail(bitmap, i2, i3, i4);
        if (bitmapExtractThumbnail == null) {
            return false;
        }
        return saveBitmapToSDcard(bitmapExtractThumbnail, str, 100);
    }

    public static Bitmap decodeSampleBitmapFromFile(String str, int i2, int i3) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(str, options);
        options.inSampleSize = calculateInSampleSize(options, i2, i3);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(str, options);
    }

    public static Bitmap decodeSampledBitmapFromResource(Resources resources, int i2, int i3, int i4) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(resources, i2, options);
        options.inSampleSize = calculateInSampleSize(options, i3, i4);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(resources, i2, options);
    }

    public static Bitmap getBitmapFromFile(File file, int i2, int i3) {
        BitmapFactory.Options options;
        if (file != null && file.exists()) {
            if (i2 <= 0 || i3 <= 0) {
                options = null;
            } else {
                options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(file.getPath(), options);
                options.inSampleSize = computeSampleSize(options, Math.min(i2, i3), i2 * i3);
                options.inJustDecodeBounds = false;
                options.inInputShareable = true;
                options.inPurgeable = true;
            }
            try {
                return BitmapFactory.decodeFile(file.getPath(), options);
            } catch (OutOfMemoryError e2) {
                Log.e(TAG, e2.getMessage());
            }
        }
        return null;
    }

    public static Bitmap getThumbnailBitmap(String str, int i2, int i3) {
        return ThumbnailUtils.extractThumbnail(decodeSampleBitmapFromFile(str, i2, i3), i2, i3, 2);
    }

    public static boolean saveBitmapToSDcard(Bitmap bitmap, String str, int i2) throws Throwable {
        FileOutputStream fileOutputStream;
        FileOutputStream fileOutputStream2 = null;
        try {
            try {
                fileOutputStream = new FileOutputStream(str);
            } catch (Throwable th) {
                th = th;
            }
        } catch (FileNotFoundException e2) {
            e = e2;
        }
        try {
            boolean zCompress = bitmap.compress(Bitmap.CompressFormat.JPEG, i2, fileOutputStream);
            try {
                fileOutputStream.flush();
                fileOutputStream.close();
            } catch (IOException e3) {
                Log.e(TAG, e3.getMessage());
            }
            return zCompress;
        } catch (FileNotFoundException e4) {
            e = e4;
            fileOutputStream2 = fileOutputStream;
            Log.e(TAG, e.getMessage());
            if (fileOutputStream2 == null) {
                return false;
            }
            try {
                fileOutputStream2.flush();
                fileOutputStream2.close();
                return false;
            } catch (IOException e5) {
                Log.e(TAG, e5.getMessage());
                return false;
            }
        } catch (Throwable th2) {
            th = th2;
            fileOutputStream2 = fileOutputStream;
            if (fileOutputStream2 != null) {
                try {
                    fileOutputStream2.flush();
                    fileOutputStream2.close();
                } catch (IOException e6) {
                    Log.e(TAG, e6.getMessage());
                }
            }
            throw th;
        }
    }

    public static boolean createThumbnailToPath(Bitmap bitmap, String str, int i2, int i3) {
        return createThumbnailToPath(bitmap, str, i2, i3, 0);
    }

    public static Bitmap getBitmapFromFile(File file) {
        if (file == null || !file.exists() || !file.isFile()) {
            return null;
        }
        try {
            return BitmapFactory.decodeFile(file.getPath());
        } catch (OutOfMemoryError e2) {
            Log.e(TAG, e2.getMessage());
            return null;
        }
    }

    public static boolean saveBitmapToSDcard(Bitmap bitmap, File file, int i2, boolean z2) {
        if (file == null) {
            return false;
        }
        if (file.exists()) {
            if (!z2) {
                return false;
            }
            file.delete();
        }
        return saveBitmapToSDcard(bitmap, file.getPath(), i2);
    }
}
