Skip to content

Commit 035fe7e

Browse files
committed
Merge remote-tracking branch 'blinkmacalahan/master'
# Conflicts: # lib/src/main/java/com/soundcloud/android/crop/Crop.java # lib/src/main/java/com/soundcloud/android/crop/CropImageActivity.java
2 parents 03d7b1e + b46aa27 commit 035fe7e

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

lib/src/main/java/com/soundcloud/android/crop/Crop.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ interface Extra {
2727
String MAX_Y = "max_y";
2828
String AS_PNG = "as_png";
2929
String ERROR = "error";
30+
String SCALE_METHOD = "scale_method";
31+
}
32+
33+
public enum ScaleMethod {
34+
/**
35+
* Using the exact dimensions provided and using a scaling function for creating a scale bitmap. When scaling a large amount, the quality can suffer.
36+
*/
37+
EXACT,
38+
/**
39+
* 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
40+
* dimensions exactly requested. This will result in better quality images when the scale amount is large.
41+
*/
42+
BETTER_QUALITY_BEST_FIT;
3043
}
3144

3245
private Intent cropIntent;
@@ -45,6 +58,7 @@ private Crop(Uri source, Uri destination) {
4558
cropIntent = new Intent();
4659
cropIntent.setData(source);
4760
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, destination);
61+
cropIntent.putExtra(Extra.SCALE_METHOD, ScaleMethod.EXACT.ordinal());
4862
}
4963

5064
/**
@@ -80,6 +94,16 @@ public Crop withMaxSize(int width, int height) {
8094
return this;
8195
}
8296

97+
/**
98+
* Set how the image will be scaled when providing {@link Extra#MAX_X} and {@link Extra#MAX_Y} with the {@link #withMaxSize(int, int)}
99+
* @param type
100+
* @return
101+
*/
102+
public Crop withScaleMethod(ScaleMethod type) {
103+
cropIntent.putExtra(Extra.SCALE_METHOD, type.ordinal());
104+
return this;
105+
}
106+
83107
/**
84108
* Set whether to save the result as a PNG or not. Helpful to preserve alpha.
85109
* @param asPng whether to save the result as a PNG or not

lib/src/main/java/com/soundcloud/android/crop/CropImageActivity.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import android.graphics.Bitmap;
2222
import android.graphics.BitmapFactory;
2323
import android.graphics.BitmapRegionDecoder;
24+
import android.graphics.Canvas;
2425
import android.graphics.Matrix;
26+
import android.graphics.Paint;
2527
import android.graphics.Rect;
2628
import android.graphics.RectF;
2729
import android.net.Uri;
@@ -57,6 +59,7 @@ public class CropImageActivity extends MonitoredActivity {
5759
private int maxY;
5860
private int exifRotation;
5961
private boolean saveAsPng;
62+
private Crop.ScaleMethod scaleMethod;
6063

6164
private Uri sourceUri;
6265
private Uri saveUri;
@@ -128,6 +131,7 @@ private void loadInput() {
128131
maxY = extras.getInt(Crop.Extra.MAX_Y);
129132
saveAsPng = extras.getBoolean(Crop.Extra.AS_PNG, false);
130133
saveUri = extras.getParcelable(MediaStore.EXTRA_OUTPUT);
134+
scaleMethod = Crop.ScaleMethod.values()[extras.getInt(Crop.Extra.SCALE_METHOD, 0)];
131135
}
132136

133137
sourceUri = intent.getData();
@@ -351,11 +355,29 @@ private Bitmap decodeRegionCrop(Rect rect, int outWidth, int outHeight) {
351355
}
352356

353357
try {
354-
croppedImage = decoder.decodeRegion(rect, new BitmapFactory.Options());
355-
if (croppedImage != null && (rect.width() > outWidth || rect.height() > outHeight)) {
356-
Matrix matrix = new Matrix();
357-
matrix.postScale((float) outWidth / rect.width(), (float) outHeight / rect.height());
358-
croppedImage = Bitmap.createBitmap(croppedImage, 0, 0, croppedImage.getWidth(), croppedImage.getHeight(), matrix, true);
358+
BitmapFactory.Options options = new BitmapFactory.Options();
359+
if ((rect.width() > outWidth || rect.height() > outHeight)) {
360+
switch (scaleMethod) {
361+
case EXACT:
362+
croppedImage = decoder.decodeRegion(rect, options);
363+
Matrix matrix = new Matrix();
364+
matrix.postScale((float) outWidth / rect.width(), (float) outHeight / rect.height());
365+
croppedImage = Bitmap.createBitmap(croppedImage, 0, 0, croppedImage.getWidth(), croppedImage.getHeight(), matrix, true);
366+
break;
367+
case BETTER_QUALITY_BEST_FIT:
368+
int w, h;
369+
int inSampleSize = 1;
370+
do {
371+
inSampleSize *= 2;
372+
w = rect.width() / inSampleSize;
373+
h = rect.height() / inSampleSize;
374+
} while(w > outWidth && h > outHeight);
375+
options.inSampleSize = inSampleSize;
376+
croppedImage = decoder.decodeRegion(rect, options);
377+
break;
378+
}
379+
} else {
380+
croppedImage = decoder.decodeRegion(rect, options);
359381
}
360382
} catch (IllegalArgumentException e) {
361383
// Rethrow with some extra information
@@ -452,5 +474,4 @@ private void setResultUri(Uri uri) {
452474
private void setResultException(Throwable throwable) {
453475
setResult(Crop.RESULT_ERROR, new Intent().putExtra(Crop.Extra.ERROR, throwable));
454476
}
455-
456477
}

0 commit comments

Comments
 (0)