|
21 | 21 | import android.graphics.Bitmap; |
22 | 22 | import android.graphics.BitmapFactory; |
23 | 23 | import android.graphics.BitmapRegionDecoder; |
| 24 | +import android.graphics.Canvas; |
24 | 25 | import android.graphics.Matrix; |
| 26 | +import android.graphics.Paint; |
25 | 27 | import android.graphics.Rect; |
26 | 28 | import android.graphics.RectF; |
27 | 29 | import android.net.Uri; |
@@ -57,6 +59,7 @@ public class CropImageActivity extends MonitoredActivity { |
57 | 59 | private int maxY; |
58 | 60 | private int exifRotation; |
59 | 61 | private boolean saveAsPng; |
| 62 | + private Crop.ScaleMethod scaleMethod; |
60 | 63 |
|
61 | 64 | private Uri sourceUri; |
62 | 65 | private Uri saveUri; |
@@ -128,6 +131,7 @@ private void loadInput() { |
128 | 131 | maxY = extras.getInt(Crop.Extra.MAX_Y); |
129 | 132 | saveAsPng = extras.getBoolean(Crop.Extra.AS_PNG, false); |
130 | 133 | saveUri = extras.getParcelable(MediaStore.EXTRA_OUTPUT); |
| 134 | + scaleMethod = Crop.ScaleMethod.values()[extras.getInt(Crop.Extra.SCALE_METHOD, 0)]; |
131 | 135 | } |
132 | 136 |
|
133 | 137 | sourceUri = intent.getData(); |
@@ -351,11 +355,29 @@ private Bitmap decodeRegionCrop(Rect rect, int outWidth, int outHeight) { |
351 | 355 | } |
352 | 356 |
|
353 | 357 | 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); |
359 | 381 | } |
360 | 382 | } catch (IllegalArgumentException e) { |
361 | 383 | // Rethrow with some extra information |
@@ -452,5 +474,4 @@ private void setResultUri(Uri uri) { |
452 | 474 | private void setResultException(Throwable throwable) { |
453 | 475 | setResult(Crop.RESULT_ERROR, new Intent().putExtra(Crop.Extra.ERROR, throwable)); |
454 | 476 | } |
455 | | - |
456 | 477 | } |
0 commit comments