Skip to content
This repository has been archived by the owner on Jan 7, 2023. It is now read-only.

Using createScaledBitmap, using a scaled matrix and calling createBit… #226

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/src/main/java/com/soundcloud/android/crop/Crop.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ interface Extra {
String MAX_X = "max_x";
String MAX_Y = "max_y";
String ERROR = "error";
String SCALE_METHOD = "scale_method";
}

public enum ScaleMethod {
/**
* Using the exact dimensions provided and using a scaling function for creating a scale bitmap. When scaling a large amount, the quality can suffer.
*/
EXACT,
/**
* This approach uses sampling while decoding the image which provides better results; however, this requires the image be scaled by powers of 2 and cannot provide
* dimensions exactly requested. This will result in better quality images when the scale amount is large.
*/
BETTER_QUALITY_BEST_FIT;
}

private Intent cropIntent;
Expand All @@ -44,6 +57,7 @@ private Crop(Uri source, Uri destination) {
cropIntent = new Intent();
cropIntent.setData(source);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, destination);
cropIntent.putExtra(Extra.SCALE_METHOD, ScaleMethod.EXACT.ordinal());
}

/**
Expand Down Expand Up @@ -79,6 +93,16 @@ public Crop withMaxSize(int width, int height) {
return this;
}

/**
* Set how the image will be scaled when providing {@link Extra#MAX_X} and {@link Extra#MAX_Y} with the {@link #withMaxSize(int, int)}
* @param type
* @return
*/
public Crop withScaleMethod(ScaleMethod type) {
cropIntent.putExtra(Extra.SCALE_METHOD, type.ordinal());
return this;
}

/**
* Send the crop Intent from an Activity
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.Uri;
Expand Down Expand Up @@ -56,6 +58,7 @@ public class CropImageActivity extends MonitoredActivity {
private int maxX;
private int maxY;
private int exifRotation;
private Crop.ScaleMethod scaleMethod;

private Uri sourceUri;
private Uri saveUri;
Expand Down Expand Up @@ -126,6 +129,7 @@ private void loadInput() {
maxX = extras.getInt(Crop.Extra.MAX_X);
maxY = extras.getInt(Crop.Extra.MAX_Y);
saveUri = extras.getParcelable(MediaStore.EXTRA_OUTPUT);
scaleMethod = Crop.ScaleMethod.values()[extras.getInt(Crop.Extra.SCALE_METHOD, 0)];
}

sourceUri = intent.getData();
Expand Down Expand Up @@ -343,11 +347,29 @@ private Bitmap decodeRegionCrop(Rect rect, int outWidth, int outHeight) {
}

try {
croppedImage = decoder.decodeRegion(rect, new BitmapFactory.Options());
if (croppedImage != null && (rect.width() > outWidth || rect.height() > outHeight)) {
Matrix matrix = new Matrix();
matrix.postScale((float) outWidth / rect.width(), (float) outHeight / rect.height());
croppedImage = Bitmap.createBitmap(croppedImage, 0, 0, croppedImage.getWidth(), croppedImage.getHeight(), matrix, true);
BitmapFactory.Options options = new BitmapFactory.Options();
if ((rect.width() > outWidth || rect.height() > outHeight)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outer parenthesis is redundant 😄

switch (scaleMethod) {
case EXACT:
croppedImage = decoder.decodeRegion(rect, options);
Matrix matrix = new Matrix();
matrix.postScale((float) outWidth / rect.width(), (float) outHeight / rect.height());
croppedImage = Bitmap.createBitmap(croppedImage, 0, 0, croppedImage.getWidth(), croppedImage.getHeight(), matrix, true);
break;
case BETTER_QUALITY_BEST_FIT:
int w, h;
int inSampleSize = 1;
do {
inSampleSize *= 2;
w = rect.width() / inSampleSize;
h = rect.height() / inSampleSize;
} while(w > outWidth && h > outHeight);
options.inSampleSize = inSampleSize;
croppedImage = decoder.decodeRegion(rect, options);
break;
}
} else {
croppedImage = decoder.decodeRegion(rect, options);
}
} catch (IllegalArgumentException e) {
// Rethrow with some extra information
Expand Down Expand Up @@ -433,5 +455,4 @@ private void setResultUri(Uri uri) {
private void setResultException(Throwable throwable) {
setResult(Crop.RESULT_ERROR, new Intent().putExtra(Crop.Extra.ERROR, throwable));
}

}