MD5 校验值:c164745004c28d9e6aebd8141ed3fb4d
ImageUtils.java 文件包含反编译后的源代码,请注意,该内容仅供学习和参考使用,不得用于非法用途。
package com.qinyue.vcommon.utils; import android.app.Activity; import android.content.Context; import android.content.ContextWrapper; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.ColorMatrix; import android.graphics.ColorMatrixColorFilter; import android.graphics.LinearGradient; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffColorFilter; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.Shader; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.media.ExifInterface; import android.os.Build; import android.renderscript.Allocation; import android.renderscript.Element; import android.renderscript.RenderScript; import android.renderscript.ScriptIntrinsicBlur; import android.view.View; import android.widget.ImageView; import androidx.core.content.ContextCompat; import androidx.core.view.MotionEventCompat; import androidx.core.view.ViewCompat; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileDescriptor; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.lang.reflect.Array; public final class ImageUtils { private ImageUtils() { throw new UnsupportedOperationException("u can't instantiate me..."); } public static byte[] bitmap2Bytes(Bitmap bitmap, Bitmap.CompressFormat compressFormat) { if (bitmap == null) { return null; } ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); bitmap.compress(compressFormat, 100, byteArrayOutputStream); return byteArrayOutputStream.toByteArray(); } public static Bitmap bytes2Bitmap(byte[] bArr) { if (bArr == null || bArr.length == 0) { return null; } return BitmapFactory.decodeByteArray(bArr, 0, bArr.length); } public static Bitmap drawable2Bitmap(Drawable drawable) { Bitmap.Config config; Bitmap createBitmap; Bitmap.Config config2; if (drawable instanceof BitmapDrawable) { BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; if (bitmapDrawable.getBitmap() != null) { return bitmapDrawable.getBitmap(); } } if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) { if (drawable.getOpacity() != -1) { config = Bitmap.Config.ARGB_8888; } else { config = Bitmap.Config.RGB_565; } createBitmap = Bitmap.createBitmap(1, 1, config); } else { int intrinsicWidth = drawable.getIntrinsicWidth(); int intrinsicHeight = drawable.getIntrinsicHeight(); if (drawable.getOpacity() != -1) { config2 = Bitmap.Config.ARGB_8888; } else { config2 = Bitmap.Config.RGB_565; } createBitmap = Bitmap.createBitmap(intrinsicWidth, intrinsicHeight, config2); } Canvas canvas = new Canvas(createBitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return createBitmap; } public static Drawable bitmap2Drawable(Bitmap bitmap) { if (bitmap == null) { return null; } return new BitmapDrawable(XUtil.getContext().getResources(), bitmap); } public static byte[] drawable2Bytes(Drawable drawable, Bitmap.CompressFormat compressFormat) { if (drawable == null) { return null; } return bitmap2Bytes(drawable2Bitmap(drawable), compressFormat); } public static Drawable bytes2Drawable(byte[] bArr) { return bitmap2Drawable(bytes2Bitmap(bArr)); } public static Bitmap view2Bitmap(View view) { if (view == null) { return null; } Bitmap createBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(createBitmap); Drawable background = view.getBackground(); if (background != null) { background.draw(canvas); } else { canvas.drawColor(-1); } view.draw(canvas); return createBitmap; } public static Bitmap getBitmap(File file) { if (file == null) { return null; } return BitmapFactory.decodeFile(file.getAbsolutePath()); } public static Bitmap getBitmap(File file, int i, int i2) { if (file == null) { return null; } BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(file.getAbsolutePath(), options); options.inSampleSize = calculateInSampleSize(options, i, i2); options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(file.getAbsolutePath(), options); } public static Bitmap getBitmap(String str) { if (isSpace(str)) { return null; } return BitmapFactory.decodeFile(str); } public static Bitmap getBitmap(String str, int i, int i2) { if (isSpace(str)) { return null; } BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(str, options); options.inSampleSize = calculateInSampleSize(options, i, i2); options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(str, options); } public static Bitmap getBitmap(InputStream inputStream) { if (inputStream == null) { return null; } return BitmapFactory.decodeStream(inputStream); } public static Bitmap getBitmap(InputStream inputStream, int i, int i2) { if (inputStream == null) { return null; } return getBitmap(input2Byte(inputStream), 0, i, i2); } public static Bitmap getBitmap(byte[] bArr, int i) { if (bArr.length == 0) { return null; } return BitmapFactory.decodeByteArray(bArr, i, bArr.length); } public static Bitmap getBitmap(byte[] bArr, int i, int i2, int i3) { if (bArr.length == 0) { return null; } BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeByteArray(bArr, i, bArr.length, options); options.inSampleSize = calculateInSampleSize(options, i2, i3); options.inJustDecodeBounds = false; return BitmapFactory.decodeByteArray(bArr, i, bArr.length, options); } public static Bitmap getBitmap(int i) { Drawable drawable = ContextCompat.getDrawable(XUtil.getContext(), i); if (drawable == null) { return null; } Canvas canvas = new Canvas(); Bitmap createBitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); canvas.setBitmap(createBitmap); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas); return createBitmap; } public static Bitmap getBitmap(int i, int i2, int i3) { BitmapFactory.Options options = new BitmapFactory.Options(); Resources resources = XUtil.getContext().getResources(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(resources, i, options); options.inSampleSize = calculateInSampleSize(options, i2, i3); options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(resources, i, options); } public static Bitmap getBitmap(FileDescriptor fileDescriptor) { if (fileDescriptor == null) { return null; } return BitmapFactory.decodeFileDescriptor(fileDescriptor); } public static Bitmap getBitmap(FileDescriptor fileDescriptor, int i, int i2) { if (fileDescriptor == null) { return null; } BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); options.inSampleSize = calculateInSampleSize(options, i, i2); options.inJustDecodeBounds = false; return BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); } public static void clearImgMemory(ImageView imageView) { Bitmap bitmap; if (imageView == null) { return; } Drawable drawable = imageView.getDrawable(); if (drawable != null && (drawable instanceof BitmapDrawable) && (bitmap = ((BitmapDrawable) drawable).getBitmap()) != null && !bitmap.isRecycled()) { bitmap.recycle(); } imageView.setImageBitmap(null); if (drawable != null) { drawable.setCallback(null); } } public static Bitmap drawColor(Bitmap bitmap, int i) { return drawColor(bitmap, i, false); } public static Bitmap drawColor(Bitmap bitmap, int i, boolean z) { if (isEmptyBitmap(bitmap)) { return null; } if (!z) { bitmap = bitmap.copy(bitmap.getConfig(), true); } new Canvas(bitmap).drawColor(i, PorterDuff.Mode.DARKEN); return bitmap; } public static Bitmap scale(Bitmap bitmap, int i, int i2) { return scale(bitmap, i, i2, false); } public static Bitmap scale(Bitmap bitmap, int i, int i2, boolean z) { if (isEmptyBitmap(bitmap)) { return null; } Bitmap createScaledBitmap = Bitmap.createScaledBitmap(bitmap, i, i2, true); if (z && !bitmap.isRecycled()) { bitmap.recycle(); } return createScaledBitmap; } public static Bitmap scale(Bitmap bitmap, float f, float f2) { return scale(bitmap, f, f2, false); } public static Bitmap scale(Bitmap bitmap, float f, float f2, boolean z) { if (isEmptyBitmap(bitmap)) { return null; } Matrix matrix = new Matrix(); matrix.setScale(f, f2); Bitmap createBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); if (z && !bitmap.isRecycled()) { bitmap.recycle(); } return createBitmap; } public static Bitmap clip(Bitmap bitmap, int i, int i2, int i3, int i4) { return clip(bitmap, i, i2, i3, i4, false); } public static Bitmap clip(Bitmap bitmap, int i, int i2, int i3, int i4, boolean z) { if (isEmptyBitmap(bitmap)) { return null; } Bitmap createBitmap = Bitmap.createBitmap(bitmap, i, i2, i3, i4); if (z && !bitmap.isRecycled()) { bitmap.recycle(); } return createBitmap; } public static Bitmap skew(Bitmap bitmap, float f, float f2) { return skew(bitmap, f, f2, 0.0f, 0.0f, false); } public static Bitmap skew(Bitmap bitmap, float f, float f2, boolean z) { return skew(bitmap, f, f2, 0.0f, 0.0f, z); } public static Bitmap skew(Bitmap bitmap, float f, float f2, float f3, float f4) { return skew(bitmap, f, f2, f3, f4, false); } public static Bitmap skew(Bitmap bitmap, float f, float f2, float f3, float f4, boolean z) { if (isEmptyBitmap(bitmap)) { return null; } Matrix matrix = new Matrix(); matrix.setSkew(f, f2, f3, f4); Bitmap createBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); if (z && !bitmap.isRecycled()) { bitmap.recycle(); } return createBitmap; } public static Bitmap rotate(Bitmap bitmap, int i, float f, float f2) { return rotate(bitmap, i, f, f2, false); } public static Bitmap rotate(Bitmap bitmap, int i, float f, float f2, boolean z) { if (isEmptyBitmap(bitmap)) { return null; } if (i == 0) { return bitmap; } Matrix matrix = new Matrix(); matrix.setRotate(i, f, f2); Bitmap createBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); if (z && !bitmap.isRecycled()) { bitmap.recycle(); } return createBitmap; } public static int getRotateDegree(String str) { try { int attributeInt = new ExifInterface(str).getAttributeInt(androidx.exifinterface.media.ExifInterface.TAG_ORIENTATION, 1); if (attributeInt == 3) { return 180; } if (attributeInt != 6) { return attributeInt != 8 ? 0 : 270; } return 90; } catch (IOException e) { e.printStackTrace(); return -1; } } public static Bitmap toRound(Bitmap bitmap) { return toRound(bitmap, 0, 0, false); } public static Bitmap toRound(Bitmap bitmap, boolean z) { return toRound(bitmap, 0, 0, z); } public static Bitmap toRound(Bitmap bitmap, int i, int i2) { return toRound(bitmap, i, i2, false); } public static Bitmap toRound(Bitmap bitmap, int i, int i2, boolean z) { if (isEmptyBitmap(bitmap)) { return null; } int width = bitmap.getWidth(); int height = bitmap.getHeight(); int min = Math.min(width, height); Paint paint = new Paint(1); Bitmap createBitmap = Bitmap.createBitmap(width, height, bitmap.getConfig()); float f = min; float f2 = f / 2.0f; float f3 = width; float f4 = height; RectF rectF = new RectF(0.0f, 0.0f, f3, f4); rectF.inset((width - min) / 2.0f, (height - min) / 2.0f); Matrix matrix = new Matrix(); matrix.setTranslate(rectF.left, rectF.top); matrix.preScale(f / f3, f / f4); BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); bitmapShader.setLocalMatrix(matrix); paint.setShader(bitmapShader); Canvas canvas = new Canvas(createBitmap); canvas.drawRoundRect(rectF, f2, f2, paint); if (i > 0) { paint.setShader(null); paint.setColor(i2); paint.setStyle(Paint.Style.STROKE); float f5 = i; paint.setStrokeWidth(f5); canvas.drawCircle(f3 / 2.0f, f4 / 2.0f, f2 - (f5 / 2.0f), paint); } if (z && !bitmap.isRecycled()) { bitmap.recycle(); } return createBitmap; } public static Bitmap toRoundCorner(Bitmap bitmap, float f) { return toRoundCorner(bitmap, f, 0, 0, false); } public static Bitmap toRoundCorner(Bitmap bitmap, float f, boolean z) { return toRoundCorner(bitmap, f, 0, 0, z); } public static Bitmap toRoundCorner(Bitmap bitmap, float f, int i, int i2) { return toRoundCorner(bitmap, f, i, i2, false); } public static Bitmap toRoundCorner(Bitmap bitmap, float f, int i, int i2, boolean z) { if (isEmptyBitmap(bitmap)) { return null; } int width = bitmap.getWidth(); int height = bitmap.getHeight(); Paint paint = new Paint(1); Bitmap createBitmap = Bitmap.createBitmap(width, height, bitmap.getConfig()); paint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)); Canvas canvas = new Canvas(createBitmap); RectF rectF = new RectF(0.0f, 0.0f, width, height); float f2 = i; float f3 = f2 / 2.0f; rectF.inset(f3, f3); canvas.drawRoundRect(rectF, f, f, paint); if (i > 0) { paint.setShader(null); paint.setColor(i2); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(f2); paint.setStrokeCap(Paint.Cap.ROUND); canvas.drawRoundRect(rectF, f, f, paint); } if (z && !bitmap.isRecycled()) { bitmap.recycle(); } return createBitmap; } public static Bitmap addCornerBorder(Bitmap bitmap, int i, int i2, float f) { return addBorder(bitmap, i, i2, false, f, false); } public static Bitmap addCornerBorder(Bitmap bitmap, int i, int i2, float f, boolean z) { return addBorder(bitmap, i, i2, false, f, z); } public static Bitmap addCircleBorder(Bitmap bitmap, int i, int i2) { return addBorder(bitmap, i, i2, true, 0.0f, false); } public static Bitmap addCircleBorder(Bitmap bitmap, int i, int i2, boolean z) { return addBorder(bitmap, i, i2, true, 0.0f, z); } private static Bitmap addBorder(Bitmap bitmap, int i, int i2, boolean z, float f, boolean z2) { if (isEmptyBitmap(bitmap)) { return null; } if (!z2) { bitmap = bitmap.copy(bitmap.getConfig(), true); } int width = bitmap.getWidth(); int height = bitmap.getHeight(); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(1); paint.setColor(i2); paint.setStyle(Paint.Style.STROKE); float f2 = i; paint.setStrokeWidth(f2); if (z) { canvas.drawCircle(width / 2.0f, height / 2.0f, (Math.min(width, height) / 2.0f) - (f2 / 2.0f), paint); } else { float f3 = i >> 1; canvas.drawRoundRect(new RectF(f3, f3, width - r5, height - r5), f, f, paint); } return bitmap; } public static Bitmap addReflection(Bitmap bitmap, int i) { return addReflection(bitmap, i, false); } public static Bitmap addReflection(Bitmap bitmap, int i, boolean z) { if (isEmptyBitmap(bitmap)) { return null; } int width = bitmap.getWidth(); int height = bitmap.getHeight(); Matrix matrix = new Matrix(); matrix.preScale(1.0f, -1.0f); Bitmap createBitmap = Bitmap.createBitmap(bitmap, 0, height - i, width, i, matrix, false); Bitmap createBitmap2 = Bitmap.createBitmap(width, height + i, bitmap.getConfig()); Canvas canvas = new Canvas(createBitmap2); canvas.drawBitmap(bitmap, 0.0f, 0.0f, (Paint) null); float f = height + 0; canvas.drawBitmap(createBitmap, 0.0f, f, (Paint) null); Paint paint = new Paint(1); paint.setShader(new LinearGradient(0.0f, height, 0.0f, createBitmap2.getHeight() + 0, 1895825407, ViewCompat.MEASURED_SIZE_MASK, Shader.TileMode.MIRROR)); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); canvas.drawRect(0.0f, f, width, createBitmap2.getHeight(), paint); if (!createBitmap.isRecycled()) { createBitmap.recycle(); } if (z && !bitmap.isRecycled()) { bitmap.recycle(); } return createBitmap2; } public static Bitmap addBitmapHorizontal(Bitmap bitmap, Bitmap bitmap2, int i, int i2) { int width = bitmap.getWidth() + bitmap2.getWidth() + i; int max = Math.max(bitmap.getHeight(), bitmap2.getHeight()); Bitmap createBitmap = Bitmap.createBitmap(width, max, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(createBitmap); canvas.drawBitmap(bitmap, 0.0f, 0.0f, (Paint) null); float f = i / 2.0f; canvas.drawLine(bitmap.getWidth() + f, 0.0f, bitmap.getWidth() + f, max, getDividerLinePaint(i, i2)); canvas.drawBitmap(bitmap2, bitmap.getWidth() + i, 0.0f, (Paint) null); return createBitmap; } public static Bitmap addBitmapVertical(Bitmap bitmap, Bitmap bitmap2, int i, int i2) { int max = Math.max(bitmap.getWidth(), bitmap2.getWidth()); Bitmap createBitmap = Bitmap.createBitmap(max, bitmap.getHeight() + bitmap2.getHeight() + i, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(createBitmap); canvas.drawBitmap(bitmap, 0.0f, 0.0f, (Paint) null); float f = i / 2.0f; canvas.drawLine(0.0f, bitmap.getHeight() + f, max, bitmap.getHeight() + f, getDividerLinePaint(i, i2)); canvas.drawBitmap(bitmap2, 0.0f, bitmap.getHeight() + i, (Paint) null); return createBitmap; } private static Paint getDividerLinePaint(int i, int i2) { Paint paint = new Paint(); paint.setColor(i2); paint.setStrokeWidth(i); return paint; } public static Bitmap addTextWatermark(Bitmap bitmap, String str, int i, int i2, float f, float f2) { return addTextWatermark(bitmap, str, i, i2, f, f2, false); } public static Bitmap addTextWatermark(Bitmap bitmap, String str, float f, int i, float f2, float f3, boolean z) { if (isEmptyBitmap(bitmap) || str == null) { return null; } Bitmap copy = bitmap.copy(bitmap.getConfig(), true); Paint paint = new Paint(1); Canvas canvas = new Canvas(copy); paint.setColor(i); paint.setTextSize(f); paint.getTextBounds(str, 0, str.length(), new Rect()); canvas.drawText(str, f2, f3 + f, paint); if (z && !bitmap.isRecycled()) { bitmap.recycle(); } return copy; } public static Bitmap addImageWatermark(Bitmap bitmap, Bitmap bitmap2, int i, int i2, int i3) { return addImageWatermark(bitmap, bitmap2, i, i2, i3, false); } public static Bitmap addImageWatermark(Bitmap bitmap, Bitmap bitmap2, int i, int i2, int i3, boolean z) { if (isEmptyBitmap(bitmap)) { return null; } Bitmap copy = bitmap.copy(bitmap.getConfig(), true); if (!isEmptyBitmap(bitmap2)) { Paint paint = new Paint(1); Canvas canvas = new Canvas(copy); paint.setAlpha(i3); canvas.drawBitmap(bitmap2, i, i2, paint); } if (z && !bitmap.isRecycled()) { bitmap.recycle(); } return copy; } public static Bitmap toAlpha(Bitmap bitmap) { return toAlpha(bitmap, false); } public static Bitmap toAlpha(Bitmap bitmap, Boolean bool) { if (isEmptyBitmap(bitmap)) { return null; } Bitmap extractAlpha = bitmap.extractAlpha(); if (bool.booleanValue() && !bitmap.isRecycled()) { bitmap.recycle(); } return extractAlpha; } public static Bitmap toGray(Bitmap bitmap) { return toGray(bitmap, false); } public static Bitmap toGray(Bitmap bitmap, boolean z) { if (isEmptyBitmap(bitmap)) { return null; } Bitmap createBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig()); Canvas canvas = new Canvas(createBitmap); Paint paint = new Paint(); ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.setSaturation(0.0f); paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix)); canvas.drawBitmap(bitmap, 0.0f, 0.0f, paint); if (z && !bitmap.isRecycled()) { bitmap.recycle(); } return createBitmap; } public static Bitmap fastBlur(Bitmap bitmap, float f, float f2) { return fastBlur(bitmap, f, f2, false); } public static Bitmap fastBlur(Bitmap bitmap, float f, float f2, boolean z) { Bitmap stackBlur; if (isEmptyBitmap(bitmap)) { return null; } int width = bitmap.getWidth(); int height = bitmap.getHeight(); Matrix matrix = new Matrix(); matrix.setScale(f, f); Bitmap createBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); Paint paint = new Paint(3); Canvas canvas = new Canvas(); paint.setColorFilter(new PorterDuffColorFilter(0, PorterDuff.Mode.SRC_ATOP)); canvas.scale(f, f); canvas.drawBitmap(createBitmap, 0.0f, 0.0f, paint); if (Build.VERSION.SDK_INT >= 17) { stackBlur = renderScriptBlur(createBitmap, f2, z); } else { stackBlur = stackBlur(createBitmap, (int) f2, z); } if (f == 1.0f) { if (z && !bitmap.isRecycled()) { bitmap.recycle(); } return stackBlur; } Bitmap createScaledBitmap = Bitmap.createScaledBitmap(stackBlur, width, height, true); if (!stackBlur.isRecycled()) { stackBlur.recycle(); } if (z && !bitmap.isRecycled()) { bitmap.recycle(); } return createScaledBitmap; } public static Bitmap renderScriptBlur(Bitmap bitmap, float f) { return renderScriptBlur(bitmap, f, false); } public static Bitmap renderScriptBlur(Bitmap bitmap, float f, boolean z) { if (!z) { bitmap = bitmap.copy(bitmap.getConfig(), true); } RenderScript renderScript = null; try { renderScript = RenderScript.create(XUtil.getContext()); renderScript.setMessageHandler(new RenderScript.RSMessageHandler()); Allocation createFromBitmap = Allocation.createFromBitmap(renderScript, bitmap, Allocation.MipmapControl.MIPMAP_NONE, 1); Allocation createTyped = Allocation.createTyped(renderScript, createFromBitmap.getType()); ScriptIntrinsicBlur create = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript)); create.setInput(createFromBitmap); create.setRadius(f); create.forEach(createTyped); createTyped.copyTo(bitmap); return bitmap; } finally { if (renderScript != null) { renderScript.destroy(); } } } public static Bitmap stackBlur(Bitmap bitmap, int i) { return stackBlur(bitmap, i, false); } public static Bitmap stackBlur(Bitmap bitmap, int i, boolean z) { int[] iArr; Bitmap copy = z ? bitmap : bitmap.copy(bitmap.getConfig(), true); int i2 = i < 1 ? 1 : i; int width = copy.getWidth(); int height = copy.getHeight(); int i3 = width * height; int[] iArr2 = new int[i3]; copy.getPixels(iArr2, 0, width, 0, 0, width, height); int i4 = width - 1; int i5 = height - 1; int i6 = i2 + i2 + 1; int[] iArr3 = new int[i3]; int[] iArr4 = new int[i3]; int[] iArr5 = new int[i3]; int[] iArr6 = new int[Math.max(width, height)]; int i7 = (i6 + 1) >> 1; int i8 = i7 * i7; int i9 = i8 * 256; int[] iArr7 = new int[i9]; for (int i10 = 0; i10 < i9; i10++) { iArr7[i10] = i10 / i8; } int[][] iArr8 = (int[][]) Array.newInstance((Class<?>) int.class, i6, 3); int i11 = i2 + 1; int i12 = 0; int i13 = 0; int i14 = 0; while (i12 < height) { Bitmap bitmap2 = copy; int i15 = height; int i16 = 0; int i17 = 0; int i18 = 0; int i19 = 0; int i20 = 0; int i21 = 0; int i22 = 0; int i23 = 0; int i24 = -i2; int i25 = 0; while (i24 <= i2) { int i26 = i5; int[] iArr9 = iArr6; int i27 = iArr2[i13 + Math.min(i4, Math.max(i24, 0))]; int[] iArr10 = iArr8[i24 + i2]; iArr10[0] = (i27 & 16711680) >> 16; iArr10[1] = (i27 & MotionEventCompat.ACTION_POINTER_INDEX_MASK) >> 8; iArr10[2] = i27 & 255; int abs = i11 - Math.abs(i24); i25 += iArr10[0] * abs; i16 += iArr10[1] * abs; i17 += iArr10[2] * abs; if (i24 > 0) { i21 += iArr10[0]; i22 += iArr10[1]; i23 += iArr10[2]; } else { i18 += iArr10[0]; i19 += iArr10[1]; i20 += iArr10[2]; } i24++; i5 = i26; iArr6 = iArr9; } int i28 = i5; int[] iArr11 = iArr6; int i29 = i25; int i30 = i2; int i31 = 0; while (i31 < width) { iArr3[i13] = iArr7[i29]; iArr4[i13] = iArr7[i16]; iArr5[i13] = iArr7[i17]; int i32 = i29 - i18; int i33 = i16 - i19; int i34 = i17 - i20; int[] iArr12 = iArr8[((i30 - i2) + i6) % i6]; int i35 = i18 - iArr12[0]; int i36 = i19 - iArr12[1]; int i37 = i20 - iArr12[2]; if (i12 == 0) { iArr = iArr7; iArr11[i31] = Math.min(i31 + i2 + 1, i4); } else { iArr = iArr7; } int i38 = iArr2[i14 + iArr11[i31]]; iArr12[0] = (i38 & 16711680) >> 16; iArr12[1] = (i38 & MotionEventCompat.ACTION_POINTER_INDEX_MASK) >> 8; iArr12[2] = i38 & 255; int i39 = i21 + iArr12[0]; int i40 = i22 + iArr12[1]; int i41 = i23 + iArr12[2]; i29 = i32 + i39; i16 = i33 + i40; i17 = i34 + i41; i30 = (i30 + 1) % i6; int[] iArr13 = iArr8[i30 % i6]; i18 = i35 + iArr13[0]; i19 = i36 + iArr13[1]; i20 = i37 + iArr13[2]; i21 = i39 - iArr13[0]; i22 = i40 - iArr13[1]; i23 = i41 - iArr13[2]; i13++; i31++; iArr7 = iArr; } i14 += width; i12++; copy = bitmap2; height = i15; i5 = i28; iArr6 = iArr11; } Bitmap bitmap3 = copy; int i42 = i5; int[] iArr14 = iArr6; int i43 = height; int[] iArr15 = iArr7; int i44 = 0; while (i44 < width) { int i45 = -i2; int i46 = i6; int[] iArr16 = iArr2; int i47 = 0; int i48 = 0; int i49 = 0; int i50 = 0; int i51 = 0; int i52 = 0; int i53 = 0; int i54 = i45; int i55 = i45 * width; int i56 = 0; int i57 = 0; while (i54 <= i2) { int i58 = width; int max = Math.max(0, i55) + i44; int[] iArr17 = iArr8[i54 + i2]; iArr17[0] = iArr3[max]; iArr17[1] = iArr4[max]; iArr17[2] = iArr5[max]; int abs2 = i11 - Math.abs(i54); i56 += iArr3[max] * abs2; i57 += iArr4[max] * abs2; i47 += iArr5[max] * abs2; if (i54 > 0) { i51 += iArr17[0]; i52 += iArr17[1]; i53 += iArr17[2]; } else { i48 += iArr17[0]; i49 += iArr17[1]; i50 += iArr17[2]; } int i59 = i42; if (i54 < i59) { i55 += i58; } i54++; i42 = i59; width = i58; } int i60 = width; int i61 = i42; int i62 = i44; int i63 = i57; int i64 = i2; int i65 = i43; int i66 = i56; int i67 = 0; while (i67 < i65) { iArr16[i62] = (iArr16[i62] & ViewCompat.MEASURED_STATE_MASK) | (iArr15[i66] << 16) | (iArr15[i63] << 8) | iArr15[i47]; int i68 = i66 - i48; int i69 = i63 - i49; int i70 = i47 - i50; int[] iArr18 = iArr8[((i64 - i2) + i46) % i46]; int i71 = i48 - iArr18[0]; int i72 = i49 - iArr18[1]; int i73 = i50 - iArr18[2]; int i74 = i2; if (i44 == 0) { iArr14[i67] = Math.min(i67 + i11, i61) * i60; } int i75 = iArr14[i67] + i44; iArr18[0] = iArr3[i75]; iArr18[1] = iArr4[i75]; iArr18[2] = iArr5[i75]; int i76 = i51 + iArr18[0]; int i77 = i52 + iArr18[1]; int i78 = i53 + iArr18[2]; i66 = i68 + i76; i63 = i69 + i77; i47 = i70 + i78; i64 = (i64 + 1) % i46; int[] iArr19 = iArr8[i64]; i48 = i71 + iArr19[0]; i49 = i72 + iArr19[1]; i50 = i73 + iArr19[2]; i51 = i76 - iArr19[0]; i52 = i77 - iArr19[1]; i53 = i78 - iArr19[2]; i62 += i60; i67++; i2 = i74; } i44++; i42 = i61; i43 = i65; i6 = i46; iArr2 = iArr16; width = i60; } int i79 = width; bitmap3.setPixels(iArr2, 0, i79, 0, 0, i79, i43); return bitmap3; } public static boolean save(View view, String str, Bitmap.CompressFormat compressFormat) { return save(view2Bitmap(view), getFileByPath(str), compressFormat, false); } public static boolean save(Bitmap bitmap, String str, Bitmap.CompressFormat compressFormat) { return save(bitmap, getFileByPath(str), compressFormat, false); } public static boolean save(Bitmap bitmap, File file, Bitmap.CompressFormat compressFormat) { return save(bitmap, file, compressFormat, false); } public static boolean save(Bitmap bitmap, String str, Bitmap.CompressFormat compressFormat, boolean z) { return save(bitmap, getFileByPath(str), compressFormat, z); } public static boolean save(Bitmap bitmap, File file, Bitmap.CompressFormat compressFormat, boolean z) { boolean z2; if (isEmptyBitmap(bitmap) || !createFileByDeleteOldFile(file)) { return false; } BufferedOutputStream bufferedOutputStream = null; try { try { BufferedOutputStream bufferedOutputStream2 = new BufferedOutputStream(new FileOutputStream(file)); try { try { z2 = bitmap.compress(compressFormat, 100, bufferedOutputStream2); if (z) { try { if (!bitmap.isRecycled()) { bitmap.recycle(); } } catch (IOException e) { e = e; bufferedOutputStream = bufferedOutputStream2; e.printStackTrace(); CloseUtils.closeIOQuietly(bufferedOutputStream); return z2; } } CloseUtils.closeIOQuietly(bufferedOutputStream2); } catch (IOException e2) { e = e2; z2 = false; } } catch (Throwable th) { th = th; bufferedOutputStream = bufferedOutputStream2; CloseUtils.closeIOQuietly(bufferedOutputStream); throw th; } } catch (IOException e3) { e = e3; z2 = false; } return z2; } catch (Throwable th2) { th = th2; } } public static boolean save(Bitmap bitmap, OutputStream outputStream, Bitmap.CompressFormat compressFormat) { return save(bitmap, outputStream, compressFormat, false); } public static boolean save(Bitmap bitmap, OutputStream outputStream, Bitmap.CompressFormat compressFormat, boolean z) { if (isEmptyBitmap(bitmap) || outputStream == null) { return false; } BufferedOutputStream bufferedOutputStream = null; try { BufferedOutputStream bufferedOutputStream2 = new BufferedOutputStream(outputStream); try { boolean compress = bitmap.compress(compressFormat, 100, bufferedOutputStream2); if (z && !bitmap.isRecycled()) { bitmap.recycle(); } CloseUtils.closeIOQuietly(bufferedOutputStream2); return compress; } catch (Throwable th) { th = th; bufferedOutputStream = bufferedOutputStream2; CloseUtils.closeIOQuietly(bufferedOutputStream); throw th; } } catch (Throwable th2) { th = th2; } } public static boolean isImage(File file) { return file != null && isImage(file.getPath()); } public static boolean isImage(String str) { String upperCase = str.toUpperCase(); return upperCase.endsWith(".PNG") || upperCase.endsWith(".JPG") || upperCase.endsWith(".JPEG") || upperCase.endsWith(".BMP") || upperCase.endsWith(".GIF") || upperCase.endsWith(".WEBP"); } public static String getImageType(String str) { return getImageType(getFileByPath(str)); } public static String getImageType(File file) { Throwable th; FileInputStream fileInputStream; IOException e; String imageType; if (file == null) { return ""; } FileInputStream fileInputStream2 = null; try { try { try { fileInputStream = new FileInputStream(file); try { imageType = getImageType(fileInputStream); } catch (IOException e2) { e = e2; e.printStackTrace(); if (fileInputStream != null) { fileInputStream.close(); } return getFileExtension(file.getAbsolutePath()).toUpperCase(); } } catch (Throwable th2) { th = th2; fileInputStream2 = fileInputStream; if (fileInputStream2 != null) { try { fileInputStream2.close(); } catch (IOException e3) { e3.printStackTrace(); } } throw th; } } catch (IOException e4) { fileInputStream = null; e = e4; } catch (Throwable th3) { th = th3; if (fileInputStream2 != null) { } throw th; } } catch (IOException e5) { e5.printStackTrace(); } if (imageType == null) { fileInputStream.close(); return getFileExtension(file.getAbsolutePath()).toUpperCase(); } try { fileInputStream.close(); } catch (IOException e6) { e6.printStackTrace(); } return imageType; } private static String getFileExtension(String str) { if (isSpace(str)) { return str; } int lastIndexOf = str.lastIndexOf(46); return (lastIndexOf == -1 || str.lastIndexOf(File.separator) >= lastIndexOf) ? "" : str.substring(lastIndexOf + 1); } private static String getImageType(InputStream inputStream) { if (inputStream == null) { return null; } try { byte[] bArr = new byte[8]; if (inputStream.read(bArr, 0, 8) != -1) { return getImageType(bArr); } return null; } catch (IOException e) { e.printStackTrace(); return null; } } private static String getImageType(byte[] bArr) { if (isJPEG(bArr)) { return "JPEG"; } if (isGIF(bArr)) { return "GIF"; } if (isPNG(bArr)) { return "PNG"; } if (isBMP(bArr)) { return "BMP"; } return null; } private static boolean isJPEG(byte[] bArr) { return bArr.length >= 2 && bArr[0] == -1 && bArr[1] == -40; } private static boolean isGIF(byte[] bArr) { return bArr.length >= 6 && bArr[0] == 71 && bArr[1] == 73 && bArr[2] == 70 && bArr[3] == 56 && (bArr[4] == 55 || bArr[4] == 57) && bArr[5] == 97; } private static boolean isPNG(byte[] bArr) { return bArr.length >= 8 && bArr[0] == -119 && bArr[1] == 80 && bArr[2] == 78 && bArr[3] == 71 && bArr[4] == 13 && bArr[5] == 10 && bArr[6] == 26 && bArr[7] == 10; } private static boolean isBMP(byte[] bArr) { return bArr.length >= 2 && bArr[0] == 66 && bArr[1] == 77; } private static boolean isEmptyBitmap(Bitmap bitmap) { return bitmap == null || bitmap.getWidth() == 0 || bitmap.getHeight() == 0; } public static Bitmap compressByScale(Bitmap bitmap, int i, int i2) { return scale(bitmap, i, i2, false); } public static Bitmap compressByScale(Bitmap bitmap, int i, int i2, boolean z) { return scale(bitmap, i, i2, z); } public static Bitmap compressByScale(Bitmap bitmap, float f, float f2) { return scale(bitmap, f, f2, false); } public static Bitmap compressByScale(Bitmap bitmap, float f, float f2, boolean z) { return scale(bitmap, f, f2, z); } public static Bitmap compressByQuality(Bitmap bitmap, int i) { return compressByQuality(bitmap, i, false); } public static Bitmap compressByQuality(Bitmap bitmap, int i, boolean z) { if (isEmptyBitmap(bitmap)) { return null; } ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, i, byteArrayOutputStream); byte[] byteArray = byteArrayOutputStream.toByteArray(); if (z && !bitmap.isRecycled()) { bitmap.recycle(); } return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); } public static Bitmap compressByQuality(Bitmap bitmap, long j) { return compressByQuality(bitmap, j, false); } public static Bitmap compressByQuality(Bitmap bitmap, long j, boolean z) { byte[] byteArray; if (isEmptyBitmap(bitmap) || j <= 0) { return null; } ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); int i = 100; bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream); if (byteArrayOutputStream.size() <= j) { byteArray = byteArrayOutputStream.toByteArray(); } else { byteArrayOutputStream.reset(); bitmap.compress(Bitmap.CompressFormat.JPEG, 0, byteArrayOutputStream); if (byteArrayOutputStream.size() >= j) { byteArray = byteArrayOutputStream.toByteArray(); } else { int i2 = 0; int i3 = 0; while (i2 < i) { i3 = (i2 + i) / 2; byteArrayOutputStream.reset(); bitmap.compress(Bitmap.CompressFormat.JPEG, i3, byteArrayOutputStream); long size = byteArrayOutputStream.size(); if (size == j) { break; } if (size > j) { i = i3 - 1; } else { i2 = i3 + 1; } } if (i == i3 - 1) { byteArrayOutputStream.reset(); bitmap.compress(Bitmap.CompressFormat.JPEG, i2, byteArrayOutputStream); } byteArray = byteArrayOutputStream.toByteArray(); } } if (z && !bitmap.isRecycled()) { bitmap.recycle(); } return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); } public static Bitmap compressBySampleSize(Bitmap bitmap, int i) { return compressBySampleSize(bitmap, i, false); } public static Bitmap compressBySampleSize(Bitmap bitmap, int i, boolean z) { if (isEmptyBitmap(bitmap)) { return null; } BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = i; ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream); byte[] byteArray = byteArrayOutputStream.toByteArray(); if (z && !bitmap.isRecycled()) { bitmap.recycle(); } return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length, options); } public static Bitmap compressBySampleSize(Bitmap bitmap, int i, int i2) { return compressBySampleSize(bitmap, i, i2, false); } public static Bitmap compressBySampleSize(Bitmap bitmap, int i, int i2, boolean z) { if (isEmptyBitmap(bitmap)) { return null; } BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream); byte[] byteArray = byteArrayOutputStream.toByteArray(); BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length, options); options.inSampleSize = calculateInSampleSize(options, i, i2); options.inJustDecodeBounds = false; if (z && !bitmap.isRecycled()) { bitmap.recycle(); } return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length, options); } private static File getFileByPath(String str) { if (isSpace(str)) { return null; } return new File(str); } private static boolean createFileByDeleteOldFile(File file) { if (file == null) { return false; } if ((file.exists() && !file.delete()) || !createOrExistsDir(file.getParentFile())) { return false; } try { return file.createNewFile(); } catch (IOException e) { e.printStackTrace(); return false; } } private static boolean createOrExistsDir(File file) { return file != null && (!file.exists() ? !file.mkdirs() : !file.isDirectory()); } private static boolean isSpace(String str) { if (str == null) { return true; } int length = str.length(); for (int i = 0; i < length; i++) { if (!Character.isWhitespace(str.charAt(i))) { return false; } } return true; } private static int calculateInSampleSize(BitmapFactory.Options options, int i, int i2) { int i3 = options.outHeight; int i4 = options.outWidth; int i5 = 1; while (true) { i4 >>= 1; if (i4 < i || (i3 = i3 >> 1) < i2) { break; } i5 <<= 1; } return i5; } private static byte[] input2Byte(InputStream inputStream) { if (inputStream == null) { return null; } try { try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] bArr = new byte[1024]; while (true) { int read = inputStream.read(bArr, 0, 1024); if (read == -1) { break; } byteArrayOutputStream.write(bArr, 0, read); } byte[] byteArray = byteArrayOutputStream.toByteArray(); try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } return byteArray; } catch (IOException e2) { e2.printStackTrace(); try { inputStream.close(); } catch (IOException e3) { e3.printStackTrace(); } return null; } } catch (Throwable th) { try { inputStream.close(); } catch (IOException e4) { e4.printStackTrace(); } throw th; } } public static boolean assertValidRequest(Context context) { if (context instanceof Activity) { return !isDestroy((Activity) context); } if (context instanceof ContextWrapper) { if (((ContextWrapper) context).getBaseContext() instanceof Activity) { return !isDestroy((Activity) r2.getBaseContext()); } } return true; } private static boolean isDestroy(Activity activity) { return activity == null || activity.isFinishing() || activity.isDestroyed(); } }