package epson.common;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.Display;
import epson.print.CommonDefine;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImageUtil {
    public static String png2jpeg(Context context, String str, int i) {
        File file;
        Bitmap createBitmap;
        String str2;
        File file2;
        Bitmap bitmap = null;
        Bitmap bitmap2 = null;
        while (true) {
            try {
                file = new File(str);
                FileInputStream fileInputStream = new FileInputStream(file);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = i;
                Bitmap decodeStream = BitmapFactory.decodeStream(fileInputStream, (Rect) null, options);
                createBitmap = Bitmap.createBitmap(decodeStream.getWidth(), decodeStream.getHeight(), Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(createBitmap);
                canvas.drawColor(-1);
                canvas.drawBitmap(decodeStream, 0.0f, 0.0f, (Paint) null);
                str2 = String.valueOf(System.currentTimeMillis()) + ".jpg";
                file2 = new File(ExternalFileUtils.getInstance(context).getTempViewDir(), str2);
                break;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e2) {
                e2.printStackTrace();
            } catch (OutOfMemoryError e3) {
                e3.printStackTrace();
                i++;
                if (bitmap != null) {
                    bitmap.recycle();
                    bitmap = null;
                }
                if (bitmap2 != null) {
                    bitmap2.recycle();
                    bitmap2 = null;
                }
                System.gc();
            }
        }
        if (file.exists()) {
            file2.delete();
        }
        if (file2.createNewFile()) {
            new FileOutputStream(file2).write(bmp2data(createBitmap, Bitmap.CompressFormat.JPEG, 100));
        }
        return str2;
    }

    private static byte[] bmp2data(Bitmap bitmap, Bitmap.CompressFormat compressFormat, int i) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(compressFormat, i, byteArrayOutputStream);
        return byteArrayOutputStream.toByteArray();
    }

    public static Bitmap loadBitmap(String str, int i, int i2, boolean z, boolean z2) throws OutOfMemoryError {
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(str, options);
            if (options.outWidth > options.outHeight) {
                options.inSampleSize = (options.outWidth / i) + 1;
            } else {
                options.inSampleSize = (options.outHeight / i2) + 1;
            }
            options.inJustDecodeBounds = false;
            if (z && z2) {
                options.inPreferredConfig = Bitmap.Config.RGB_565;
            }
            Bitmap decodeFile = BitmapFactory.decodeFile(str, options);
            return !z ? createGrayBitmap(decodeFile) : decodeFile;
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            throw e;
        }
    }

    public static Bitmap createGrayBitmap(Bitmap bitmap) {
        Bitmap createBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(createBitmap);
        Paint paint = new Paint();
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.set(new float[]{0.11f, 0.59f, 0.3f, 0.0f, 0.0f, 0.11f, 0.59f, 0.3f, 0.0f, 0.0f, 0.11f, 0.59f, 0.3f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f});
        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        canvas.drawBitmap(bitmap, 0.0f, 0.0f, paint);
        return createBitmap;
    }

    public static BitmapFactory.Options getImageSize2(String str) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(str, options);
        return options;
    }

    public static Point getImageSize(String str) {
        if (str == null) {
            return null;
        }
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(str, options);
        return new Point(options.outWidth, options.outHeight);
    }

    @SuppressLint({"NewApi"})
    public static Point getDisplaySize(Display display) {
        Point point = new Point();
        display.getSize(point);
        return point;
    }

    public static void addImage(ContentResolver contentResolver, File file) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("mime_type", "image/jpeg");
        contentValues.put("title", file.getName());
        contentValues.put("_data", file.toString());
        contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
    }

    public static void galleryAddPic(Context context, File file) {
        Intent intent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
        intent.setData(Uri.fromFile(file));
        context.sendBroadcast(intent);
    }

    public static boolean isHEIFFormat(String str) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(str, options);
        if (options.outWidth <= 0 || options.outHeight <= 0 || options.outMimeType == null || !options.outMimeType.equals(CommonDefine.IMAGE_TYPE_HEIF)) {
            return false;
        }
        return true;
    }

    /* JADX WARNING: Removed duplicated region for block: B:39:0x0055 A[SYNTHETIC, Splitter:B:39:0x0055] */
    /* JADX WARNING: Removed duplicated region for block: B:51:0x0067 A[SYNTHETIC, Splitter:B:51:0x0067] */
    /* JADX WARNING: Removed duplicated region for block: B:57:0x0072  */
    /* JADX WARNING: Removed duplicated region for block: B:60:? A[RETURN, SYNTHETIC] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static boolean convertHEIFtoJPEG(java.lang.String r6, java.lang.String r7) {
        /*
            r0 = 0
            r1 = 0
            android.graphics.Bitmap r2 = android.graphics.BitmapFactory.decodeFile(r6)     // Catch:{ Exception -> 0x0064, all -> 0x0051 }
            if (r2 != 0) goto L_0x0014
            if (r2 == 0) goto L_0x0013
            boolean r6 = r2.isRecycled()
            if (r6 != 0) goto L_0x0013
            r2.recycle()
        L_0x0013:
            return r0
        L_0x0014:
            java.io.File r3 = new java.io.File     // Catch:{ Exception -> 0x0065, all -> 0x004f }
            r3.<init>(r7)     // Catch:{ Exception -> 0x0065, all -> 0x004f }
            java.io.FileOutputStream r4 = new java.io.FileOutputStream     // Catch:{ Exception -> 0x0065, all -> 0x004f }
            r4.<init>(r3)     // Catch:{ Exception -> 0x0065, all -> 0x004f }
            android.graphics.Bitmap$CompressFormat r3 = android.graphics.Bitmap.CompressFormat.JPEG     // Catch:{ Exception -> 0x004d, all -> 0x004a }
            r5 = 100
            boolean r3 = r2.compress(r3, r5, r4)     // Catch:{ Exception -> 0x004d, all -> 0x004a }
            if (r3 != 0) goto L_0x0037
            r4.close()     // Catch:{ IOException -> 0x002b }
        L_0x002b:
            if (r2 == 0) goto L_0x0036
            boolean r6 = r2.isRecycled()
            if (r6 != 0) goto L_0x0036
            r2.recycle()
        L_0x0036:
            return r0
        L_0x0037:
            r4.close()     // Catch:{ Exception -> 0x004d, all -> 0x004a }
            epson.common.ExifCopy.copyExif(r6, r7)     // Catch:{ Exception -> 0x0065, all -> 0x004f }
            if (r2 == 0) goto L_0x0048
            boolean r6 = r2.isRecycled()
            if (r6 != 0) goto L_0x0048
            r2.recycle()
        L_0x0048:
            r0 = 1
            goto L_0x0075
        L_0x004a:
            r6 = move-exception
            r1 = r4
            goto L_0x0053
        L_0x004d:
            r1 = r4
            goto L_0x0065
        L_0x004f:
            r6 = move-exception
            goto L_0x0053
        L_0x0051:
            r6 = move-exception
            r2 = r1
        L_0x0053:
            if (r1 == 0) goto L_0x0058
            r1.close()     // Catch:{ IOException -> 0x0058 }
        L_0x0058:
            if (r2 == 0) goto L_0x0063
            boolean r7 = r2.isRecycled()
            if (r7 != 0) goto L_0x0063
            r2.recycle()
        L_0x0063:
            throw r6
        L_0x0064:
            r2 = r1
        L_0x0065:
            if (r1 == 0) goto L_0x006a
            r1.close()     // Catch:{ IOException -> 0x006a }
        L_0x006a:
            if (r2 == 0) goto L_0x0075
            boolean r6 = r2.isRecycled()
            if (r6 != 0) goto L_0x0075
            r2.recycle()
        L_0x0075:
            return r0
        */
        throw new UnsupportedOperationException("Method not decompiled: epson.common.ImageUtil.convertHEIFtoJPEG(java.lang.String, java.lang.String):boolean");
    }
}
