package com.yalantis.ucrop.task;
|
|
import android.content.Context;
|
import android.graphics.Bitmap;
|
import android.graphics.Matrix;
|
import android.graphics.RectF;
|
import android.media.ExifInterface;
|
import android.net.Uri;
|
import android.os.AsyncTask;
|
import android.os.Build;
|
import android.os.ParcelFileDescriptor;
|
import android.util.Log;
|
|
import androidx.annotation.NonNull;
|
import androidx.annotation.Nullable;
|
|
import com.yalantis.ucrop.callback.BitmapCropCallback;
|
import com.yalantis.ucrop.model.CropParameters;
|
import com.yalantis.ucrop.model.ExifInfo;
|
import com.yalantis.ucrop.model.ImageState;
|
import com.yalantis.ucrop.util.BitmapLoadUtils;
|
import com.yalantis.ucrop.util.FileUtils;
|
import com.yalantis.ucrop.util.ImageHeaderParser;
|
|
import java.io.File;
|
import java.io.FileDescriptor;
|
import java.io.FileInputStream;
|
import java.io.FileNotFoundException;
|
import java.io.IOException;
|
import java.io.OutputStream;
|
import java.lang.ref.WeakReference;
|
|
/**
|
* Crops part of image that fills the crop bounds.
|
* <p/>
|
* First image is downscaled if max size was set and if resulting image is larger that max size.
|
* Then image is rotated accordingly.
|
* Finally new Bitmap object is created and saved to file.
|
*/
|
public class BitmapCropTask extends AsyncTask<Void, Void, Throwable> {
|
|
private static final String TAG = "BitmapCropTask";
|
|
private final WeakReference<Context> mContext;
|
|
private Bitmap mViewBitmap;
|
|
private final RectF mCropRect;
|
private final RectF mCurrentImageRect;
|
|
private float mCurrentScale, mCurrentAngle;
|
private final int mMaxResultImageSizeX, mMaxResultImageSizeY;
|
|
private final Bitmap.CompressFormat mCompressFormat;
|
private final int mCompressQuality;
|
private final Uri mImageInputUri;
|
private final String mImageOutputPath;
|
private final ExifInfo mExifInfo;
|
private final BitmapCropCallback mCropCallback;
|
|
private int mCroppedImageWidth, mCroppedImageHeight;
|
private int cropOffsetX, cropOffsetY;
|
|
public BitmapCropTask(@NonNull Context context, @Nullable Bitmap viewBitmap, @NonNull ImageState imageState, @NonNull CropParameters cropParameters,
|
@Nullable BitmapCropCallback cropCallback) {
|
|
mContext = new WeakReference<>(context);
|
|
mViewBitmap = viewBitmap;
|
mCropRect = imageState.getCropRect();
|
mCurrentImageRect = imageState.getCurrentImageRect();
|
|
mCurrentScale = imageState.getCurrentScale();
|
mCurrentAngle = imageState.getCurrentAngle();
|
mMaxResultImageSizeX = cropParameters.getMaxResultImageSizeX();
|
mMaxResultImageSizeY = cropParameters.getMaxResultImageSizeY();
|
|
mCompressFormat = cropParameters.getCompressFormat();
|
mCompressQuality = cropParameters.getCompressQuality();
|
|
mImageInputUri = cropParameters.getImageInputUri();
|
mImageOutputPath = cropParameters.getImageOutputPath();
|
mExifInfo = cropParameters.getExifInfo();
|
|
mCropCallback = cropCallback;
|
}
|
|
@Override
|
@Nullable
|
protected Throwable doInBackground(Void... params) {
|
if (mViewBitmap == null) {
|
return new NullPointerException("ViewBitmap is null");
|
} else if (mViewBitmap.isRecycled()) {
|
return new NullPointerException("ViewBitmap is recycled");
|
} else if (mCurrentImageRect.isEmpty()) {
|
return new NullPointerException("CurrentImageRect is empty");
|
}
|
|
|
try {
|
crop();
|
mViewBitmap = null;
|
} catch (Throwable throwable) {
|
return throwable;
|
}
|
|
return null;
|
}
|
|
|
private boolean crop() throws IOException {
|
// Downsize if needed
|
if (mMaxResultImageSizeX > 0 && mMaxResultImageSizeY > 0) {
|
float cropWidth = mCropRect.width() / mCurrentScale;
|
float cropHeight = mCropRect.height() / mCurrentScale;
|
|
if (cropWidth > mMaxResultImageSizeX || cropHeight > mMaxResultImageSizeY) {
|
|
float scaleX = mMaxResultImageSizeX / cropWidth;
|
float scaleY = mMaxResultImageSizeY / cropHeight;
|
float resizeScale = Math.min(scaleX, scaleY);
|
|
Bitmap resizedBitmap = Bitmap.createScaledBitmap(mViewBitmap,
|
Math.round(mViewBitmap.getWidth() * resizeScale),
|
Math.round(mViewBitmap.getHeight() * resizeScale), false);
|
if (mViewBitmap != resizedBitmap) {
|
mViewBitmap.recycle();
|
}
|
mViewBitmap = resizedBitmap;
|
|
mCurrentScale /= resizeScale;
|
}
|
}
|
|
// Rotate if needed
|
if (mCurrentAngle != 0) {
|
Matrix tempMatrix = new Matrix();
|
tempMatrix.setRotate(mCurrentAngle, mViewBitmap.getWidth() / 2, mViewBitmap.getHeight() / 2);
|
|
Bitmap rotatedBitmap = Bitmap.createBitmap(mViewBitmap, 0, 0, mViewBitmap.getWidth(), mViewBitmap.getHeight(),
|
tempMatrix, true);
|
if (mViewBitmap != rotatedBitmap) {
|
mViewBitmap.recycle();
|
}
|
mViewBitmap = rotatedBitmap;
|
}
|
|
cropOffsetX = Math.round((mCropRect.left - mCurrentImageRect.left) / mCurrentScale);
|
cropOffsetY = Math.round((mCropRect.top - mCurrentImageRect.top) / mCurrentScale);
|
mCroppedImageWidth = Math.round(mCropRect.width() / mCurrentScale);
|
mCroppedImageHeight = Math.round(mCropRect.height() / mCurrentScale);
|
|
boolean shouldCrop = shouldCrop(mCroppedImageWidth, mCroppedImageHeight);
|
Log.i(TAG, "Should crop: " + shouldCrop);
|
boolean isAndroidQ = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q;
|
if (shouldCrop) {
|
ExifInterface originalExif;
|
if (isAndroidQ && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
|
ParcelFileDescriptor parcelFileDescriptor =
|
mContext.get().getContentResolver().openFileDescriptor(mImageInputUri, "r");
|
FileInputStream inputStream = new FileInputStream(parcelFileDescriptor.getFileDescriptor());
|
originalExif = new ExifInterface(inputStream);
|
} else {
|
originalExif = new ExifInterface(mImageInputUri.getPath());
|
}
|
saveImage(Bitmap.createBitmap(mViewBitmap, cropOffsetX, cropOffsetY, mCroppedImageWidth, mCroppedImageHeight));
|
if (mCompressFormat.equals(Bitmap.CompressFormat.JPEG)) {
|
ImageHeaderParser.copyExif(originalExif, mCroppedImageWidth, mCroppedImageHeight, mImageOutputPath);
|
}
|
return true;
|
} else {
|
if (isAndroidQ && mImageInputUri.toString().startsWith("content://")) {
|
ParcelFileDescriptor parcelFileDescriptor =
|
mContext.get().getContentResolver().openFileDescriptor(mImageInputUri, "r");
|
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
|
FileInputStream inputStream = new FileInputStream(fileDescriptor);
|
FileUtils.copyFile(inputStream, mImageOutputPath);
|
} else {
|
FileUtils.copyFile(mImageInputUri.getPath(), mImageOutputPath);
|
}
|
return true;
|
}
|
}
|
|
private void saveImage(@NonNull Bitmap croppedBitmap) throws FileNotFoundException {
|
Context context = mContext.get();
|
if (context == null) {
|
return;
|
}
|
|
OutputStream outputStream = null;
|
try {
|
outputStream = context.getContentResolver().openOutputStream(Uri.fromFile(new File(mImageOutputPath)));
|
croppedBitmap.compress(mCompressFormat, mCompressQuality, outputStream);
|
croppedBitmap.recycle();
|
} finally {
|
BitmapLoadUtils.close(outputStream);
|
}
|
}
|
|
/**
|
* Check whether an image should be cropped at all or just file can be copied to the destination path.
|
* For each 1000 pixels there is one pixel of error due to matrix calculations etc.
|
*
|
* @param width - crop area width
|
* @param height - crop area height
|
* @return - true if image must be cropped, false - if original image fits requirements
|
*/
|
private boolean shouldCrop(int width, int height) {
|
int pixelError = 1;
|
pixelError += Math.round(Math.max(width, height) / 1000f);
|
return (mMaxResultImageSizeX > 0 && mMaxResultImageSizeY > 0)
|
|| Math.abs(mCropRect.left - mCurrentImageRect.left) > pixelError
|
|| Math.abs(mCropRect.top - mCurrentImageRect.top) > pixelError
|
|| Math.abs(mCropRect.bottom - mCurrentImageRect.bottom) > pixelError
|
|| Math.abs(mCropRect.right - mCurrentImageRect.right) > pixelError;
|
}
|
|
@Override
|
protected void onPostExecute(@Nullable Throwable t) {
|
if (mCropCallback != null) {
|
if (t == null) {
|
Uri uri = Uri.fromFile(new File(mImageOutputPath));
|
mCropCallback.onBitmapCropped(uri, cropOffsetX, cropOffsetY, mCroppedImageWidth, mCroppedImageHeight);
|
} else {
|
mCropCallback.onCropFailure(t);
|
}
|
}
|
}
|
|
}
|