Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/photo_picker/BitmapUtils.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/photo_picker/BitmapUtils.java b/chrome/android/java/src/org/chromium/chrome/browser/photo_picker/BitmapUtils.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3b80472a31da673a70a4cadadef1f31cfb42efd1 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/photo_picker/BitmapUtils.java |
| @@ -0,0 +1,125 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.photo_picker; |
| + |
| +import android.graphics.Bitmap; |
| +import android.graphics.BitmapFactory; |
| + |
| +import java.io.FileDescriptor; |
| + |
| +class BitmapUtils { |
|
Theresa
2017/04/13 02:25:26
nit: JavaDoc for the class
Finnur
2017/04/18 17:21:14
Done.
|
| + /** |
| + * Takes a |bitmap| and returns a square thumbnail of |width| from the center of the bitmap |
| + * specified. |
| + * @param bitmap The bitmap to adjust. |
| + * @param width The desired width. |
| + * @return The new bitmap thumbnail. |
| + */ |
| + public static Bitmap sizeBitmap(Bitmap bitmap, int width) { |
| + bitmap = ensureMinSize(bitmap, width); |
| + bitmap = cropToSquare(bitmap, width); |
|
Theresa
2017/04/13 02:25:27
I think that instead of manipulating the Bitmap he
Finnur
2017/04/18 17:21:14
That's a good point. I'll take a look at this tomo
|
| + return bitmap; |
| + } |
| + |
| + /** |
| + * Given a FileDescriptor, decodes the contents and returns a bitmap of size |width|x|width|. |
| + * @param descriptor The FileDescriptor for the file to read. |
| + * @param width The width of the bitmap to return. |
| + * @return The resulting bitmap. |
| + */ |
| + public static Bitmap decodeBitmapFromFileDescriptor(FileDescriptor descriptor, int width) { |
| + BitmapFactory.Options options = new BitmapFactory.Options(); |
| + options.inJustDecodeBounds = true; |
| + BitmapFactory.decodeFileDescriptor(descriptor, null, options); |
| + options.inSampleSize = calculateInSampleSize(options, width, width); |
| + options.inJustDecodeBounds = false; |
| + Bitmap bitmap = BitmapFactory.decodeFileDescriptor(descriptor, null, options); |
| + |
| + if (bitmap == null) return null; |
| + |
| + return sizeBitmap(bitmap, width); |
| + } |
| + |
| + /** |
| + * Calculates the sub-sampling factor {@link BitmapFactory#inSampleSize} option for a given |
| + * bitmap option, which will be used to create a bitmap of a pre-determined size (no larger than |
| + * |width| and |height|). |
| + * @param options The bitmap options to consider. |
| + * @param width The requested width. |
| + * @param height The requested height. |
| + * @return The sub-sampling factor (1 = no change, 2 = half-size, etc). |
| + */ |
| + private static int calculateInSampleSize(BitmapFactory.Options options, int width, int height) { |
|
Theresa
2017/04/13 02:25:26
nit: this method can be private
Also, since the w
Finnur
2017/04/18 17:21:14
Not sure I understand. It already is...
If you mea
|
| + int inSampleSize = 1; |
| + |
| + if (options.outHeight > height || options.outWidth > width) { |
| + final int halfHeight = options.outHeight / 2; |
| + final int halfWidth = options.outWidth / 2; |
| + |
| + while ((halfHeight / inSampleSize) >= height || (halfWidth / inSampleSize) >= width) { |
|
Theresa
2017/04/13 02:25:26
I think the math for calculating the inSampleSize
Finnur
2017/04/18 17:21:14
The idea is to produce a photo that is no larger i
Theresa
2017/04/18 19:06:20
Shouldn't we ensure that the photo is at least |si
Finnur
2017/04/19 16:01:35
The sweet spot in my mind is the highest inSampleS
Theresa
2017/04/24 16:57:38
That's a bit surprising since CENTER_CROP's docume
|
| + inSampleSize *= 2; |
| + } |
| + } |
| + |
| + return inSampleSize; |
| + } |
| + |
| + /** |
| + * Scales a |bitmap| to a certain size. |
| + * @param bitmap The bitmap to scale. |
| + * @param scaleMaxSize What to scale it to. |
| + * @param filter True if the source should be filtered. |
| + * @return The resulting scaled bitmap. |
| + */ |
| + public static Bitmap scale(Bitmap bitmap, float scaleMaxSize, boolean filter) { |
|
Theresa
2017/04/13 02:25:26
Where is this method called from?
Finnur
2017/04/18 17:21:14
Hmm... it isn't (yet)... Removed.
|
| + float ratio = Math.min((float) scaleMaxSize / bitmap.getWidth(), |
| + (float) scaleMaxSize / bitmap.getHeight()); |
| + int height = Math.round(ratio * bitmap.getHeight()); |
| + int width = Math.round(ratio * bitmap.getWidth()); |
| + |
| + return Bitmap.createScaledBitmap(bitmap, width, height, filter); |
| + } |
| + |
| + /** |
| + * Ensures a |bitmap| is at least |size| in both width and height. |
| + * @param bitmap The bitmap to modify. |
| + * @param size The minimum size (width and height). |
| + * @return The resulting (scaled) bitmap. |
| + */ |
| + private static Bitmap ensureMinSize(Bitmap bitmap, int size) { |
| + int width = bitmap.getWidth(); |
| + int height = bitmap.getHeight(); |
| + if (width < size) { |
| + float scale = (float) size / width; |
| + width = size; |
| + height *= scale; |
| + } |
| + |
| + if (height < size) { |
| + float scale = (float) size / height; |
| + height = size; |
| + width *= scale; |
| + } |
| + |
| + return Bitmap.createScaledBitmap(bitmap, width, height, true); |
| + } |
| + |
| + /** |
| + * Crops a |bitmap| to a certain square |size| |
| + * @param bitmap The bitmap to crop. |
| + * @param size The size desired (width and height). |
| + * @return The resulting (square) bitmap. |
| + */ |
| + private static Bitmap cropToSquare(Bitmap bitmap, int size) { |
| + int x = 0; |
| + int y = 0; |
| + int width = bitmap.getWidth(); |
| + int height = bitmap.getHeight(); |
| + |
| + if (width > size) x = (width - size) / 2; |
| + if (height > size) y = (height - size) / 2; |
| + return Bitmap.createBitmap(bitmap, x, y, size, size); |
| + } |
| +} |