package io.flutter.plugins.imagepicker;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import e0.h;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/* JADX INFO: loaded from: classes.dex */
class ImageResizer {
    private final Context context;
    private final ExifDataCopier exifDataCopier;

    public ImageResizer(Context context, ExifDataCopier exifDataCopier) {
        this.context = context;
        this.exifDataCopier = exifDataCopier;
    }

    private int calculateSampleSize(BitmapFactory.Options options, int i, int i5) {
        int i6 = options.outHeight;
        int i7 = options.outWidth;
        int i8 = 1;
        if (i6 <= i5 && i7 <= i) {
            return 1;
        }
        int i9 = i6 / 2;
        int i10 = i7 / 2;
        while (i9 / i8 >= i5 && i10 / i8 >= i) {
            i8 *= 2;
        }
        return i8;
    }

    private O.d calculateTargetSize(double d3, double d6, Double d7, Double d8) {
        double d9 = d3 / d6;
        boolean z5 = false;
        boolean z6 = d7 != null;
        boolean z7 = d8 != null;
        double dMin = z6 ? Math.min(d3, Math.round(d7.doubleValue())) : d3;
        double dMin2 = z7 ? Math.min(d6, Math.round(d8.doubleValue())) : d6;
        boolean z8 = z6 && d7.doubleValue() < d3;
        if (z7 && d8.doubleValue() < d6) {
            z5 = true;
        }
        if (z8 || z5) {
            double d10 = dMin2 * d9;
            double d11 = dMin / d9;
            if (d11 > dMin2) {
                dMin = Math.round(d10);
            } else {
                dMin2 = Math.round(d11);
            }
        }
        return new O.d((float) dMin, (float) dMin2);
    }

    private void copyExif(String str, String str2) throws Throwable {
        try {
            this.exifDataCopier.copyExif(new h(str), new h(str2));
        } catch (Exception e2) {
            Log.e("ImageResizer", "Error preserving Exif data on selected image: " + e2);
        }
    }

    private File createFile(File file, String str) {
        File file2 = new File(file, str);
        if (!file2.getParentFile().exists()) {
            file2.getParentFile().mkdirs();
        }
        return file2;
    }

    private File createImageOnExternalDirectory(String str, Bitmap bitmap, int i) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        boolean zHasAlpha = bitmap.hasAlpha();
        if (zHasAlpha) {
            Log.d("ImageResizer", "image_picker: compressing is not supported for type PNG. Returning the image with original quality");
        }
        bitmap.compress(zHasAlpha ? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG, i, byteArrayOutputStream);
        File fileCreateFile = createFile(this.context.getCacheDir(), str);
        FileOutputStream fileOutputStreamCreateOutputStream = createOutputStream(fileCreateFile);
        fileOutputStreamCreateOutputStream.write(byteArrayOutputStream.toByteArray());
        fileOutputStreamCreateOutputStream.close();
        return fileCreateFile;
    }

    private FileOutputStream createOutputStream(File file) {
        return new FileOutputStream(file);
    }

    private Bitmap createScaledBitmap(Bitmap bitmap, int i, int i5, boolean z5) {
        return Bitmap.createScaledBitmap(bitmap, i, i5, z5);
    }

    private Bitmap decodeFile(String str, BitmapFactory.Options options) {
        return BitmapFactory.decodeFile(str, options);
    }

    private File resizedImage(Bitmap bitmap, Double d3, Double d6, int i, String str) {
        return createImageOnExternalDirectory(s0.c.f("/scaled_", str), createScaledBitmap(bitmap, d3.intValue(), d6.intValue(), false), i);
    }

    public O.d readFileDimensions(String str) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        decodeFile(str, options);
        return new O.d(options.outWidth, options.outHeight);
    }

    public String resizeImageIfNeeded(String str, Double d3, Double d6, int i) throws Throwable {
        O.d fileDimensions = readFileDimensions(str);
        if (fileDimensions.f2255a != -1.0f) {
            float f6 = fileDimensions.f2256b;
            if (f6 != -1.0f) {
                if (d3 == null && d6 == null && i >= 100) {
                    return str;
                }
                try {
                    String str2 = str.split("/")[r2.length - 1];
                    try {
                        O.d dVarCalculateTargetSize = calculateTargetSize(fileDimensions.f2255a, f6, d3, d6);
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        float f7 = dVarCalculateTargetSize.f2255a;
                        float f8 = dVarCalculateTargetSize.f2256b;
                        options.inSampleSize = calculateSampleSize(options, (int) f7, (int) f8);
                        Bitmap bitmapDecodeFile = decodeFile(str, options);
                        if (bitmapDecodeFile == null) {
                            return str;
                        }
                        try {
                            File fileResizedImage = resizedImage(bitmapDecodeFile, Double.valueOf(dVarCalculateTargetSize.f2255a), Double.valueOf(f8), i, str2);
                            copyExif(str, fileResizedImage.getPath());
                            return fileResizedImage.getPath();
                        } catch (IOException e2) {
                            e = e2;
                        }
                    } catch (IOException e6) {
                        e = e6;
                    }
                } catch (IOException e7) {
                    e = e7;
                }
                throw new RuntimeException(e);
            }
        }
        return str;
    }
}
