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 java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import epson.print.CommonDefine;

public class ImageUtil {
    /**
     * png转jpg
     *
     * @param context 上下文
     * @param str
     * @param i
     * @return
     * @throws IOException
     */
    public static String png2jpeg(Context context, String str, int i) throws IOException {
        File file;
        Bitmap createBitmap;
        String str2;
        File file2;
        Bitmap bitmap = null;
        Bitmap bitmap2 = null;
        while (true) {

            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;

        }
        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;
    }


    public static boolean convertHEIFtoJPEG(java.lang.String r6, java.lang.String r7) {

        throw new UnsupportedOperationException("Method not decompiled: epson.common.ImageUtil.convertHEIFtoJPEG(java.lang.String, java.lang.String):boolean");
    }
}
