Skip to content

Commit

Permalink
Merge remote-tracking branch 'blinkmacalahan/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	lib/src/main/java/com/soundcloud/android/crop/Crop.java
#	lib/src/main/java/com/soundcloud/android/crop/CropImageActivity.java
  • Loading branch information
mtrakal committed Mar 28, 2017
2 parents 03d7b1e + b46aa27 commit 035fe7e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
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 @@ -27,6 +27,19 @@ interface Extra {
String MAX_Y = "max_y";
String AS_PNG = "as_png";
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 @@ -45,6 +58,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 @@ -80,6 +94,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;
}

/**
* Set whether to save the result as a PNG or not. Helpful to preserve alpha.
* @param asPng whether to save the result as a PNG or not
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 @@ -57,6 +59,7 @@ public class CropImageActivity extends MonitoredActivity {
private int maxY;
private int exifRotation;
private boolean saveAsPng;
private Crop.ScaleMethod scaleMethod;

private Uri sourceUri;
private Uri saveUri;
Expand Down Expand Up @@ -128,6 +131,7 @@ private void loadInput() {
maxY = extras.getInt(Crop.Extra.MAX_Y);
saveAsPng = extras.getBoolean(Crop.Extra.AS_PNG, false);
saveUri = extras.getParcelable(MediaStore.EXTRA_OUTPUT);
scaleMethod = Crop.ScaleMethod.values()[extras.getInt(Crop.Extra.SCALE_METHOD, 0)];
}

sourceUri = intent.getData();
Expand Down Expand Up @@ -351,11 +355,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)) {
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 @@ -452,5 +474,4 @@ private void setResultUri(Uri uri) {
private void setResultException(Throwable throwable) {
setResult(Crop.RESULT_ERROR, new Intent().putExtra(Crop.Extra.ERROR, throwable));
}

}

0 comments on commit 035fe7e

Please sign in to comment.