PicWish v1.6.8版本的 MD5 值为:7e8bab240f03cf6dad8952f8374c4ed1

以下内容为反编译后的 BitmapUtil.java 源代码,内容仅作参考


package com.apowersoft.common.bitmap;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.MediaMetadataRetriever;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.util.Size;
import android.view.View;
import android.webkit.URLUtil;
import androidx.appcompat.view.a;
import androidx.exifinterface.media.ExifInterface;
import com.apowersoft.common.CommonApplication;
import com.apowersoft.common.logger.Logger;
import com.apowersoft.common.oss.upload.OssErrorCode;
import com.google.firebase.perf.network.FirebasePerfUrlConnection;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
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.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
public class BitmapUtil {
    private static final String TAG = "BitmapUtil";
    private static final String imageBase64Head = "data:image/";
    private static final String jpegBase64Head = "data:image/jpeg;base64,";
    private static final String jpgBase64Head = "data:image/jpg;base64,";
    private static final String pngBase64Head = "data:image/png;base64,";

    private static byte[] addBMPImageHeader(int i10) {
        return new byte[]{66, 77, (byte) (i10 >> 0), (byte) (i10 >> 8), (byte) (i10 >> 16), (byte) (i10 >> 24), 0, 0, 0, 0, 54, 0, 0, 0};
    }

    private static byte[] addBMPImageInfosHeader(int i10, int i11) {
        return new byte[]{40, 0, 0, 0, (byte) (i10 >> 0), (byte) (i10 >> 8), (byte) (i10 >> 16), (byte) (i10 >> 24), (byte) (i11 >> 0), (byte) (i11 >> 8), (byte) (i11 >> 16), (byte) (i11 >> 24), 1, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, -32, 1, 0, 0, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    }

    private static byte[] addBMP_RGB_888(int[] iArr, int i10, int i11) {
        byte[] bArr = new byte[i11 * i10 * 3];
        int length = iArr.length - 1;
        int i12 = 0;
        while (length >= i10) {
            int i13 = length - i10;
            for (int i14 = i13 + 1; i14 <= length; i14++) {
                bArr[i12] = (byte) (iArr[i14] >> 0);
                bArr[i12 + 1] = (byte) (iArr[i14] >> 8);
                bArr[i12 + 2] = (byte) (iArr[i14] >> 16);
                i12 += 3;
            }
            length = i13;
        }
        return bArr;
    }

    public static String addPngBase64Head(String str) {
        return a.d(pngBase64Head, str);
    }

    public static Bitmap base64ToBitmap(String str) throws IllegalArgumentException {
        try {
            byte[] base64ToByte = base64ToByte(str);
            return BitmapFactory.decodeByteArray(base64ToByte, 0, base64ToByte.length);
        } catch (Exception e10) {
            e10.printStackTrace();
            return null;
        }
    }

    public static byte[] base64ToByte(String str) throws IllegalArgumentException {
        return Base64.decode(str, 0);
    }

    public static byte[] bitmap2Bytes(Bitmap bitmap) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
        return byteArrayOutputStream.toByteArray();
    }

    public static Drawable bitmap2Drawable(Bitmap bitmap) {
        if (bitmap == null) {
            return null;
        }
        return new BitmapDrawable(bitmap);
    }

    public static InputStream bitmap2InputStream(Bitmap bitmap, int i10, Bitmap.CompressFormat compressFormat) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        if (compressFormat == null) {
            compressFormat = Bitmap.CompressFormat.PNG;
        }
        bitmap.compress(compressFormat, i10, byteArrayOutputStream);
        return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    }

    public static String bitmapToBase64(Bitmap bitmap) {
        ByteArrayOutputStream byteArrayOutputStream;
        ByteArrayOutputStream byteArrayOutputStream2 = null;
        if (bitmap == null) {
            return null;
        }
        try {
            byteArrayOutputStream = new ByteArrayOutputStream();
            try {
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
                byteArrayOutputStream.flush();
                byteArrayOutputStream.close();
                String bytesToBase64 = bytesToBase64(byteArrayOutputStream.toByteArray());
                try {
                    byteArrayOutputStream.flush();
                    byteArrayOutputStream.close();
                } catch (IOException e10) {
                    e10.printStackTrace();
                }
                return bytesToBase64;
            } catch (Exception unused) {
                try {
                    byteArrayOutputStream.flush();
                    byteArrayOutputStream.close();
                } catch (IOException e11) {
                    e11.printStackTrace();
                }
                return null;
            } catch (Throwable th2) {
                th = th2;
                byteArrayOutputStream2 = byteArrayOutputStream;
                try {
                    byteArrayOutputStream2.flush();
                    byteArrayOutputStream2.close();
                } catch (IOException e12) {
                    e12.printStackTrace();
                }
                throw th;
            }
        } catch (Exception unused2) {
            byteArrayOutputStream = null;
        } catch (Throwable th3) {
            th = th3;
        }
    }

    public static Bitmap bytes2Bitmap(byte[] bArr) {
        if (bArr.length != 0) {
            return BitmapFactory.decodeByteArray(bArr, 0, bArr.length);
        }
        return null;
    }

    public static Bitmap bytes2Bitmap(byte[] bArr, int i10, int i11) {
        if (bArr.length != 0) {
            Bitmap decodeByteArray = BitmapFactory.decodeByteArray(bArr, 0, bArr.length, computeSampleSize(bArr, i10, i11));
            if (decodeByteArray == null) {
                return null;
            }
            return createSmallBitmap(decodeByteArray, i10, i11, null);
        }
        return null;
    }

    public static String bytesToBase64(byte[] bArr) {
        return Base64.encodeToString(bArr, 0);
    }

    public static void computeOptions(BitmapFactory.Options options, int i10, int i11) {
        options.inDither = false;
        options.inJustDecodeBounds = false;
        options.inSampleSize = 1;
        int i12 = options.outWidth;
        int i13 = options.outHeight;
        if (i12 <= 0 || i13 <= 0 || i10 <= 0 || i11 <= 0) {
            return;
        }
        options.inSampleSize = ((int) Math.min((i12 * 1.0f) / i10, (i13 * 1.0f) / i11)) + 1;
    }

    public static BitmapFactory.Options computeSampleSize(InputStream inputStream, int i10, int i11) {
        BitmapFactory.Options createOptions = createOptions();
        BitmapFactory.decodeStream(inputStream, null, createOptions);
        computeOptions(createOptions, i10, i11);
        return createOptions;
    }

    public static BitmapFactory.Options computeSampleSize(String str, int i10, int i11) {
        BitmapFactory.Options createOptions = createOptions();
        createOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
        BitmapFactory.decodeFile(str, createOptions);
        computeOptions(createOptions, i10, i11);
        return createOptions;
    }

    public static BitmapFactory.Options computeSampleSize(byte[] bArr, int i10, int i11) {
        BitmapFactory.Options createOptions = createOptions();
        BitmapFactory.decodeByteArray(bArr, 0, bArr.length, createOptions);
        computeOptions(createOptions, i10, i11);
        return createOptions;
    }

    public static Bitmap convertViewToBitmap(View view) {
        view.clearFocus();
        view.setPressed(false);
        boolean willNotCacheDrawing = view.willNotCacheDrawing();
        view.setWillNotCacheDrawing(false);
        int drawingCacheBackgroundColor = view.getDrawingCacheBackgroundColor();
        view.setDrawingCacheBackgroundColor(0);
        if (drawingCacheBackgroundColor != 0) {
            view.destroyDrawingCache();
        }
        view.buildDrawingCache();
        Bitmap drawingCache = view.getDrawingCache();
        if (drawingCache == null) {
            return null;
        }
        Bitmap createBitmap = Bitmap.createBitmap(drawingCache);
        view.destroyDrawingCache();
        view.setWillNotCacheDrawing(willNotCacheDrawing);
        view.setDrawingCacheBackgroundColor(drawingCacheBackgroundColor);
        return createBitmap;
    }

    public static synchronized Bitmap createAlbumThumbnail(String str, int i10, int i11) {
        Bitmap bitmap;
        synchronized (BitmapUtil.class) {
            MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
            bitmap = null;
            try {
                try {
                    if (URLUtil.isNetworkUrl(str)) {
                        mediaMetadataRetriever.setDataSource(str, new HashMap());
                    } else {
                        mediaMetadataRetriever.setDataSource(str);
                    }
                    byte[] embeddedPicture = mediaMetadataRetriever.getEmbeddedPicture();
                    if (embeddedPicture != null) {
                        int length = embeddedPicture.length;
                        if (i10 <= 0 || i11 <= 0) {
                            i10 = OssErrorCode.INVALID_PARAMETERS;
                            i11 = OssErrorCode.INVALID_PARAMETERS;
                        }
                        Bitmap decodeByteArray = BitmapFactory.decodeByteArray(embeddedPicture, 0, length, computeSampleSize(embeddedPicture, i10, i11));
                        if (decodeByteArray != null) {
                            if (length > 1048576) {
                                double ceil = Math.ceil(length / 1048576);
                                if (ceil > 0.0d) {
                                    i10 = (int) (decodeByteArray.getWidth() / ceil);
                                    i11 = (int) (decodeByteArray.getHeight() / ceil);
                                }
                            }
                            bitmap = createSmallBitmap(decodeByteArray, i10, i11, null);
                            if (bitmap != decodeByteArray) {
                                decodeByteArray.recycle();
                            }
                        }
                    }
                } catch (RuntimeException e10) {
                    e = e10;
                }
                try {
                    mediaMetadataRetriever.release();
                } catch (RuntimeException e11) {
                    e = e11;
                    e.printStackTrace();
                    return bitmap;
                }
            } catch (Exception unused) {
                mediaMetadataRetriever.release();
            } catch (Throwable th2) {
                try {
                    mediaMetadataRetriever.release();
                } catch (RuntimeException e12) {
                    e12.printStackTrace();
                }
                throw th2;
            }
        }
        return bitmap;
    }

    public static Bitmap createBitmap1080P(String str, boolean z10) {
        return createBitmapByP(str, 1080, z10, new boolean[0]);
    }

    public static Bitmap createBitmap720P(String str, boolean z10) {
        return createBitmapByP(str, 720, z10, new boolean[0]);
    }

    public static Bitmap createBitmapByP(String str, int i10, boolean z10, boolean... zArr) {
        BitmapFactory.Options computeSampleSize = computeSampleSize(str, i10, i10);
        int i11 = computeSampleSize.outWidth;
        int i12 = computeSampleSize.outHeight;
        if (i11 < i10 || i12 < i10) {
            return createSmallBitmap(str, i11, i12, z10, true);
        }
        if (i11 > 0 && i12 > 0) {
            float f10 = (i12 * 1.0f) / i11;
            return f10 >= 1.0f ? (zArr.length <= 0 || !zArr[0]) ? createSmallBitmap(str, i10, (int) (i10 * f10), z10, new boolean[0]) : createSmallBitmap(str, (int) (i10 / f10), i10, z10, new boolean[0]) : (zArr.length <= 0 || !zArr[0]) ? createSmallBitmap(str, (int) (i10 / f10), i10, z10, new boolean[0]) : createSmallBitmap(str, i10, (int) (i10 * f10), z10, new boolean[0]);
        }
        try {
            try {
                try {
                    return BitmapFactory.decodeFile(str, computeSampleSize);
                } catch (Error unused) {
                    computeSampleSize.inSampleSize++;
                    return BitmapFactory.decodeFile(str, computeSampleSize);
                }
            } catch (Exception unused2) {
                computeSampleSize.inSampleSize++;
                return BitmapFactory.decodeFile(str, computeSampleSize);
            }
        } catch (Exception e10) {
            e10.printStackTrace();
            return null;
        }
    }

    public static BitmapFactory.Options createOptions() {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = false;
        options.inDither = false;
        options.inJustDecodeBounds = true;
        options.inPurgeable = true;
        options.inInputShareable = true;
        return options;
    }

    public static Bitmap createSmallBitmap(Bitmap bitmap, int i10, int i11, Matrix matrix) {
        if (bitmap == null) {
            return null;
        }
        if (i10 < 1 || i11 < 1) {
            return bitmap;
        }
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        try {
            Bitmap createBitmap = Bitmap.createBitmap(i10, i11, Bitmap.Config.ARGB_8888);
            Rect rect = new Rect(0, 0, i10, i11);
            Paint paint = new Paint();
            paint.setDither(true);
            paint.setFilterBitmap(true);
            Canvas canvas = new Canvas(createBitmap);
            if (matrix != null) {
                canvas.setMatrix(matrix);
            }
            float f10 = i10;
            float f11 = i11;
            if ((width * 1.0f) / f10 < (height * 1.0f) / f11) {
                int i12 = (int) (((i11 * width) * 1.0f) / f10);
                canvas.drawBitmap(bitmap, new Rect(0, (height - i12) / 2, width, (height + i12) / 2), rect, paint);
            } else {
                int i13 = (int) (((i10 * height) * 1.0f) / f11);
                canvas.drawBitmap(bitmap, new Rect((width - i13) / 2, 0, (width + i13) / 2, height), rect, paint);
            }
            return createBitmap;
        } catch (OutOfMemoryError e10) {
            e10.printStackTrace();
            return null;
        }
    }

    public static Bitmap createSmallBitmap(Bitmap bitmap, int i10, int i11, Matrix matrix, boolean z10) {
        Bitmap createSmallBitmap = createSmallBitmap(bitmap, i10, i11, matrix);
        if (z10 && createSmallBitmap != bitmap) {
            bitmap.recycle();
        }
        return createSmallBitmap;
    }

    public static Bitmap createSmallBitmap(String str, int i10, int i11) {
        return createSmallBitmap(str, i10, i11, true, new boolean[0]);
    }

    public static Bitmap createSmallBitmap(String str, int i10, int i11, boolean z10, boolean... zArr) {
        BitmapFactory.Options options;
        Bitmap decodeFile;
        Bitmap createSmallBitmap;
        Bitmap bitmap = null;
        try {
            if (zArr.length > 0) {
                options = computeSampleSize(str, i10, i11);
                options.inSampleSize = 1;
            } else {
                options = null;
            }
            try {
                decodeFile = BitmapFactory.decodeFile(str, options);
            } catch (Error unused) {
                options.inSampleSize++;
                decodeFile = BitmapFactory.decodeFile(str, options);
            } catch (Exception unused2) {
                options.inSampleSize++;
                decodeFile = BitmapFactory.decodeFile(str, options);
            }
        } catch (Exception e10) {
            e10.printStackTrace();
        }
        if (decodeFile == null) {
            return null;
        }
        if (z10) {
            int readPictureAngle = readPictureAngle(str);
            if (readPictureAngle != 90 && readPictureAngle != 270) {
                createSmallBitmap = createSmallBitmap(decodeFile, i10, i11, null);
            }
            decodeFile = rotateBitmap(decodeFile, readPictureAngle, true);
            createSmallBitmap = createSmallBitmap(decodeFile, i11, i10, null);
        } else {
            createSmallBitmap = createSmallBitmap(decodeFile, i10, i11, null);
        }
        bitmap = createSmallBitmap;
        if (bitmap != decodeFile && !decodeFile.isRecycled()) {
            decodeFile.recycle();
        }
        return bitmap;
    }

    public static Bitmap createVideoThumbnail(String str, int i10, int i11) {
        Bitmap bitmap = null;
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
        try {
            try {
                if (URLUtil.isNetworkUrl(str)) {
                    mediaMetadataRetriever.setDataSource(str, new HashMap());
                } else {
                    mediaMetadataRetriever.setDataSource(str);
                }
                Bitmap frameAtTime = mediaMetadataRetriever.getFrameAtTime();
                if (frameAtTime != null && (bitmap = createSmallBitmap(frameAtTime, i10, i11, null)) != frameAtTime) {
                    frameAtTime.recycle();
                }
                mediaMetadataRetriever.release();
            } catch (RuntimeException e10) {
                e10.printStackTrace();
            }
        } catch (Exception unused) {
            mediaMetadataRetriever.release();
        } catch (Throwable th2) {
            try {
                mediaMetadataRetriever.release();
            } catch (RuntimeException e11) {
                e11.printStackTrace();
            }
            throw th2;
        }
        return bitmap;
    }

    public static Bitmap drawable2Bitmap(Drawable drawable) {
        if (drawable == null) {
            return null;
        }
        int intrinsicWidth = drawable.getIntrinsicWidth();
        int intrinsicHeight = drawable.getIntrinsicHeight();
        Bitmap createBitmap = Bitmap.createBitmap(intrinsicWidth, intrinsicHeight, drawable.getOpacity() != -1 ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(createBitmap);
        drawable.setBounds(0, 0, intrinsicWidth, intrinsicHeight);
        drawable.draw(canvas);
        return createBitmap;
    }

    private static String getAlbumArt(Context context, long j10) {
        Throwable th2;
        Cursor cursor;
        String str;
        Exception e10;
        Cursor cursor2 = null;
        try {
            cursor = context.getContentResolver().query(Uri.parse("content://media/external/audio/albums/" + j10), new String[]{"album_art"}, null, null, null);
        } catch (Exception e11) {
            str = null;
            e10 = e11;
            cursor = null;
        } catch (Throwable th3) {
            th2 = th3;
            cursor = null;
        }
        if (cursor != null) {
            try {
                try {
                } catch (Exception e12) {
                    str = null;
                    e10 = e12;
                }
                if (cursor.moveToFirst()) {
                    str = cursor.getString(0);
                    try {
                        cursor.close();
                        if (cursor2 != null && !cursor2.isClosed()) {
                            cursor2.close();
                        }
                    } catch (Exception e13) {
                        e10 = e13;
                        Logger.e(e10, "getAlbumArt ex");
                        if (cursor != null && !cursor.isClosed()) {
                            cursor.close();
                        }
                        return str;
                    }
                    return str;
                }
            } catch (Throwable th4) {
                th2 = th4;
                if (cursor != null && !cursor.isClosed()) {
                    cursor.close();
                }
                throw th2;
            }
        }
        str = null;
        cursor2 = cursor;
        if (cursor2 != null) {
            cursor2.close();
        }
        return str;
    }

    public static Bitmap getAlbumArtPoster(Context context, long j10) {
        Bitmap decodeFile;
        String albumArt = getAlbumArt(context, j10);
        return (TextUtils.isEmpty(albumArt) || (decodeFile = BitmapFactory.decodeFile(albumArt)) == null) ? getArtwork(context, j10) : decodeFile;
    }

    public static Bitmap getAlbumArtPoster(Context context, long j10, int i10, int i11) {
        Bitmap albumArtPoster;
        Bitmap bitmap = null;
        try {
            albumArtPoster = getAlbumArtPoster(context, j10);
        } catch (Exception e10) {
            e10.printStackTrace();
        } catch (OutOfMemoryError e11) {
            e11.printStackTrace();
        }
        if (i10 >= 1 && i11 >= 1) {
            if (albumArtPoster != null && (bitmap = createSmallBitmap(albumArtPoster, i10, i11, null)) != albumArtPoster) {
                albumArtPoster.recycle();
            }
            return bitmap;
        }
        return albumArtPoster;
    }

    public static Bitmap getAlbumArtPoster(Context context, String str, int i10, int i11) {
        try {
            Cursor query = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[]{"album_id"}, "_data=?", new String[]{str}, null);
            long j10 = 0;
            if (query != null) {
                if (!query.moveToFirst() || query.getCount() <= 0) {
                    return null;
                }
                j10 = query.getLong(0);
                query.close();
            }
            return getAlbumArtPoster(context, j10, i10, i11);
        } catch (Exception e10) {
            e10.printStackTrace();
            return null;
        }
    }

    public static Bitmap getAppIcon(Context context, String str, int i10, int i11) {
        Drawable drawable;
        ApplicationInfo applicationInfo;
        PackageManager packageManager = context.getPackageManager();
        PackageInfo packageArchiveInfo = packageManager.getPackageArchiveInfo(str, 1);
        if (packageArchiveInfo != null && (applicationInfo = packageArchiveInfo.applicationInfo) != null) {
            try {
                applicationInfo.sourceDir = str;
                applicationInfo.publicSourceDir = str;
                drawable = packageManager.getApplicationIcon(applicationInfo);
            } catch (OutOfMemoryError e10) {
                e10.printStackTrace();
            }
            return iconToBitmap(drawable, i10, i11);
        }
        drawable = null;
        return iconToBitmap(drawable, i10, i11);
    }

    private static Bitmap getArtwork(Context e10, long j10) {
        Bitmap bitmap = null;
        bitmap = null;
        bitmap = null;
        InputStream inputStream = null;
        try {
        } catch (Throwable th2) {
            th = th2;
            inputStream = e10;
        }
        try {
            try {
                e10 = e10.getContentResolver().openInputStream(Uri.parse("content://media/external/audio/albumart/" + j10));
            } catch (IOException e11) {
                e10 = e11;
                e10.printStackTrace();
            }
            try {
                bitmap = BitmapFactory.decodeStream(e10, null, null);
            } catch (Exception e12) {
                e = e12;
                Logger.d(TAG, "getArtwork bitmap fail:" + e.getLocalizedMessage());
                if (e10 != 0) {
                    e10.close();
                    e10 = e10;
                }
                return bitmap;
            }
        } catch (Exception e13) {
            e = e13;
            e10 = 0;
        } catch (Throwable th3) {
            th = th3;
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e14) {
                    e14.printStackTrace();
                }
            }
            throw th;
        }
        if (e10 != 0) {
            e10.close();
            e10 = e10;
        }
        return bitmap;
    }

    public static Bitmap getBitmap(String str) {
        Bitmap bitmap = null;
        try {
            URLConnection uRLConnection = (URLConnection) FirebasePerfUrlConnection.instrument(new URL(str).openConnection());
            int contentLength = ((HttpURLConnection) uRLConnection).getContentLength();
            uRLConnection.connect();
            InputStream inputStream = uRLConnection.getInputStream();
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream, contentLength);
            bitmap = BitmapFactory.decodeStream(bufferedInputStream);
            bufferedInputStream.close();
            inputStream.close();
            return bitmap;
        } catch (Exception e10) {
            Logger.e(e10, "BitmapUtil getBitmap exception");
            return bitmap;
        }
    }

    public static Bitmap getBitmapFromNet(String str, int i10, int i11) {
        InputStream inputStream;
        Bitmap decodeStream;
        InputStream inputStream2 = null;
        try {
            try {
                inputStream = getInputStream(str);
                if (inputStream != null) {
                    try {
                        try {
                            BitmapFactory.Options computeSampleSize = computeSampleSize(inputStream, i10, i11);
                            inputStream.close();
                            inputStream = getInputStream(str);
                            if (inputStream != null && (decodeStream = BitmapFactory.decodeStream(inputStream, new Rect(), computeSampleSize)) != null) {
                                Bitmap createSmallBitmap = createSmallBitmap(decodeStream, i10, i11, null);
                                try {
                                    inputStream.close();
                                } catch (IOException e10) {
                                    e10.printStackTrace();
                                }
                                return createSmallBitmap;
                            }
                        } catch (Exception e11) {
                            e = e11;
                            e.printStackTrace();
                            if (inputStream != null) {
                                inputStream.close();
                            }
                            return null;
                        }
                    } catch (Throwable th2) {
                        th = th2;
                        inputStream2 = inputStream;
                        if (inputStream2 != null) {
                            try {
                                inputStream2.close();
                            } catch (IOException e12) {
                                e12.printStackTrace();
                            }
                        }
                        throw th;
                    }
                }
            } catch (IOException e13) {
                e13.printStackTrace();
            }
        } catch (Exception e14) {
            e = e14;
            inputStream = null;
        } catch (Throwable th3) {
            th = th3;
            if (inputStream2 != null) {
            }
            throw th;
        }
        if (inputStream != null) {
            inputStream.close();
        }
        return null;
    }

    public static InputStream getInputStream(String str) {
        try {
            HttpURLConnection httpURLConnection = (HttpURLConnection) ((URLConnection) FirebasePerfUrlConnection.instrument(new URL(str).openConnection()));
            if (httpURLConnection.getResponseCode() == 200) {
                return httpURLConnection.getInputStream();
            }
            return null;
        } catch (Exception e10) {
            e10.printStackTrace();
            return null;
        }
    }

    public static Bitmap getInstallAppIcon(Context context, String str, int i10, int i11) {
        Drawable drawable;
        PackageManager packageManager = context.getPackageManager();
        try {
            drawable = packageManager.getPackageInfo(str, 0).applicationInfo.loadIcon(packageManager);
        } catch (PackageManager.NameNotFoundException e10) {
            e10.printStackTrace();
            drawable = null;
            return iconToBitmap(drawable, i10, i11);
        } catch (OutOfMemoryError e11) {
            e11.printStackTrace();
            drawable = null;
            return iconToBitmap(drawable, i10, i11);
        }
        return iconToBitmap(drawable, i10, i11);
    }

    public static Bitmap getLocalBitmap(String str) {
        try {
            File file = new File(str);
            if (file.exists()) {
                return BitmapFactory.decodeStream(new FileInputStream(file));
            }
            return null;
        } catch (Exception e10) {
            e10.printStackTrace();
            return null;
        }
    }

    public static Bitmap getSetSizeBitmap(int i10, Bitmap bitmap) {
        int i11;
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        float f10 = (width * 1.0f) / height;
        if (Math.max(width, height) > i10) {
            if (width > height) {
                i11 = (int) ((i10 * 1.0f) / f10);
            } else {
                i11 = i10;
                i10 = (int) (i10 * 1.0f * f10);
            }
            Bitmap createScaledBitmap = Bitmap.createScaledBitmap(bitmap, i10, i11, false);
            bitmap.recycle();
            return createScaledBitmap;
        }
        return bitmap;
    }

    public static Bitmap getThumbnail(String str, int i10, int i11) {
        if (!TextUtils.isEmpty(str) && new File(str).exists()) {
            try {
                Bitmap decodeFile = BitmapFactory.decodeFile(str, computeSampleSize(str, i10, i11));
                if (decodeFile != null) {
                    int readPictureAngle = readPictureAngle(str);
                    Log.d(TAG, "getThumbnail angle:" + readPictureAngle);
                    Matrix matrix = new Matrix();
                    int width = decodeFile.getWidth();
                    int height = decodeFile.getHeight();
                    matrix.setRotate(readPictureAngle, width / 2, height / 2);
                    Bitmap createBitmap = Bitmap.createBitmap(decodeFile, 0, 0, width, height, matrix, true);
                    if (createBitmap != null) {
                        return createBitmap;
                    }
                }
                return decodeFile;
            } catch (Exception unused) {
                return null;
            } catch (OutOfMemoryError e10) {
                e10.printStackTrace();
                return null;
            }
        }
        return null;
    }

    public static Bitmap getThumbnail(String str, long j10) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        File file = new File(str);
        if (file.exists()) {
            try {
                BitmapFactory.Options options = new BitmapFactory.Options();
                long length = file.length();
                int i10 = (int) ((length / j10) + (length % j10 >= j10 / 2 ? 1 : 0));
                options.inSampleSize = i10;
                options.inSampleSize = Math.max(i10, 1);
                options.inDither = false;
                options.inPurgeable = true;
                options.inInputShareable = true;
                Bitmap decodeFile = BitmapFactory.decodeFile(str, options);
                if (decodeFile != null) {
                    int readPictureAngle = readPictureAngle(str);
                    Matrix matrix = new Matrix();
                    int width = decodeFile.getWidth();
                    int height = decodeFile.getHeight();
                    matrix.setRotate(readPictureAngle, width / 2, height / 2);
                    Bitmap createBitmap = Bitmap.createBitmap(decodeFile, 0, 0, width, height, matrix, true);
                    if (createBitmap != null) {
                        return createBitmap;
                    }
                }
                return decodeFile;
            } catch (Exception e10) {
                e10.printStackTrace();
                return null;
            } catch (OutOfMemoryError e11) {
                e11.printStackTrace();
                return null;
            }
        }
        return null;
    }

    public static Bitmap getThumbnailByUri(Uri uri, int i10, int i11) {
        try {
            return Build.VERSION.SDK_INT > 29 ? CommonApplication.getContext().getContentResolver().loadThumbnail(uri, new Size(i10, i11), null) : createSmallBitmap(BitmapFactory.decodeStream(CommonApplication.getContext().getContentResolver().openInputStream(uri)), i10, i11, null);
        } catch (FileNotFoundException e10) {
            e10.printStackTrace();
            return null;
        } catch (IOException e11) {
            e11.printStackTrace();
            return null;
        }
    }

    public static Bitmap getVideoThumbByPath(String str, int i10, int i11, boolean z10) {
        Bitmap videoThumbByPath = getVideoThumbByPath(str, z10);
        Bitmap bitmap = null;
        if (videoThumbByPath != null && (bitmap = createSmallBitmap(videoThumbByPath, i10, i11, null)) != videoThumbByPath) {
            videoThumbByPath.recycle();
        }
        return bitmap;
    }

    public static Bitmap getVideoThumbByPath(String str, boolean z10) {
        return z10 ? ThumbnailUtils.createVideoThumbnail(str, 3) : ThumbnailUtils.createVideoThumbnail(str, 1);
    }

    public static Bitmap handleImageEffect(Bitmap bitmap, int i10, int i11) {
        ColorMatrix colorMatrix = new ColorMatrix();
        Bitmap createBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        ColorMatrix colorMatrix2 = new ColorMatrix();
        colorMatrix2.setSaturation((float) ((i10 * 1.0f) / 100.0d));
        Paint paint = new Paint();
        Canvas canvas = new Canvas(createBitmap);
        float f10 = (float) ((i11 - 127) / 2.5d);
        ColorMatrix colorMatrix3 = new ColorMatrix();
        colorMatrix3.set(new float[]{1.0f, 0.0f, 0.0f, 0.0f, f10, 0.0f, 1.0f, 0.0f, 0.0f, f10, 0.0f, 0.0f, 1.0f, 0.0f, f10, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f});
        colorMatrix.setConcat(colorMatrix2, colorMatrix3);
        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        canvas.drawBitmap(bitmap, 0.0f, 0.0f, paint);
        return createBitmap;
    }

    private static Bitmap iconToBitmap(Drawable drawable, int i10, int i11) {
        Bitmap drawable2Bitmap;
        Bitmap bitmap = null;
        if (drawable == null) {
            return null;
        }
        try {
            drawable2Bitmap = drawable2Bitmap(drawable);
        } catch (OutOfMemoryError e10) {
            e10.printStackTrace();
        }
        if (i10 >= 1 && i11 >= 1) {
            if (drawable2Bitmap != null && (bitmap = createSmallBitmap(drawable2Bitmap, i10, i11, null)) != drawable2Bitmap) {
                drawable2Bitmap.recycle();
            }
            return bitmap;
        }
        return drawable2Bitmap;
    }

    private static boolean isDirInBox(String str) {
        return str.startsWith(CommonApplication.getContext().getCacheDir().getAbsolutePath()) || str.startsWith(CommonApplication.getContext().getFilesDir().getAbsolutePath()) || str.startsWith(CommonApplication.getContext().getExternalCacheDir().getParentFile().getAbsolutePath());
    }

    public static boolean isRecycle(Bitmap bitmap) {
        return bitmap == null || bitmap.isRecycled();
    }

    public static int readPictureAngle(String str) {
        try {
            int attributeInt = new ExifInterface(str).getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
            if (attributeInt != 3) {
                if (attributeInt != 6) {
                    return attributeInt != 8 ? 0 : 270;
                }
                return 90;
            }
            return 180;
        } catch (IOException unused) {
            return 0;
        }
    }

    public static void recycle(Bitmap bitmap) {
        if (bitmap == null || bitmap.isRecycled()) {
            return;
        }
        bitmap.recycle();
    }

    public static String removePngBase64Head(String str) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        return str.startsWith(pngBase64Head) ? str.substring(str.indexOf(pngBase64Head) + 22) : str.startsWith(jpegBase64Head) ? str.substring(str.indexOf(jpegBase64Head) + 23) : str.startsWith(jpgBase64Head) ? str.substring(str.indexOf(jpgBase64Head) + 22) : (str.startsWith(imageBase64Head) && str.contains(",")) ? str.substring(str.indexOf(",") + 1) : str;
    }

    public static Bitmap rotateBitmap(Bitmap bitmap, int i10, boolean z10) {
        if (bitmap == null) {
            return null;
        }
        Matrix matrix = new Matrix();
        matrix.postRotate(i10);
        try {
            Bitmap createBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            if (createBitmap == null) {
                return bitmap;
            }
            if (z10 && createBitmap != bitmap) {
                bitmap.recycle();
            }
            return createBitmap;
        } catch (OutOfMemoryError unused) {
            return bitmap;
        }
    }

    public static void saveBitmap(Bitmap bitmap, String str) {
        saveBitmap(bitmap, str, 100);
    }

    public static void saveBitmap(Bitmap bitmap, String str, int i10) {
        saveBitmap(bitmap, str, i10, Bitmap.CompressFormat.PNG);
    }

    public static void saveBitmap(Bitmap bitmap, String str, int i10, Bitmap.CompressFormat compressFormat) {
        if (Build.VERSION.SDK_INT > 29 && !isDirInBox(str)) {
            String str2 = Environment.DIRECTORY_PICTURES;
            if (str.contains(Environment.DIRECTORY_DCIM)) {
                str2 = Environment.DIRECTORY_DCIM;
            }
            saveBitmapQ(bitmap, new File(str).getName(), str2, i10, compressFormat);
            return;
        }
        FileOutputStream fileOutputStream = null;
        try {
            try {
                try {
                } catch (Exception e10) {
                    e10.printStackTrace();
                    return;
                }
            } catch (Exception e11) {
                e = e11;
            }
            if (bitmap.isRecycled()) {
                return;
            }
            File file = new File(str);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream fileOutputStream2 = new FileOutputStream(file);
            try {
                bitmap.compress(compressFormat, i10, fileOutputStream2);
                fileOutputStream2.flush();
                fileOutputStream2.close();
            } catch (Exception e12) {
                e = e12;
                fileOutputStream = fileOutputStream2;
                Logger.e(e, "saveBitmap ex");
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (Throwable th2) {
                th = th2;
                fileOutputStream = fileOutputStream2;
                if (fileOutputStream != null) {
                    try {
                        fileOutputStream.close();
                    } catch (Exception e13) {
                        e13.printStackTrace();
                    }
                }
                throw th;
            }
        } catch (Throwable th3) {
            th = th3;
        }
    }

    public static boolean saveBitmapQ(Bitmap bitmap, String str, String str2, int i10) {
        return saveBitmapQ(bitmap, str, str2, i10, Bitmap.CompressFormat.PNG);
    }

    public static boolean saveBitmapQ(Bitmap bitmap, String str, String str2, int i10, Bitmap.CompressFormat compressFormat) {
        try {
            ContentValues contentValues = new ContentValues();
            contentValues.put("_display_name", str);
            String str3 = null;
            if (compressFormat == Bitmap.CompressFormat.JPEG) {
                str3 = "image/jepg";
            } else if (compressFormat == Bitmap.CompressFormat.PNG) {
                str3 = "image/png";
            } else if (compressFormat == Bitmap.CompressFormat.WEBP) {
                str3 = "image/webp";
            }
            contentValues.put("mime_type", str3);
            contentValues.put("relative_path", str2);
            ContentResolver contentResolver = CommonApplication.getContext().getContentResolver();
            Uri insert = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
            if (insert != null) {
                OutputStream openOutputStream = contentResolver.openOutputStream(insert);
                bitmap.compress(compressFormat, i10, openOutputStream);
                openOutputStream.flush();
                return true;
            }
            return false;
        } catch (Exception e10) {
            Logger.e(e10, "saveBitmapQ error");
            return false;
        }
    }

    public static void saveBitmapToBMP(Bitmap bitmap, String str) {
        if (bitmap != null) {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            int[] iArr = new int[width * height];
            bitmap.getPixels(iArr, 0, width, 0, 0, width, height);
            byte[] addBMP_RGB_888 = addBMP_RGB_888(iArr, width, height);
            byte[] addBMPImageHeader = addBMPImageHeader(addBMP_RGB_888.length);
            byte[] addBMPImageInfosHeader = addBMPImageInfosHeader(width, height);
            byte[] bArr = new byte[addBMP_RGB_888.length + 54];
            System.arraycopy(addBMPImageHeader, 0, bArr, 0, addBMPImageHeader.length);
            System.arraycopy(addBMPImageInfosHeader, 0, bArr, 14, addBMPImageInfosHeader.length);
            System.arraycopy(addBMP_RGB_888, 0, bArr, 54, addBMP_RGB_888.length);
            try {
                new FileOutputStream(str).write(bArr);
            } catch (FileNotFoundException e10) {
                e10.printStackTrace();
            } catch (IOException e11) {
                e11.printStackTrace();
            }
        }
    }

    public static void saveBitmapWithDpi(Bitmap bitmap, String str, int i10) {
        FileOutputStream fileOutputStream = null;
        try {
            try {
                try {
                } catch (Throwable th2) {
                    th = th2;
                }
            } catch (Exception e10) {
                e = e10;
            }
            if (bitmap.isRecycled()) {
                return;
            }
            FileOutputStream fileOutputStream2 = new FileOutputStream(str);
            try {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                Bitmap.CompressFormat compressFormat = Bitmap.CompressFormat.JPEG;
                bitmap.compress(compressFormat, 100, byteArrayOutputStream);
                byte[] byteArray = byteArrayOutputStream.toByteArray();
                setDpi(byteArray, i10);
                fileOutputStream2.write(byteArray);
                fileOutputStream2.flush();
                fileOutputStream2.close();
                fileOutputStream = compressFormat;
            } catch (Exception e11) {
                e = e11;
                fileOutputStream = fileOutputStream2;
                Logger.e(e, "saveBitmap ex");
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                    fileOutputStream = fileOutputStream;
                }
            } catch (Throwable th3) {
                th = th3;
                fileOutputStream = fileOutputStream2;
                if (fileOutputStream != null) {
                    try {
                        fileOutputStream.close();
                    } catch (Exception e12) {
                        e12.printStackTrace();
                    }
                }
                throw th;
            }
        } catch (Exception e13) {
            e13.printStackTrace();
        }
    }

    public static Bitmap scaleBitmap(Bitmap bitmap, int i10, int i11) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        float min = Math.min(i10 / width, i11 / height);
        Matrix matrix = new Matrix();
        matrix.postScale(min, min);
        return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    }

    private static void setDpi(byte[] bArr, int i10) {
        bArr[13] = 1;
        byte b10 = (byte) (i10 >> 8);
        bArr[14] = b10;
        byte b11 = (byte) (i10 & 255);
        bArr[15] = b11;
        bArr[16] = b10;
        bArr[17] = b11;
    }
}