Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
|
Theresa
2017/03/28 20:40:27
s/2016/2017
Finnur
2017/03/31 14:26:49
Done.
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chrome.browser.photo_picker; | |
| 6 | |
| 7 import android.graphics.Bitmap; | |
| 8 import android.graphics.BitmapFactory; | |
| 9 | |
| 10 import java.io.FileDescriptor; | |
| 11 | |
| 12 class BitmapUtils { | |
| 13 /** | |
| 14 * Takes a |bitmap| and returns a square thumbnail of |width| from the cente r of the bitmap | |
| 15 * specified. | |
| 16 * @param bitmap The bitmap to adjust. | |
| 17 * @param width The desired width. | |
| 18 * @return The new bitmap thumbnail. | |
| 19 */ | |
| 20 public static Bitmap sizeBitmap(Bitmap bitmap, int width) { | |
| 21 bitmap = ensureMinSize(bitmap, width); | |
| 22 bitmap = cropToSquare(bitmap, width); | |
| 23 return bitmap; | |
| 24 } | |
| 25 | |
| 26 /** | |
| 27 * Given a FileDescriptor, decodes the contents and returns a bitmap of size |width|x|width|. | |
| 28 * @param descriptor The FileDescriptor for the file to read. | |
| 29 * @param width The width of the bitmap to return. | |
| 30 * @return The resulting bitmap. | |
| 31 */ | |
| 32 public static Bitmap decodeBitmapFromFileDescriptor(FileDescriptor descripto r, int width) { | |
| 33 BitmapFactory.Options options = new BitmapFactory.Options(); | |
| 34 options.inJustDecodeBounds = true; | |
| 35 BitmapFactory.decodeFileDescriptor(descriptor, null, options); | |
| 36 options.inSampleSize = calculateInSampleSize(options, width, width); | |
| 37 options.inJustDecodeBounds = false; | |
| 38 Bitmap bitmap = BitmapFactory.decodeFileDescriptor(descriptor, null, opt ions); | |
| 39 if (bitmap == null) { | |
| 40 return null; | |
|
Theresa
2017/03/28 20:40:27
nit: in line "return null" and add blank space:
Finnur
2017/03/31 14:26:49
Done.
| |
| 41 } | |
| 42 return sizeBitmap(bitmap, width); | |
| 43 } | |
| 44 | |
| 45 /** | |
| 46 * Given a file path, decodes the contents of the file and returns a bitmap of size | |
| 47 * |width|x|width|. | |
| 48 * @param filePath The path to the file to read. | |
| 49 * @param width The width of the bitmap to return. | |
| 50 * @return The resulting bitmap. | |
| 51 */ | |
| 52 public static Bitmap decodeBitmapFromDisk(String filePath, int width) { | |
| 53 BitmapFactory.Options options = new BitmapFactory.Options(); | |
| 54 options.inJustDecodeBounds = true; | |
| 55 BitmapFactory.decodeFile(filePath, options); | |
| 56 options.inSampleSize = calculateInSampleSize(options, width, width); | |
| 57 options.inJustDecodeBounds = false; | |
|
Theresa
2017/03/28 20:40:28
nit: Maybe there should be a helper method that cr
Finnur
2017/03/31 14:26:49
Done (I think).
| |
| 58 Bitmap bitmap = BitmapFactory.decodeFile(filePath, options); | |
| 59 if (bitmap == null) { | |
| 60 return null; | |
| 61 } | |
| 62 return sizeBitmap(bitmap, width); | |
| 63 } | |
| 64 | |
| 65 /** | |
| 66 * Calculates the sub-sampling factor (inSampleSize option from the BitmapFa ctory) for a given | |
|
Theresa
2017/03/28 20:40:27
nit: {@link BitmapFactory#inSampleSize}?
Finnur
2017/03/31 14:26:49
Done.
| |
| 67 * bitmap option, which will be used to create a bitmap of a pre-determined size (no larger than | |
| 68 * |width| and |height|). | |
| 69 * @param options The bitmap options to consider. | |
| 70 * @param width The requested width. | |
| 71 * @param height The requested height. | |
| 72 * @return The sub-sampling factor (1 = no change, 2 = half-size, etc). | |
| 73 */ | |
| 74 private static int calculateInSampleSize(BitmapFactory.Options options, int width, int height) { | |
| 75 int inSampleSize = 1; | |
| 76 | |
| 77 if (options.outHeight > height || options.outWidth > width) { | |
| 78 final int halfHeight = options.outHeight / 2; | |
| 79 final int halfWidth = options.outWidth / 2; | |
| 80 | |
| 81 while ((halfHeight / inSampleSize) >= height || (halfWidth / inSampl eSize) >= width) { | |
| 82 inSampleSize *= 2; | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 return inSampleSize; | |
| 87 } | |
| 88 | |
| 89 /** | |
| 90 * Scales a |bitmap| to a certain size. | |
| 91 * @param bitmap The bitmap to scale. | |
| 92 * @param scaleMaxSize What to scale it to. | |
| 93 * @param filter true if the source should be filtered. | |
|
Theresa
2017/03/28 20:40:27
nit: s/true/True
Finnur
2017/03/31 14:26:49
Done.
| |
| 94 * @return The resulting scaled bitmap. | |
| 95 */ | |
| 96 public static Bitmap scale(Bitmap bitmap, float scaleMaxSize, boolean filter ) { | |
| 97 float ratio = Math.min((float) scaleMaxSize / bitmap.getWidth(), | |
| 98 (float) scaleMaxSize / bitmap.getHeight()); | |
| 99 int height = Math.round(ratio * bitmap.getHeight()); | |
| 100 int width = Math.round(ratio * bitmap.getWidth()); | |
| 101 | |
| 102 return Bitmap.createScaledBitmap(bitmap, width, height, filter); | |
| 103 } | |
| 104 | |
| 105 /** | |
| 106 * Ensures a |bitmap| is at least |size| in both width and height. | |
| 107 * @param bitmap The bitmap to modify. | |
| 108 * @param size The minimum size (width and height). | |
| 109 * @return The resulting (scaled) bitmap. | |
| 110 */ | |
| 111 private static Bitmap ensureMinSize(Bitmap bitmap, int size) { | |
| 112 int width = bitmap.getWidth(); | |
| 113 int height = bitmap.getHeight(); | |
| 114 if (width < size) { | |
| 115 float scale = (float) size / width; | |
| 116 width = size; | |
| 117 height *= scale; | |
| 118 } | |
| 119 | |
| 120 if (height < size) { | |
| 121 float scale = (float) size / height; | |
| 122 height = size; | |
| 123 width *= scale; | |
| 124 } | |
| 125 | |
| 126 return Bitmap.createScaledBitmap(bitmap, width, height, true); | |
| 127 } | |
| 128 | |
| 129 /** | |
| 130 * Crops a |bitmap| to a certain square |size| | |
| 131 * @param bitmap The bitmap to crop. | |
| 132 * @param size The size desired (width and height). | |
| 133 * @return The resulting (square) bitmap. | |
| 134 */ | |
| 135 private static Bitmap cropToSquare(Bitmap bitmap, int size) { | |
| 136 int x = 0; | |
| 137 int y = 0; | |
| 138 int width = bitmap.getWidth(); | |
| 139 int height = bitmap.getHeight(); | |
| 140 | |
| 141 if (width > size) x = (width - size) / 2; | |
| 142 if (height > size) y = (height - size) / 2; | |
| 143 return Bitmap.createBitmap(bitmap, x, y, size, size); | |
| 144 } | |
| 145 } | |
| OLD | NEW |