package io.flutter.plugins.imagepicker; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; /* loaded from: classes6.dex */ class ImageResizer { private final ExifDataCopier exifDataCopier; private final File externalFilesDirectory; /* JADX INFO: Access modifiers changed from: package-private */ public ImageResizer(File file, ExifDataCopier exifDataCopier) { this.externalFilesDirectory = file; this.exifDataCopier = exifDataCopier; } /* JADX INFO: Access modifiers changed from: package-private */ public String resizeImageIfNeeded(String str, Double d, Double d2, Integer num) { Bitmap decodeFile = decodeFile(str); if (decodeFile == null) { return null; } if (d == null && d2 == null && !isImageQualityValid(num)) { return str; } try { File resizedImage = resizedImage(decodeFile, d, d2, num, str.split("/")[r0.length - 1]); copyExif(str, resizedImage.getPath()); return resizedImage.getPath(); } catch (IOException e) { throw new RuntimeException(e); } } private File resizedImage(Bitmap bitmap, Double d, Double d2, Integer num, String str) throws IOException { double width = bitmap.getWidth(); double height = bitmap.getHeight(); Integer num2 = num; if (!isImageQualityValid(num2)) { num2 = 100; } boolean z = d != null; boolean z2 = d2 != null; Double valueOf = Double.valueOf(z ? Math.min(width, d.doubleValue()) : width); Double valueOf2 = Double.valueOf(z2 ? Math.min(height, d2.doubleValue()) : height); boolean z3 = z && d.doubleValue() < width; boolean z4 = z2 && d2.doubleValue() < height; if (z3 || z4) { double doubleValue = (valueOf2.doubleValue() / height) * width; double doubleValue2 = (valueOf.doubleValue() / width) * height; if (valueOf.doubleValue() < valueOf2.doubleValue()) { if (!z) { valueOf = Double.valueOf(doubleValue); } else { valueOf2 = Double.valueOf(doubleValue2); } } else if (valueOf2.doubleValue() < valueOf.doubleValue()) { if (!z2) { valueOf2 = Double.valueOf(doubleValue2); } else { valueOf = Double.valueOf(doubleValue); } } else if (width < height) { valueOf = Double.valueOf(doubleValue); } else if (height < width) { valueOf2 = Double.valueOf(doubleValue2); } } return createImageOnExternalDirectory("/scaled_".concat(String.valueOf(str)), createScaledBitmap(bitmap, valueOf.intValue(), valueOf2.intValue(), false), num2.intValue()); } private File createFile(File file, String str) { File file2 = new File(file, str); if (!file2.getParentFile().exists()) { file2.getParentFile().mkdirs(); } return file2; } private FileOutputStream createOutputStream(File file) throws IOException { return new FileOutputStream(file); } private void copyExif(String str, String str2) { this.exifDataCopier.copyExif(str, str2); } private Bitmap decodeFile(String str) { return BitmapFactory.decodeFile(str); } private Bitmap createScaledBitmap(Bitmap bitmap, int i, int i2, boolean z) { return Bitmap.createScaledBitmap(bitmap, i, i2, z); } private boolean isImageQualityValid(Integer num) { return num != null && num.intValue() > 0 && num.intValue() < 100; } private File createImageOnExternalDirectory(String str, Bitmap bitmap, int i) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); bitmap.compress(bitmap.hasAlpha() ? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG, i, byteArrayOutputStream); File createFile = createFile(this.externalFilesDirectory, str); FileOutputStream createOutputStream = createOutputStream(createFile); createOutputStream.write(byteArrayOutputStream.toByteArray()); createOutputStream.close(); return createFile; } }