Skip to content

Commit f4e36aa

Browse files
committed
Merge pull request jdamcd#194 from chrislacy/master
Can specify resulting image to be saved as a PNG
2 parents 415e7bd + f5440af commit f4e36aa

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

lib/src/androidTest/java/com/soundcloud/android/crop/CropBuilderTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,11 @@ public void testBuildsIntentWithMultipleOptions() {
7474
assertThat(intent.getIntExtra("max_y", 0)).isEqualTo(200);
7575
}
7676

77+
public void testAsPngSetAsExtras() {
78+
builder.asPng(true);
79+
80+
Intent intent = builder.getIntent(activity);
81+
82+
assertThat(intent.getBooleanExtra("as_png", false)).isEqualTo(true);
83+
}
7784
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ interface Extra {
2525
String ASPECT_Y = "aspect_y";
2626
String MAX_X = "max_x";
2727
String MAX_Y = "max_y";
28+
String AS_PNG = "as_png";
2829
String ERROR = "error";
2930
}
3031

@@ -79,6 +80,15 @@ public Crop withMaxSize(int width, int height) {
7980
return this;
8081
}
8182

83+
/**
84+
* Set whether to save the result as a PNG or not. Helpful to preserve alpha.
85+
* @param asPng whether to save the result as a PNG or not
86+
*/
87+
public Crop asPng(boolean asPng) {
88+
cropIntent.putExtra(Extra.AS_PNG, asPng);
89+
return this;
90+
}
91+
8292
/**
8393
* Send the crop Intent from an Activity
8494
*

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public class CropImageActivity extends MonitoredActivity {
5656
private int maxX;
5757
private int maxY;
5858
private int exifRotation;
59+
private boolean saveAsPng;
5960

6061
private Uri sourceUri;
6162
private Uri saveUri;
@@ -125,6 +126,7 @@ private void loadInput() {
125126
aspectY = extras.getInt(Crop.Extra.ASPECT_Y);
126127
maxX = extras.getInt(Crop.Extra.MAX_X);
127128
maxY = extras.getInt(Crop.Extra.MAX_Y);
129+
saveAsPng = extras.getBoolean(Crop.Extra.AS_PNG, false);
128130
saveUri = extras.getParcelable(MediaStore.EXTRA_OUTPUT);
129131
}
130132

@@ -381,7 +383,9 @@ private void saveOutput(Bitmap croppedImage) {
381383
try {
382384
outputStream = getContentResolver().openOutputStream(saveUri);
383385
if (outputStream != null) {
384-
croppedImage.compress(Bitmap.CompressFormat.JPEG, 90, outputStream);
386+
croppedImage.compress(saveAsPng ? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG,
387+
90, // note: quality is ignored when using PNG
388+
outputStream);
385389
}
386390
} catch (IOException e) {
387391
setResultException(e);

0 commit comments

Comments
 (0)