package com.kofax.kmc.kui.uicontrols; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Debug; import android.os.Environment; import com.kofax.kmc.kut.utilities.CertificateValidatorListener; import com.kofax.mobile.sdk._internal.impl.extraction.rtti.RttiJsonExactionHelper; import com.kofax.mobile.sdk._internal.k; import com.kofax.mobile.sdk.ak.d; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.nio.channels.FileChannel; import org.json.JSONObject; /* loaded from: classes3.dex */ public class ImageService { private static final String TAG = "ImageService"; public static final int THUMBNAIL_SIZE = 100; private static final int ig = 1; private static final int ih = 1; private static final int ii = 200; private static final int ij = 200; private static final int ik = 24; private static final int il = 3; private static final int im = 72; /* renamed from: io, reason: collision with root package name */ private static final int f6504io = 72; private static boolean kK = false; private static Bitmap kL; private static String kM; private static String kN; private static UseableImageMemoryLimit kO = UseableImageMemoryLimit.NORMAL; private static InterimImageFileFormat kP = InterimImageFileFormat.PNG; /* loaded from: classes3.dex */ public enum InterimImageFileFormat { PNG, JPEG } /* loaded from: classes3.dex */ public enum UseableImageMemoryLimit { NORMAL, LARGE, MAXIMUM } public static String getNonNullIpResults() { String str = kN; return str == null ? new String() : str; } public static void storeGlobalBitmap(Bitmap bitmap) { k.b(TAG, "Storing global bitmap"); Bitmap bitmap2 = kL; if (bitmap2 != null && bitmap2 != bitmap) { clearGlobalBitmap(); } kL = bitmap; } public static void clearGlobalBitmap() { k.b(TAG, "Clearing global bitmap"); Bitmap bitmap = kL; if (bitmap != null) { bitmap.recycle(); kL = null; } } public static Bitmap getBitmapFromURL(URL url) { try { HttpURLConnection a = d.a(url, (CertificateValidatorListener) null); a.setDoInput(true); a.connect(); return BitmapFactory.decodeStream(a.getInputStream()); } catch (Exception unused) { return null; } } public static Bitmap getBitmapFromURI(Context context, Uri uri) { InputStream inputStream; try { inputStream = context.getContentResolver().openInputStream(uri); } catch (FileNotFoundException unused) { inputStream = null; } if (inputStream != null) { return BitmapFactory.decodeStream(inputStream); } return null; } private static int a(Context context, Uri uri, int i, int i2) throws FileNotFoundException { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; InputStream openInputStream = context.getContentResolver().openInputStream(uri); Bitmap decodeStream = BitmapFactory.decodeStream(openInputStream, null, options); int i3 = options.outHeight; float f = i / options.outWidth; float f2 = i2 / i3; try { openInputStream.close(); } catch (Exception e) { k.e(TAG, "Error closing input stream", (Throwable) e); } if (decodeStream != null) { decodeStream.recycle(); } return (int) Math.pow(2.0d, Math.min(f, f2)); } private static long af() { long j = Runtime.getRuntime().totalMemory(); long maxMemory = Runtime.getRuntime().maxMemory(); long j2 = maxMemory / 2; long min = Math.min(j, j2); int i = AnonymousClass1.kQ[kO.ordinal()]; if (i == 1) { maxMemory = j2; } else if (i != 2) { maxMemory = min; } dumpHeapStats("getLargestUseableImage()"); return maxMemory; } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: com.kofax.kmc.kui.uicontrols.ImageService$1, reason: invalid class name */ /* loaded from: classes3.dex */ public static /* synthetic */ class AnonymousClass1 { static final int[] kQ; static { int[] iArr = new int[UseableImageMemoryLimit.values().length]; kQ = iArr; try { iArr[UseableImageMemoryLimit.LARGE.ordinal()] = 1; } catch (NoSuchFieldError unused) { } try { kQ[UseableImageMemoryLimit.MAXIMUM.ordinal()] = 2; } catch (NoSuchFieldError unused2) { } try { kQ[UseableImageMemoryLimit.NORMAL.ordinal()] = 3; } catch (NoSuchFieldError unused3) { } } } public static void dumpHeapStats(String str) { String str2 = TAG; k.b(str2, "*** [START] Dumping heap stats ***"); k.b(str2, "--- msg -> ".concat(String.valueOf(str))); StringBuilder sb = new StringBuilder("--- native heap size -> "); sb.append(Debug.getNativeHeapSize()); k.b(str2, sb.toString()); StringBuilder sb2 = new StringBuilder("--- native heap size (free) -> "); sb2.append(Debug.getNativeHeapFreeSize()); k.b(str2, sb2.toString()); StringBuilder sb3 = new StringBuilder("--- native heap size (allocated) -> "); sb3.append(Debug.getNativeHeapAllocatedSize()); k.b(str2, sb3.toString()); StringBuilder sb4 = new StringBuilder("--- totalMemory -> "); sb4.append(Runtime.getRuntime().totalMemory()); k.b(str2, sb4.toString()); StringBuilder sb5 = new StringBuilder("--- maxMemory -> "); sb5.append(Runtime.getRuntime().maxMemory()); k.b(str2, sb5.toString()); k.b(str2, "*** [END] Dumping heap stats ***"); } public static Bitmap createScaledBitmap(Bitmap bitmap, boolean z) { long af = af(); long rowBytes = bitmap.getRowBytes() * bitmap.getHeight(); String str = TAG; k.b(str, "Creating scaled bitmap..."); k.b(str, "... original bitmap size -> ".concat(String.valueOf(rowBytes))); if (rowBytes > af) { dumpHeapStats("createScaledBitmap - preScale"); int width = bitmap.getWidth(); int height = bitmap.getHeight(); float f = ((float) af) / ((float) rowBytes); int i = (int) (width * f); int i2 = (int) (height * f); k.b(str, "... scaling bitmap with factor of ".concat(String.valueOf(f))); try { Bitmap createScaledBitmap = Bitmap.createScaledBitmap(bitmap, i, i2, true); StringBuilder sb = new StringBuilder("... new bitmap size -> "); sb.append(createScaledBitmap.getRowBytes() * createScaledBitmap.getHeight()); k.b(str, sb.toString()); if (z) { k.b(str, "... recyling old bitmap"); bitmap.recycle(); bitmap = null; } dumpHeapStats("createScaledBitmap - postScale"); return createScaledBitmap; } catch (OutOfMemoryError e) { String str2 = TAG; k.e(str2, "Got OOM while trying to create scaled bitmap", (Throwable) e); k.e(str2, "... largestImageSize -> ".concat(String.valueOf(af))); k.e(str2, "... bmSize -> ".concat(String.valueOf(rowBytes))); StringBuilder sb2 = new StringBuilder("... orgHeight -> "); sb2.append(height); sb2.append(", orgWidth -> "); sb2.append(width); k.e(str2, sb2.toString()); StringBuilder sb3 = new StringBuilder("... height -> "); sb3.append(i2); sb3.append(", width -> "); sb3.append(i); k.e(str2, sb3.toString()); } } return bitmap; } public static Bitmap rotateImage(byte[] bArr, boolean z, boolean z2) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inPurgeable = true; dumpHeapStats("rotateImage - preDecode"); Bitmap decodeByteArray = BitmapFactory.decodeByteArray(bArr, 0, bArr.length, options); dumpHeapStats("rotateImage - postDecode"); return rotateBitmap(decodeByteArray, z, z2); } /* JADX WARN: Removed duplicated region for block: B:16:0x00e9 */ /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct add '--show-bad-code' argument */ public static android.graphics.Bitmap rotateBitmap(android.graphics.Bitmap r13, boolean r14, boolean r15) { /* java.lang.String r0 = "Error rotating bitmap" java.lang.String r1 = com.kofax.kmc.kui.uicontrols.ImageService.TAG java.lang.String r2 = "About to scale bitmap. Source information before scaling: " com.kofax.mobile.sdk._internal.k.b(r1, r2) java.lang.StringBuilder r2 = new java.lang.StringBuilder java.lang.String r3 = "... size -> " r2.(r3) int r4 = r13.getRowBytes() int r5 = r13.getHeight() int r4 = r4 * r5 r2.append(r4) java.lang.String r2 = r2.toString() com.kofax.mobile.sdk._internal.k.b(r1, r2) java.lang.StringBuilder r2 = new java.lang.StringBuilder java.lang.String r4 = "... height -> " r2.(r4) int r5 = r13.getHeight() r2.append(r5) java.lang.String r2 = r2.toString() com.kofax.mobile.sdk._internal.k.b(r1, r2) java.lang.StringBuilder r2 = new java.lang.StringBuilder java.lang.String r5 = "... width -> " r2.(r5) int r6 = r13.getWidth() r2.append(r6) java.lang.String r2 = r2.toString() com.kofax.mobile.sdk._internal.k.b(r1, r2) if (r15 == 0) goto L54 r15 = 1 android.graphics.Bitmap r13 = createScaledBitmap(r13, r15) L54: android.graphics.Matrix r11 = new android.graphics.Matrix r11.() if (r14 == 0) goto L5e r14 = 1119092736(0x42b40000, float:90.0) goto L60 L5e: r14 = -1028390912(0xffffffffc2b40000, float:-90.0) L60: r11.postRotate(r14) java.lang.String r14 = "rotateImage - preRotate" dumpHeapStats(r14) java.lang.String r14 = "About to rotate bitmap. Source information: " com.kofax.mobile.sdk._internal.k.b(r1, r14) java.lang.StringBuilder r14 = new java.lang.StringBuilder r14.(r3) int r15 = r13.getRowBytes() int r2 = r13.getHeight() int r15 = r15 * r2 r14.append(r15) java.lang.String r14 = r14.toString() com.kofax.mobile.sdk._internal.k.b(r1, r14) java.lang.StringBuilder r14 = new java.lang.StringBuilder r14.(r4) int r15 = r13.getHeight() r14.append(r15) java.lang.String r14 = r14.toString() com.kofax.mobile.sdk._internal.k.b(r1, r14) java.lang.StringBuilder r14 = new java.lang.StringBuilder r14.(r5) int r15 = r13.getWidth() r14.append(r15) java.lang.String r14 = r14.toString() com.kofax.mobile.sdk._internal.k.b(r1, r14) r7 = 0 r8 = 0 r14 = 0 int r9 = r13.getWidth() // Catch: java.lang.Exception -> Lda java.lang.OutOfMemoryError -> Le1 int r10 = r13.getHeight() // Catch: java.lang.Exception -> Lda java.lang.OutOfMemoryError -> Le1 r12 = 0 r6 = r13 android.graphics.Bitmap r14 = android.graphics.Bitmap.createBitmap(r6, r7, r8, r9, r10, r11, r12) // Catch: java.lang.Exception -> Lda java.lang.OutOfMemoryError -> Le1 java.lang.StringBuilder r15 = new java.lang.StringBuilder // Catch: java.lang.Exception -> Lda java.lang.OutOfMemoryError -> Le1 java.lang.String r2 = "... rotated bitmap size -> " r15.(r2) // Catch: java.lang.Exception -> Lda java.lang.OutOfMemoryError -> Le1 int r2 = r14.getRowBytes() // Catch: java.lang.Exception -> Lda java.lang.OutOfMemoryError -> Le1 int r3 = r14.getHeight() // Catch: java.lang.Exception -> Lda java.lang.OutOfMemoryError -> Le1 int r2 = r2 * r3 r15.append(r2) // Catch: java.lang.Exception -> Lda java.lang.OutOfMemoryError -> Le1 java.lang.String r15 = r15.toString() // Catch: java.lang.Exception -> Lda java.lang.OutOfMemoryError -> Le1 com.kofax.mobile.sdk._internal.k.b(r1, r15) // Catch: java.lang.Exception -> Lda java.lang.OutOfMemoryError -> Le1 r13.recycle() // Catch: java.lang.Exception -> Lda java.lang.OutOfMemoryError -> Le1 return r14 Lda: r15 = move-exception java.lang.String r1 = com.kofax.kmc.kui.uicontrols.ImageService.TAG com.kofax.mobile.sdk._internal.k.e(r1, r0, r15) goto Le7 Le1: r15 = move-exception java.lang.String r1 = com.kofax.kmc.kui.uicontrols.ImageService.TAG com.kofax.mobile.sdk._internal.k.e(r1, r0, r15) Le7: if (r14 == 0) goto Lec r14.recycle() Lec: return r13 */ throw new UnsupportedOperationException("Method not decompiled: com.kofax.kmc.kui.uicontrols.ImageService.rotateBitmap(android.graphics.Bitmap, boolean, boolean):android.graphics.Bitmap"); } private static void f(Bitmap bitmap) { JSONObject jSONObject = new JSONObject(); try { jSONObject.put(RttiJsonExactionHelper.HEIGHT, bitmap.getHeight()); jSONObject.put(RttiJsonExactionHelper.WIDTH, bitmap.getWidth()); jSONObject.put("density", bitmap.getDensity()); jSONObject.put("rowBytes", bitmap.getRowBytes()); jSONObject.put("totalBytes", bitmap.getRowBytes() * bitmap.getHeight()); } catch (Exception e) { k.e(TAG, "Error creating JSON for Bitmap", (Throwable) e); } String str = TAG; StringBuilder sb = new StringBuilder("Bitmap -> "); sb.append(jSONObject.toString()); k.b(str, sb.toString()); } public static String dumpBitmapSize(Bitmap bitmap, String str) { if (bitmap == null || bitmap.isRecycled()) { return "Unable to calculate size"; } StringBuilder sb = new StringBuilder(); StringBuilder sb2 = new StringBuilder("Dumping bitmap data from "); sb2.append(str); sb2.append(": "); sb.append(sb2.toString()); StringBuilder sb3 = new StringBuilder("Height -> "); sb3.append(bitmap.getHeight()); sb.append(sb3.toString()); StringBuilder sb4 = new StringBuilder(", Width -> "); sb4.append(bitmap.getWidth()); sb.append(sb4.toString()); StringBuilder sb5 = new StringBuilder(", Density -> "); sb5.append(bitmap.getDensity()); sb.append(sb5.toString()); StringBuilder sb6 = new StringBuilder(", Size -> "); sb6.append(bitmap.getRowBytes() * bitmap.getHeight()); sb.append(sb6.toString()); return sb.toString(); } public static Bitmap loadBitmapFromFile(Activity activity, String str, int i) { return loadBitmapFromURI(activity, Uri.fromFile(new File(str)), i, -1L); } public static Bitmap loadBitmapFromURI(Activity activity, Uri uri, int i, long j) { Bitmap decodeStream; while (true) { try { InputStream openInputStream = activity.getContentResolver().openInputStream(uri); try { try { BitmapFactory.Options options = new BitmapFactory.Options(); options.inPurgeable = true; if (i > 1) { options.inSampleSize = i; } decodeStream = BitmapFactory.decodeStream(openInputStream, null, options); try { openInputStream.close(); } catch (Exception e) { k.e(TAG, "Error closing input stream", (Throwable) e); } if (j <= 0 || decodeStream.getRowBytes() * decodeStream.getHeight() <= j) { break; } if (decodeStream != null) { decodeStream.recycle(); } i <<= 1; } catch (OutOfMemoryError unused) { String str = TAG; StringBuilder sb = new StringBuilder("OOM while loading a bitmap at scale "); sb.append(i); k.e(str, sb.toString()); Bitmap loadBitmapFromURI = loadBitmapFromURI(activity, uri, i << 1, j); try { openInputStream.close(); } catch (Exception e2) { k.e(TAG, "Error closing input stream", (Throwable) e2); } return loadBitmapFromURI; } } catch (Throwable th) { try { openInputStream.close(); } catch (Exception e3) { k.e(TAG, "Error closing input stream", (Throwable) e3); } throw th; } } catch (FileNotFoundException e4) { k.e(TAG, "Error loading file", (Throwable) e4); return null; } } return decodeStream; } public static void copyImageToExternalStorage(File file) throws IOException { Throwable th; FileChannel fileChannel; FileChannel fileChannel2; StringBuilder sb = new StringBuilder(); sb.append(Environment.getExternalStorageDirectory()); sb.append(File.separator); sb.append(file.getName()); File file2 = new File(sb.toString()); FileChannel fileChannel3 = null; try { file2.createNewFile(); FileChannel channel = new FileInputStream(file).getChannel(); try { fileChannel3 = new FileOutputStream(file2).getChannel(); channel.transferTo(0L, channel.size(), fileChannel3); String str = TAG; StringBuilder sb2 = new StringBuilder("copyImageToExternalStorage(), successfully moved image to external storage Path: "); sb2.append(file2.getAbsolutePath()); k.c(str, sb2.toString()); if (channel != null) { channel.close(); } if (fileChannel3 != null) { fileChannel3.close(); } } catch (IOException unused) { FileChannel fileChannel4 = fileChannel3; fileChannel3 = channel; fileChannel2 = fileChannel4; if (fileChannel3 != null) { fileChannel3.close(); } if (fileChannel2 != null) { fileChannel2.close(); } } catch (Throwable th2) { th = th2; FileChannel fileChannel5 = fileChannel3; fileChannel3 = channel; fileChannel = fileChannel5; if (fileChannel3 != null) { fileChannel3.close(); } if (fileChannel == null) { throw th; } fileChannel.close(); throw th; } } catch (IOException unused2) { fileChannel2 = null; } catch (Throwable th3) { th = th3; fileChannel = null; } } public static void setUseableImageMemoryLimits(UseableImageMemoryLimit useableImageMemoryLimit) { kO = useableImageMemoryLimit; } public static void setInterimImageFileFormat(InterimImageFileFormat interimImageFileFormat) { kP = interimImageFileFormat; } public static UseableImageMemoryLimit getUseableImageMemoryLimits() { return kO; } public static String getIpResults() { return kN; } public static InterimImageFileFormat getInterimImageFileFormat() { return kP; } public static Bitmap getGlobalBitmap() { return kL; } public static String getEnhancedBitonalImageFileName() { return kM; } }