TIM v2.3.1版本的 MD5 值为:d6957e3ee7ce901ccc491cefdea0da8c

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


package cooperation.qzone.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Build;
import android.text.TextUtils;
import com.tencent.component.network.utils.Base64;
import com.tencent.image.JpegExifReader;
import com.tencent.qphone.base.util.QLog;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.json.JSONArray;
public class GifCoder {
    private static final String f98674a = "GifCoder";
    private static boolean f70781a;
    private long f70783a;
    private OnEncodeGifFinishedListener f70785a;
    private boolean f70786b;
    private boolean f70787c;
    private int f70782a = gifCoderWnsConfig.b();
    private final int f98675b = 0;
    private final int f98676c = 1;
    private final int d = 2;
    private int e = 0;
    private boolean f70788d = true;
    private EncodingType f70784a = EncodingType.ENCODING_TYPE_SIMPLE_FAST;

    public enum EncodingType {
        ENCODING_TYPE_SIMPLE_FAST,
        ENCODING_TYPE_NORMAL_LOW_MEMORY,
        ENCODING_TYPE_STABLE_HIGH_MEMORY
    }

    public interface OnEncodeGifFinishedListener {
        void a();

        void a(String str);
    }

    private native boolean nativeEncodeFrame(long j, Bitmap bitmap, int i);

    private native void nativeEncoderClose(long j);

    private native long nativeEncoderInit(int i, int i2, String str, int i3);

    private native void nativeEncoderSetDither(long j, boolean z);

    static {
        a();
    }

    public static void a() {
        if (AlbumLibDownloaderUtil.a().a(AlbumLibDownloaderUtil.f70755a, true)) {
            m11072a(AlbumLibDownloaderUtil.a().b(AlbumLibDownloaderUtil.f70755a));
        } else {
            AlbumLibDownloaderUtil.a().m11049a();
        }
    }

    public void a(int i) {
        this.f70782a = i;
    }

    public void a(boolean z) {
        this.f70786b = z;
    }

    private static synchronized boolean m11072a(String str) {
        boolean z;
        synchronized (GifCoder.class) {
            QLog.i(f98674a, 1, "gif lib start load");
            System.load(str);
            QLog.i(f98674a, 1, "gif lib load success");
            f70781a = true;
            z = f70781a;
        }
        return z;
    }

    public void b(boolean z) {
        this.f70787c = z;
    }

    public void a(EncodingType encodingType) {
        this.f70784a = encodingType;
    }

    public void finalize() {
        super.finalize();
        b();
    }

    public void a(int i, int i2, String str) {
        if (!f70781a) {
            a();
            if (!f70781a) {
                return;
            }
        }
        a(i, i2, str, EncodingType.ENCODING_TYPE_SIMPLE_FAST);
    }

    public void a(int i, int i2, String str, EncodingType encodingType) {
        if (!f70781a) {
            a();
            if (!f70781a) {
                return;
            }
        }
        if (0 != this.f70783a) {
            b();
        }
        QLog.i(f98674a, 1, "start nativeEncoderInit");
        this.f70783a = nativeEncoderInit(i, i2, str, encodingType.ordinal());
        QLog.i(f98674a, 1, "nativeEncoderInit success");
        if (0 == this.f70783a) {
            throw new FileNotFoundException();
        }
        QLog.i(f98674a, 1, "start nativeEncoderSetDither");
        nativeEncoderSetDither(this.f70783a, this.f70788d);
        QLog.i(f98674a, 1, "nativeEncoderSetDither success");
    }

    public void b() {
        if (f70781a && this.f70783a != 0) {
            QLog.i(f98674a, 1, "start close gifEncoder");
            nativeEncoderClose(this.f70783a);
            QLog.i(f98674a, 1, "close gifEncoder success");
            this.f70783a = 0L;
        }
    }

    public void c(boolean z) {
        if (f70781a) {
            this.f70788d = z;
        }
    }

    public boolean m11073a(Bitmap bitmap, int i) {
        if (f70781a && 0 != this.f70783a) {
            QLog.i(f98674a, 1, "start encodeFrame");
            nativeEncodeFrame(this.f70783a, bitmap, i);
            QLog.i(f98674a, 1, "encodeFrame success");
            return true;
        }
        return false;
    }

    private Bitmap a(Bitmap bitmap) {
        if (bitmap == null) {
            return null;
        }
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        if (width <= height) {
            if (this.e == 1 || this.e == 0) {
                this.e = 1;
                return bitmap;
            }
            return null;
        } else if (width > height) {
            if (this.e == 2 || this.e == 0) {
                this.e = 2;
                return bitmap;
            }
            return null;
        } else {
            return bitmap;
        }
    }

    private static boolean m11070a() {
        boolean[] zArr = {false};
        CountDownLatch countDownLatch = new CountDownLatch(1);
        AlbumLibDownloaderUtil.a().b(new ygq(zArr, countDownLatch));
        try {
            countDownLatch.await(30L, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            zArr[0] = false;
        }
        return zArr[0];
    }

    public boolean a(Bitmap bitmap, String str, int i) {
        return a(bitmap, str, i, false);
    }

    public boolean a(Bitmap bitmap, String str, int i, boolean z) {
        if (bitmap == null) {
            return false;
        }
        if (!f70781a) {
            if (z) {
                if (m11070a()) {
                    a();
                }
            } else {
                a();
            }
            if (!f70781a) {
                QLog.w(f98674a, 1, "gif lib loaded failed 1");
                return false;
            }
        }
        if (this.f70787c) {
            if (m11071a(bitmap)) {
                bitmap = a(bitmap, 100);
            }
        } else {
            bitmap = a(bitmap, this.f70782a, this.f70782a, 0, this.f70786b);
        }
        if (this.f70783a == 0) {
            try {
                a(bitmap.getWidth(), bitmap.getHeight(), str, this.f70784a);
            } catch (Throwable th) {
                th.printStackTrace();
                File file = new File(str);
                if (file.exists()) {
                    file.delete();
                }
                QLog.i(f98674a, 1, "initEncoder happen exception");
                return false;
            }
        }
        QLog.i(f98674a, 1, "encode gif ,frame time  = " + i);
        return m11073a(bitmap, i);
    }

    static boolean a(Bitmap bitmap, BitmapFactory.Options options) {
        if (bitmap == null || !bitmap.isMutable() || bitmap.isRecycled()) {
            return false;
        }
        return Build.VERSION.SDK_INT >= 19 ? ((options.outWidth / options.inSampleSize) * (options.outHeight / options.inSampleSize)) * a(bitmap.getConfig()) <= bitmap.getAllocationByteCount() : bitmap.getWidth() == options.outWidth && bitmap.getHeight() == options.outHeight && options.inSampleSize == 1;
    }

    static int a(Bitmap.Config config) {
        if (config == Bitmap.Config.ARGB_8888) {
            return 4;
        }
        if (config == Bitmap.Config.RGB_565 || config == Bitmap.Config.ARGB_4444) {
            return 2;
        }
        return config == Bitmap.Config.ALPHA_8 ? 1 : 1;
    }

    private static Bitmap b(String str, int i, int i2, boolean z, Bitmap bitmap) {
        int i3;
        Bitmap bitmap2 = null;
        if (str == null || "".equals(str) || str.length() <= 0 || TextUtils.isEmpty(str)) {
            return null;
        }
        try {
            byte[] decode = Base64.decode(str, 0);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeByteArray(decode, 0, decode.length, options);
            options.inJustDecodeBounds = false;
            if (i == 0 || i2 == 0) {
                i3 = 1;
            } else if (z) {
                i3 = Math.max(options.outWidth / i, options.outHeight / i2);
            } else {
                i3 = Math.min(options.outWidth / i, options.outHeight / i2);
            }
            if (i3 < 1) {
                i3 = 1;
            }
            options.inSampleSize = i3;
            options.inMutable = true;
            if (Build.VERSION.SDK_INT >= 11 && a(bitmap, options)) {
                options.inBitmap = bitmap;
            }
            bitmap2 = BitmapFactory.decodeByteArray(decode, 0, decode.length, options);
            return bitmap2;
        } catch (IllegalArgumentException e) {
            QLog.w(f98674a, 1, "", e);
            return b(str, i, i2, z, bitmap2);
        } catch (OutOfMemoryError e2) {
            QLog.e(f98674a, 1, "", e2);
            return bitmap2;
        } catch (Throwable th) {
            QLog.e(f98674a, 1, "", th);
            return bitmap2;
        }
    }

    private String m11069a(String str) {
        int indexOf;
        if (str == null) {
            return null;
        }
        if (str.startsWith("data:image") && (indexOf = str.indexOf("base64,")) >= 0 && "base64,".length() + indexOf < str.length()) {
            return str.substring("base64,".length() + indexOf);
        }
        return str;
    }

    public boolean a(String str, JSONArray jSONArray, int i, boolean z) {
        Bitmap b2;
        if (!f70781a) {
            a();
            if (!f70781a) {
                QLog.w(f98674a, 1, "gif lib loaded failed 2");
                return false;
            }
        }
        this.e = 0;
        Bitmap bitmap = null;
        int i2 = 0;
        boolean z2 = false;
        while (i2 < jSONArray.length()) {
            String m11069a = m11069a(jSONArray.optString(i2));
            if (z) {
                b2 = b(m11069a, 0, 0, this.f70786b, bitmap);
            } else {
                b2 = b(m11069a, this.f70782a, this.f70782a, this.f70786b, bitmap);
            }
            if (!z2 && b2 != null) {
                try {
                    a(b2.getWidth(), b2.getHeight(), str, this.f70784a);
                    z2 = true;
                } catch (Exception e) {
                    File file = new File(str);
                    if (file.exists()) {
                        file.delete();
                    }
                    QLog.i(f98674a, 1, "initEncoder happen exception");
                    return false;
                }
            }
            m11073a(b2, i);
            if (bitmap != null) {
                b2 = bitmap;
            }
            i2++;
            bitmap = b2;
        }
        b();
        return true;
    }

    public boolean a(String str, ArrayList arrayList, int i) {
        return a(str, arrayList, i, false);
    }

    public boolean a(String str, ArrayList arrayList, int i, boolean z) {
        Bitmap a2;
        boolean z2;
        if (!f70781a) {
            if (z) {
                if (m11070a()) {
                    a();
                }
            } else {
                a();
            }
            if (!f70781a) {
                QLog.w(f98674a, 1, "gif lib loaded failed 3");
                return false;
            }
        }
        this.e = 0;
        Iterator it = arrayList.iterator();
        Bitmap bitmap = null;
        boolean z3 = false;
        while (it.hasNext()) {
            String str2 = (String) it.next();
            if (this.f70787c) {
                a2 = a(str2, bitmap);
            } else {
                a2 = a(str2, this.f70782a, this.f70782a, this.f70786b, bitmap);
            }
            Bitmap bitmap2 = bitmap == null ? a2 : bitmap;
            int a3 = a(str2);
            if (!this.f70787c) {
                a2 = a(a2, this.f70782a, this.f70782a, a3, this.f70786b);
            }
            Bitmap a4 = a(a2);
            if (a4 == null) {
                File file = new File(str);
                if (file.exists()) {
                    file.delete();
                }
                QLog.i(f98674a, 1, "decode bitmap is NULL,decode pic = " + str2);
                bitmap = bitmap2;
            } else {
                if (z3) {
                    z2 = z3;
                } else {
                    try {
                        a(a4.getWidth(), a4.getHeight(), str, this.f70784a);
                        z2 = true;
                    } catch (Exception e) {
                        e.printStackTrace();
                        File file2 = new File(str);
                        if (file2.exists()) {
                            file2.delete();
                        }
                        QLog.i(f98674a, 1, "initEncoder happen exception");
                        return false;
                    }
                }
                QLog.i(f98674a, 1, "encode gif ,frame time  = " + i + ",pics = " + arrayList.size());
                m11073a(a4, i);
                bitmap = bitmap2;
                z3 = z2;
            }
        }
        b();
        return true;
    }

    public static Bitmap a(String str, Bitmap bitmap) {
        return a(str, 0, 0, false, bitmap);
    }

    public static int a(String str) {
        try {
            switch (JpegExifReader.readOrientation(str)) {
                case 3:
                    return 180;
                case 4:
                case 5:
                case 7:
                default:
                    return 0;
                case 6:
                    return 90;
                case 8:
                    return 270;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static boolean m11071a(Bitmap bitmap) {
        if (bitmap == null || bitmap.getWidth() <= 2 || bitmap.getHeight() <= 2) {
            return false;
        }
        try {
            return (bitmap.getPixel(1, 1) >> 24) == 0 && (bitmap.getPixel(bitmap.getWidth() + (-2), bitmap.getHeight() + (-2)) >> 24) == 0;
        } catch (Exception e) {
            return true;
        }
    }

    public static Bitmap a(Bitmap bitmap, int i) {
        if (bitmap == null) {
            return null;
        }
        int[] iArr = new int[bitmap.getWidth() * bitmap.getHeight()];
        bitmap.getPixels(iArr, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
        int i2 = (i * 255) / 100;
        for (int i3 = 0; i3 < iArr.length; i3++) {
            int i4 = (iArr[i3] >> 24) & 255;
            int i5 = iArr[i3] & 255;
            int i6 = (iArr[i3] >> 8) & 255;
            int i7 = (iArr[i3] >> 16) & 255;
            if (i7 == 0 && i6 == 0 && i7 == 0 && i4 == 0) {
                iArr[i3] = 16777215;
                iArr[i3] = (i2 << 24) | (iArr[i3] & 16777215);
            }
        }
        return Bitmap.createBitmap(iArr, bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    }

    public static Bitmap a(String str, int i, int i2, boolean z, Bitmap bitmap) {
        BitmapFactory.Options options;
        int i3;
        Bitmap bitmap2 = null;
        if (str == null || "".equals(str) || str.length() <= 0 || TextUtils.isEmpty(str) || !new File(str).exists()) {
            return null;
        }
        try {
            options = new BitmapFactory.Options();
        } catch (Throwable th) {
            options = null;
        }
        try {
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(str, options);
            options.inJustDecodeBounds = false;
            if (i == 0 || i2 == 0) {
                i3 = 1;
            } else if (z) {
                i3 = Math.max(options.outWidth / i, options.outHeight / i2);
            } else {
                i3 = Math.min(options.outWidth / i, options.outHeight / i2);
            }
            options.inSampleSize = i3;
            options.inMutable = true;
            if (Build.VERSION.SDK_INT >= 11 && bitmap != null) {
                options.inBitmap = bitmap;
            }
            bitmap2 = BitmapFactory.decodeStream(new BufferedInputStream(new FileInputStream(str), 4096), null, options);
            return bitmap2;
        } catch (Throwable th2) {
            System.gc();
            try {
                return BitmapFactory.decodeFile(str, options);
            } catch (Throwable th3) {
                QLog.i(f98674a, 1, "decodeBitmapFromFile happen exception");
                System.gc();
                return bitmap2;
            }
        }
    }

    public static Bitmap a(Bitmap bitmap, double d, double d2, int i, boolean z) {
        Bitmap bitmap2;
        if (bitmap == null) {
            QLog.i(f98674a, 1, "need zoom bgimage is null");
            return null;
        }
        float width = bitmap.getWidth();
        float height = bitmap.getHeight();
        Matrix matrix = new Matrix();
        float f = ((float) d) / width;
        float f2 = ((float) d2) / height;
        if (z) {
            if (f <= f2) {
                f2 = f;
            }
        } else {
            if (f <= f2) {
                f = f2;
            }
            f2 = f;
        }
        matrix.postScale(f2, f2);
        matrix.postRotate(i);
        try {
            bitmap2 = Bitmap.createBitmap(bitmap, 0, 0, (int) width, (int) height, matrix, true);
        } catch (Throwable th) {
            System.gc();
            try {
                bitmap2 = Bitmap.createBitmap(bitmap, 0, 0, (int) width, (int) height, matrix, true);
            } catch (Throwable th2) {
                QLog.i(f98674a, 1, "zoomImage happen exception");
                bitmap2 = null;
                System.gc();
            }
        }
        if (m11071a(bitmap2)) {
            return a(bitmap2, 100);
        }
        return bitmap2;
    }
}