|
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;
|
@@ -56,6 +58,7 @@ public class CropImageActivity extends MonitoredActivity {
|
56 | 58 | private int maxX;
|
57 | 59 | private int maxY;
|
58 | 60 | private int exifRotation;
|
| 61 | + private Crop.ScaleMethod scaleMethod; |
59 | 62 |
|
60 | 63 | private Uri sourceUri;
|
61 | 64 | private Uri saveUri;
|
@@ -126,6 +129,7 @@ private void loadInput() {
|
126 | 129 | maxX = extras.getInt(Crop.Extra.MAX_X);
|
127 | 130 | maxY = extras.getInt(Crop.Extra.MAX_Y);
|
128 | 131 | saveUri = extras.getParcelable(MediaStore.EXTRA_OUTPUT);
|
| 132 | + scaleMethod = Crop.ScaleMethod.values()[extras.getInt(Crop.Extra.SCALE_METHOD, 0)]; |
129 | 133 | }
|
130 | 134 |
|
131 | 135 | sourceUri = intent.getData();
|
@@ -343,11 +347,29 @@ private Bitmap decodeRegionCrop(Rect rect, int outWidth, int outHeight) {
|
343 | 347 | }
|
344 | 348 |
|
345 | 349 | try {
|
346 |
| - croppedImage = decoder.decodeRegion(rect, new BitmapFactory.Options()); |
347 |
| - if (croppedImage != null && (rect.width() > outWidth || rect.height() > outHeight)) { |
348 |
| - Matrix matrix = new Matrix(); |
349 |
| - matrix.postScale((float) outWidth / rect.width(), (float) outHeight / rect.height()); |
350 |
| - croppedImage = Bitmap.createBitmap(croppedImage, 0, 0, croppedImage.getWidth(), croppedImage.getHeight(), matrix, true); |
| 350 | + BitmapFactory.Options options = new BitmapFactory.Options(); |
| 351 | + if ((rect.width() > outWidth || rect.height() > outHeight)) { |
| 352 | + switch (scaleMethod) { |
| 353 | + case EXACT: |
| 354 | + croppedImage = decoder.decodeRegion(rect, options); |
| 355 | + Matrix matrix = new Matrix(); |
| 356 | + matrix.postScale((float) outWidth / rect.width(), (float) outHeight / rect.height()); |
| 357 | + croppedImage = Bitmap.createBitmap(croppedImage, 0, 0, croppedImage.getWidth(), croppedImage.getHeight(), matrix, true); |
| 358 | + break; |
| 359 | + case BETTER_QUALITY_BEST_FIT: |
| 360 | + int w, h; |
| 361 | + int inSampleSize = 1; |
| 362 | + do { |
| 363 | + inSampleSize *= 2; |
| 364 | + w = rect.width() / inSampleSize; |
| 365 | + h = rect.height() / inSampleSize; |
| 366 | + } while(w > outWidth && h > outHeight); |
| 367 | + options.inSampleSize = inSampleSize; |
| 368 | + croppedImage = decoder.decodeRegion(rect, options); |
| 369 | + break; |
| 370 | + } |
| 371 | + } else { |
| 372 | + croppedImage = decoder.decodeRegion(rect, options); |
351 | 373 | }
|
352 | 374 | } catch (IllegalArgumentException e) {
|
353 | 375 | // Rethrow with some extra information
|
@@ -433,5 +455,4 @@ private void setResultUri(Uri uri) {
|
433 | 455 | private void setResultException(Throwable throwable) {
|
434 | 456 | setResult(Crop.RESULT_ERROR, new Intent().putExtra(Crop.Extra.ERROR, throwable));
|
435 | 457 | }
|
436 |
| - |
437 | 458 | }
|
0 commit comments