导航菜单

页面标题

页面副标题

i.BarIS v2024.401.30.180 - CropImageActivity.java 源代码

正在查看: i.BarIS v2024.401.30.180 应用的 CropImageActivity.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.soundcloud.android.crop;

import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.Uri;
import android.opengl.GLES10;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import com.soundcloud.android.crop.Crop;
import com.soundcloud.android.crop.ImageViewTouchBase;
import com.soundcloud.android.crop.MonitoredActivity;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.CountDownLatch;

public class CropImageActivity extends MonitoredActivity {
    private static final int SIZE_DEFAULT = 2048;
    private static final int SIZE_LIMIT = 4096;
    private int aspectX;
    private int aspectY;
    private HighlightView cropView;
    private int exifRotation;
    private final Handler handler = new Handler();
    private CropImageView imageView;
    private boolean isSaving;
    private int maxX;
    private int maxY;
    private RotateBitmap rotateBitmap;
    private int sampleSize;
    private Uri saveUri;
    private Uri sourceUri;

    @Override
    public boolean onSearchRequested() {
        return false;
    }

    @Override
    public void addLifeCycleListener(MonitoredActivity.LifeCycleListener lifeCycleListener) {
        super.addLifeCycleListener(lifeCycleListener);
    }

    @Override
    public void removeLifeCycleListener(MonitoredActivity.LifeCycleListener lifeCycleListener) {
        super.removeLifeCycleListener(lifeCycleListener);
    }

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        requestWindowFeature(1);
        setContentView(R.layout.crop__activity_crop);
        initViews();
        setupFromIntent();
        if (this.rotateBitmap == null) {
            finish();
        } else {
            startCrop();
        }
    }

    private void initViews() {
        CropImageView cropImageView = (CropImageView) findViewById(R.id.crop_image);
        this.imageView = cropImageView;
        cropImageView.context = this;
        this.imageView.setRecycler(new ImageViewTouchBase.Recycler() {
            @Override
            public void recycle(Bitmap bitmap) {
                bitmap.recycle();
                System.gc();
            }
        });
        findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CropImageActivity.this.setResult(0);
                CropImageActivity.this.finish();
            }
        });
        findViewById(R.id.btn_done).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CropImageActivity.this.onSaveClicked();
            }
        });
    }

    private void setupFromIntent() {
        Throwable th;
        InputStream inputStream;
        OutOfMemoryError e;
        IOException e2;
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            this.aspectX = extras.getInt(Crop.Extra.ASPECT_X);
            this.aspectY = extras.getInt(Crop.Extra.ASPECT_Y);
            this.maxX = extras.getInt(Crop.Extra.MAX_X);
            this.maxY = extras.getInt(Crop.Extra.MAX_Y);
            this.saveUri = (Uri) extras.getParcelable("output");
        }
        Uri data = intent.getData();
        this.sourceUri = data;
        if (data != null) {
            ContentResolver contentResolver = getContentResolver();
            ?? r1 = this.sourceUri;
            this.exifRotation = CropUtil.getExifRotation(CropUtil.getFromMediaUri(this, contentResolver, r1));
            try {
                try {
                    this.sampleSize = calculateBitmapSampleSize(this.sourceUri);
                    inputStream = getContentResolver().openInputStream(this.sourceUri);
                    try {
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inSampleSize = this.sampleSize;
                        this.rotateBitmap = new RotateBitmap(BitmapFactory.decodeStream(inputStream, null, options), this.exifRotation);
                        r1 = inputStream;
                    } catch (IOException e3) {
                        e2 = e3;
                        Log.e("Error reading image: " + e2.getMessage(), e2);
                        setResultException(e2);
                        r1 = inputStream;
                        CropUtil.closeSilently(r1);
                    } catch (OutOfMemoryError e4) {
                        e = e4;
                        Log.e("OOM reading image: " + e.getMessage(), e);
                        setResultException(e);
                        r1 = inputStream;
                        CropUtil.closeSilently(r1);
                    }
                } catch (Throwable th2) {
                    th = th2;
                    CropUtil.closeSilently(r1);
                    throw th;
                }
            } catch (IOException e5) {
                inputStream = null;
                e2 = e5;
            } catch (OutOfMemoryError e6) {
                inputStream = null;
                e = e6;
            } catch (Throwable th3) {
                r1 = 0;
                th = th3;
                CropUtil.closeSilently(r1);
                throw th;
            }
            CropUtil.closeSilently(r1);
        }
    }

    private int calculateBitmapSampleSize(Uri uri) throws IOException {
        BitmapFactory.Options options = new BitmapFactory.Options();
        int i = 1;
        options.inJustDecodeBounds = true;
        InputStream inputStream = null;
        try {
            InputStream openInputStream = getContentResolver().openInputStream(uri);
            try {
                BitmapFactory.decodeStream(openInputStream, null, options);
                CropUtil.closeSilently(openInputStream);
                int maxImageSize = getMaxImageSize();
                while (true) {
                    if (options.outHeight / i <= maxImageSize && options.outWidth / i <= maxImageSize) {
                        return i;
                    }
                    i <<= 1;
                }
            } catch (Throwable th) {
                th = th;
                inputStream = openInputStream;
                CropUtil.closeSilently(inputStream);
                throw th;
            }
        } catch (Throwable th2) {
            th = th2;
        }
    }

    private int getMaxImageSize() {
        int maxTextureSize = getMaxTextureSize();
        if (maxTextureSize == 0) {
            return 2048;
        }
        return Math.min(maxTextureSize, 4096);
    }

    private int getMaxTextureSize() {
        int[] iArr = new int[1];
        GLES10.glGetIntegerv(3379, iArr, 0);
        return iArr[0];
    }

    private void startCrop() {
        if (isFinishing()) {
            return;
        }
        this.imageView.setImageRotateBitmapResetBase(this.rotateBitmap, true);
        CropUtil.startBackgroundJob(this, null, getResources().getString(R.string.crop__wait), new Runnable() {
            @Override
            public void run() {
                final CountDownLatch countDownLatch = new CountDownLatch(1);
                CropImageActivity.this.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (CropImageActivity.this.imageView.getScale() == 1.0f) {
                            CropImageActivity.this.imageView.center(true, true);
                        }
                        countDownLatch.countDown();
                    }
                });
                try {
                    countDownLatch.await();
                    new Cropper().crop();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, this.handler);
    }

    private class Cropper {
        private Cropper() {
        }

        public void makeDefault() {
            int i;
            if (CropImageActivity.this.rotateBitmap == null) {
                return;
            }
            HighlightView highlightView = new HighlightView(CropImageActivity.this.imageView);
            int width = CropImageActivity.this.rotateBitmap.getWidth();
            int height = CropImageActivity.this.rotateBitmap.getHeight();
            boolean z = false;
            Rect rect = new Rect(0, 0, width, height);
            int min = (Math.min(width, height) * 4) / 5;
            if (CropImageActivity.this.aspectX == 0 || CropImageActivity.this.aspectY == 0) {
                i = min;
            } else if (CropImageActivity.this.aspectX > CropImageActivity.this.aspectY) {
                i = (CropImageActivity.this.aspectY * min) / CropImageActivity.this.aspectX;
            } else {
                i = min;
                min = (CropImageActivity.this.aspectX * min) / CropImageActivity.this.aspectY;
            }
            RectF rectF = new RectF((width - min) / 2, (height - i) / 2, r1 + min, r2 + i);
            Matrix unrotatedMatrix = CropImageActivity.this.imageView.getUnrotatedMatrix();
            if (CropImageActivity.this.aspectX != 0 && CropImageActivity.this.aspectY != 0) {
                z = true;
            }
            highlightView.setup(unrotatedMatrix, rect, rectF, z);
            CropImageActivity.this.imageView.add(highlightView);
        }

        public void crop() {
            CropImageActivity.this.handler.post(new Runnable() {
                @Override
                public void run() {
                    Cropper.this.makeDefault();
                    CropImageActivity.this.imageView.invalidate();
                    if (CropImageActivity.this.imageView.highlightViews.size() == 1) {
                        CropImageActivity.this.cropView = CropImageActivity.this.imageView.highlightViews.get(0);
                        CropImageActivity.this.cropView.setFocus(true);
                    }
                }
            });
        }
    }

    public void onSaveClicked() {
        int i;
        HighlightView highlightView = this.cropView;
        if (highlightView == null || this.isSaving) {
            return;
        }
        this.isSaving = true;
        Rect scaledCropRect = highlightView.getScaledCropRect(this.sampleSize);
        int width = scaledCropRect.width();
        int height = scaledCropRect.height();
        int i2 = this.maxX;
        if (i2 > 0 && (i = this.maxY) > 0 && (width > i2 || height > i)) {
            float f = width / height;
            int i3 = this.maxX;
            int i4 = this.maxY;
            if (i3 / i4 > f) {
                width = (int) ((i4 * f) + 0.5f);
                height = i4;
            } else {
                height = (int) ((i3 / f) + 0.5f);
                width = i3;
            }
        }
        try {
            Bitmap decodeRegionCrop = decodeRegionCrop(scaledCropRect, width, height);
            if (decodeRegionCrop != null) {
                this.imageView.setImageRotateBitmapResetBase(new RotateBitmap(decodeRegionCrop, this.exifRotation), true);
                this.imageView.center(true, true);
                this.imageView.highlightViews.clear();
            }
            saveImage(decodeRegionCrop);
        } catch (IllegalArgumentException e) {
            setResultException(e);
            finish();
        }
    }

    private void saveImage(final Bitmap bitmap) {
        if (bitmap != null) {
            CropUtil.startBackgroundJob(this, null, getResources().getString(R.string.crop__saving), new Runnable() {
                @Override
                public void run() {
                    CropImageActivity.this.saveOutput(bitmap);
                }
            }, this.handler);
        } else {
            finish();
        }
    }

    private Bitmap decodeRegionCrop(Rect rect, int i, int i2) {
        OutOfMemoryError outOfMemoryError;
        Bitmap bitmap;
        IOException iOException;
        InputStream openInputStream;
        Rect rect2;
        clearImageView();
        InputStream inputStream = null;
        try {
            try {
                openInputStream = getContentResolver().openInputStream(this.sourceUri);
            } catch (Throwable th) {
                th = th;
            }
            try {
                BitmapRegionDecoder newInstance = BitmapRegionDecoder.newInstance(openInputStream, false);
                int width = newInstance.getWidth();
                int height = newInstance.getHeight();
                if (this.exifRotation != 0) {
                    Matrix matrix = new Matrix();
                    matrix.setRotate(-this.exifRotation);
                    RectF rectF = new RectF();
                    matrix.mapRect(rectF, new RectF(rect));
                    rectF.offset(rectF.left < 0.0f ? width : 0.0f, rectF.top < 0.0f ? height : 0.0f);
                    rect2 = new Rect((int) rectF.left, (int) rectF.top, (int) rectF.right, (int) rectF.bottom);
                } else {
                    rect2 = rect;
                }
                try {
                    Bitmap decodeRegion = newInstance.decodeRegion(rect2, new BitmapFactory.Options());
                    if (rect2.width() > i || rect2.height() > i2) {
                        Matrix matrix2 = new Matrix();
                        matrix2.postScale(i / rect2.width(), i2 / rect2.height());
                        decodeRegion = Bitmap.createBitmap(decodeRegion, 0, 0, decodeRegion.getWidth(), decodeRegion.getHeight(), matrix2, true);
                    }
                    CropUtil.closeSilently(openInputStream);
                    return decodeRegion;
                } catch (IllegalArgumentException e) {
                    throw new IllegalArgumentException("Rectangle " + rect2 + " is outside of the image (" + width + "," + height + "," + this.exifRotation + ")", e);
                }
            } catch (IOException e2) {
                iOException = e2;
                bitmap = null;
                inputStream = openInputStream;
                Log.e("Error cropping image: " + iOException.getMessage(), iOException);
                finish();
                CropUtil.closeSilently(inputStream);
                return bitmap;
            } catch (OutOfMemoryError e3) {
                outOfMemoryError = e3;
                bitmap = null;
                inputStream = openInputStream;
                Log.e("OOM cropping image: " + outOfMemoryError.getMessage(), outOfMemoryError);
                setResultException(outOfMemoryError);
                CropUtil.closeSilently(inputStream);
                return bitmap;
            } catch (Throwable th2) {
                th = th2;
                inputStream = openInputStream;
                CropUtil.closeSilently(inputStream);
                throw th;
            }
        } catch (IOException e4) {
            iOException = e4;
            bitmap = null;
        } catch (OutOfMemoryError e5) {
            outOfMemoryError = e5;
            bitmap = null;
        }
    }

    private void clearImageView() {
        this.imageView.clear();
        RotateBitmap rotateBitmap = this.rotateBitmap;
        if (rotateBitmap != null) {
            rotateBitmap.recycle();
        }
        System.gc();
    }

    public void saveOutput(final Bitmap bitmap) {
        if (this.saveUri != null) {
            OutputStream outputStream = null;
            try {
                try {
                    outputStream = getContentResolver().openOutputStream(this.saveUri);
                    if (outputStream != null) {
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outputStream);
                    }
                } catch (IOException e) {
                    setResultException(e);
                    Log.e("Cannot open file: " + this.saveUri, e);
                }
                CropUtil.copyExifRotation(CropUtil.getFromMediaUri(this, getContentResolver(), this.sourceUri), CropUtil.getFromMediaUri(this, getContentResolver(), this.saveUri));
                setResultUri(this.saveUri);
            } finally {
                CropUtil.closeSilently(outputStream);
            }
        }
        this.handler.post(new Runnable() {
            @Override
            public void run() {
                CropImageActivity.this.imageView.clear();
                bitmap.recycle();
            }
        });
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RotateBitmap rotateBitmap = this.rotateBitmap;
        if (rotateBitmap != null) {
            rotateBitmap.recycle();
        }
    }

    public boolean isSaving() {
        return this.isSaving;
    }

    private void setResultUri(Uri uri) {
        setResult(-1, new Intent().putExtra("output", uri));
    }

    private void setResultException(Throwable th) {
        setResult(Crop.RESULT_ERROR, new Intent().putExtra("error", th));
    }
}