OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
Theresa
2017/03/31 18:05:55
Is this class being used anywhere currently?
Finnur
2017/04/03 17:30:29
Huh... You are right. Removed.
| |
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 = createSizingOptions(); | |
34 BitmapFactory.decodeFileDescriptor(descriptor, null, options); | |
35 Bitmap bitmap = BitmapFactory.decodeFileDescriptor( | |
Theresa
2017/03/31 18:05:55
Why does this call decodeFileDescriptor twice? Sam
Finnur
2017/04/03 17:30:29
Because that's how the sub-sampling is implemented
| |
36 descriptor, null, createDecodingOptions(options, width)); | |
37 | |
38 if (bitmap == null) return null; | |
39 | |
40 return sizeBitmap(bitmap, width); | |
41 } | |
42 | |
43 /** | |
44 * Given a file path, decodes the contents of the file and returns a bitmap of size | |
45 * |width|x|width|. | |
46 * @param filePath The path to the file to read. | |
47 * @param width The width of the bitmap to return. | |
48 * @return The resulting bitmap. | |
49 */ | |
50 public static Bitmap decodeBitmapFromDisk(String filePath, int width) { | |
51 BitmapFactory.Options options = createSizingOptions(); | |
52 BitmapFactory.decodeFile(filePath, options); | |
53 Bitmap bitmap = BitmapFactory.decodeFile(filePath, createDecodingOptions (options, width)); | |
54 | |
55 if (bitmap == null) return null; | |
56 | |
57 return sizeBitmap(bitmap, width); | |
58 } | |
59 | |
60 /** | |
61 * Creates a {@link BitmapFactory#Options} object ready for size calculation s. | |
62 */ | |
63 private static BitmapFactory.Options createSizingOptions() { | |
64 BitmapFactory.Options options = new BitmapFactory.Options(); | |
65 options.inJustDecodeBounds = true; | |
66 return options; | |
67 } | |
68 | |
69 /** | |
70 * Converts a {@link BitmapFactory#Options} object for size calculations int o a decoding options | |
71 * object, with optimal decoding size specified. | |
72 */ | |
73 private static BitmapFactory.Options createDecodingOptions( | |
74 BitmapFactory.Options sizingOptions, int width) { | |
75 sizingOptions.inSampleSize = calculateInSampleSize(sizingOptions, width, width); | |
76 sizingOptions.inJustDecodeBounds = false; | |
77 return sizingOptions; | |
78 } | |
79 | |
80 /** | |
81 * Calculates the sub-sampling factor {@link BitmapFactory#inSampleSize} opt ion for a given | |
82 * bitmap option, which will be used to create a bitmap of a pre-determined size (no larger than | |
83 * |width| and |height|). | |
84 * @param options The bitmap options to consider. | |
85 * @param width The requested width. | |
86 * @param height The requested height. | |
87 * @return The sub-sampling factor (1 = no change, 2 = half-size, etc). | |
88 */ | |
89 private static int calculateInSampleSize(BitmapFactory.Options options, int width, int height) { | |
90 int inSampleSize = 1; | |
91 | |
92 if (options.outHeight > height || options.outWidth > width) { | |
93 final int halfHeight = options.outHeight / 2; | |
94 final int halfWidth = options.outWidth / 2; | |
95 | |
96 while ((halfHeight / inSampleSize) >= height || (halfWidth / inSampl eSize) >= width) { | |
97 inSampleSize *= 2; | |
98 } | |
99 } | |
100 | |
101 return inSampleSize; | |
102 } | |
103 | |
104 /** | |
105 * Scales a |bitmap| to a certain size. | |
106 * @param bitmap The bitmap to scale. | |
107 * @param scaleMaxSize What to scale it to. | |
108 * @param filter True if the source should be filtered. | |
109 * @return The resulting scaled bitmap. | |
110 */ | |
111 public static Bitmap scale(Bitmap bitmap, float scaleMaxSize, boolean filter ) { | |
112 float ratio = Math.min((float) scaleMaxSize / bitmap.getWidth(), | |
113 (float) scaleMaxSize / bitmap.getHeight()); | |
114 int height = Math.round(ratio * bitmap.getHeight()); | |
115 int width = Math.round(ratio * bitmap.getWidth()); | |
116 | |
117 return Bitmap.createScaledBitmap(bitmap, width, height, filter); | |
118 } | |
119 | |
120 /** | |
121 * Ensures a |bitmap| is at least |size| in both width and height. | |
122 * @param bitmap The bitmap to modify. | |
123 * @param size The minimum size (width and height). | |
124 * @return The resulting (scaled) bitmap. | |
125 */ | |
126 private static Bitmap ensureMinSize(Bitmap bitmap, int size) { | |
127 int width = bitmap.getWidth(); | |
128 int height = bitmap.getHeight(); | |
129 if (width < size) { | |
130 float scale = (float) size / width; | |
131 width = size; | |
132 height *= scale; | |
133 } | |
134 | |
135 if (height < size) { | |
136 float scale = (float) size / height; | |
137 height = size; | |
138 width *= scale; | |
139 } | |
140 | |
141 return Bitmap.createScaledBitmap(bitmap, width, height, true); | |
142 } | |
143 | |
144 /** | |
145 * Crops a |bitmap| to a certain square |size| | |
146 * @param bitmap The bitmap to crop. | |
147 * @param size The size desired (width and height). | |
148 * @return The resulting (square) bitmap. | |
149 */ | |
150 private static Bitmap cropToSquare(Bitmap bitmap, int size) { | |
151 int x = 0; | |
152 int y = 0; | |
153 int width = bitmap.getWidth(); | |
154 int height = bitmap.getHeight(); | |
155 | |
156 if (width > size) x = (width - size) / 2; | |
157 if (height > size) y = (height - size) / 2; | |
158 return Bitmap.createBitmap(bitmap, x, y, size, size); | |
159 } | |
160 } | |
OLD | NEW |