|
61 | 61 | import java.lang.reflect.*; |
62 | 62 | import java.util.*; |
63 | 63 | import java.text.Collator; |
| 64 | +import java.lang.Math; |
64 | 65 |
|
65 | 66 | import java.security.KeyStore; |
66 | 67 | import java.security.cert.CertificateFactory; |
@@ -138,6 +139,7 @@ public class Engine extends View implements EngineApi |
138 | 139 | private boolean m_new_intent; |
139 | 140 |
|
140 | 141 | private int m_photo_width, m_photo_height; |
| 142 | + private int m_jpeg_quality; |
141 | 143 |
|
142 | 144 | private int m_night_mode; |
143 | 145 |
|
@@ -241,6 +243,7 @@ public void onScreenOrientationChanged(int orientation) |
241 | 243 |
|
242 | 244 | m_photo_width = 0; |
243 | 245 | m_photo_height = 0; |
| 246 | + m_jpeg_quality = 100; |
244 | 247 |
|
245 | 248 | m_night_mode = |
246 | 249 | p_context.getResources().getConfiguration().uiMode & |
@@ -2090,10 +2093,11 @@ private ArrayList<String> getPermissionsForGroup(String p_group_name) |
2090 | 2093 | } |
2091 | 2094 |
|
2092 | 2095 |
|
2093 | | - public void showPhotoPicker(String p_source, int p_width, int p_height) |
| 2096 | + public void showPhotoPicker(String p_source, int p_width, int p_height, int p_jpeg_quality) |
2094 | 2097 | { |
2095 | 2098 | m_photo_width = p_width; |
2096 | 2099 | m_photo_height = p_height; |
| 2100 | + m_jpeg_quality = p_jpeg_quality; |
2097 | 2101 |
|
2098 | 2102 | if (p_source.contains("camera")) |
2099 | 2103 | showCamera(p_source); |
@@ -2205,27 +2209,91 @@ else if (resultCode == Activity.RESULT_OK) |
2205 | 2209 | t_photo_uri = Uri.fromFile(m_temp_image_file); |
2206 | 2210 | else |
2207 | 2211 | t_photo_uri = data.getData(); |
| 2212 | + |
2208 | 2213 | InputStream t_in = ((LiveCodeActivity)getContext()).getContentResolver().openInputStream(t_photo_uri); |
2209 | 2214 | ByteArrayOutputStream t_out = new ByteArrayOutputStream(); |
2210 | | - byte[] t_buffer = new byte[4096]; |
2211 | | - int t_readcount; |
2212 | | - while (-1 != (t_readcount = t_in.read(t_buffer))) |
2213 | | - { |
2214 | | - t_out.write(t_buffer, 0, t_readcount); |
2215 | | - } |
2216 | | - |
| 2215 | + |
2217 | 2216 | // HH-2017-01-19: [[ Bug 11313 ]]Support maximum width and height of the image |
2218 | 2217 | if(m_photo_height > 0 && m_photo_width > 0) |
2219 | 2218 | { |
2220 | | - Bitmap bm = BitmapFactory.decodeByteArray(t_out.toByteArray(), 0, t_out.size()); |
2221 | | - Bitmap rBm = Bitmap.createScaledBitmap(bm, m_photo_width, m_photo_height, true); |
2222 | | - ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
2223 | | - rBm.compress(Bitmap.CompressFormat.PNG, 100, stream); |
2224 | | - byte[] byteArray = stream.toByteArray(); |
2225 | | - doPhotoPickerDone(byteArray, byteArray.length); |
| 2219 | + Bitmap t_bitmap = BitmapFactory.decodeStream(t_in); |
| 2220 | + |
| 2221 | + // scale to required max width/height |
| 2222 | + float t_width = t_bitmap.getWidth(); |
| 2223 | + float t_height = t_bitmap.getHeight(); |
| 2224 | + |
| 2225 | + InputStream t_exif_in = ((LiveCodeActivity)getContext()).getContentResolver().openInputStream(t_photo_uri); |
| 2226 | + |
| 2227 | + ExifInterface t_exif = new ExifInterface(t_exif_in); |
| 2228 | + |
| 2229 | + int t_orientation = t_exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, |
| 2230 | + ExifInterface.ORIENTATION_NORMAL); |
| 2231 | + |
| 2232 | + /* Max width and height need to be flipped if the image orientation is |
| 2233 | + * requires a 90 or 270 degree rotation */ |
| 2234 | + int t_max_width; |
| 2235 | + int t_max_height; |
| 2236 | + switch (t_orientation) |
| 2237 | + { |
| 2238 | + case ExifInterface.ORIENTATION_TRANSPOSE: |
| 2239 | + case ExifInterface.ORIENTATION_ROTATE_90: |
| 2240 | + case ExifInterface.ORIENTATION_TRANSVERSE: |
| 2241 | + case ExifInterface.ORIENTATION_ROTATE_270: |
| 2242 | + t_max_height = m_photo_width; |
| 2243 | + t_max_width = m_photo_height; |
| 2244 | + break; |
| 2245 | + default: |
| 2246 | + t_max_width = m_photo_width; |
| 2247 | + t_max_height = m_photo_height; |
| 2248 | + break; |
| 2249 | + } |
| 2250 | + |
| 2251 | + float t_scale = Math.min(t_max_width / t_width, t_max_height / t_height); |
| 2252 | + |
| 2253 | + Matrix t_matrix = new Matrix(); |
| 2254 | + t_matrix.setScale(t_scale, t_scale, 0, 0); |
| 2255 | + |
| 2256 | + switch (t_orientation) |
| 2257 | + { |
| 2258 | + case ExifInterface.ORIENTATION_FLIP_HORIZONTAL: |
| 2259 | + t_matrix.postScale(-1, 1); |
| 2260 | + break; |
| 2261 | + case ExifInterface.ORIENTATION_ROTATE_180: |
| 2262 | + t_matrix.postRotate(180); |
| 2263 | + break; |
| 2264 | + case ExifInterface.ORIENTATION_FLIP_VERTICAL: |
| 2265 | + t_matrix.postRotate(180); |
| 2266 | + t_matrix.postScale(-1, 1); |
| 2267 | + break; |
| 2268 | + case ExifInterface.ORIENTATION_TRANSPOSE: |
| 2269 | + t_matrix.postRotate(90); |
| 2270 | + t_matrix.postScale(-1, 1); |
| 2271 | + break; |
| 2272 | + case ExifInterface.ORIENTATION_ROTATE_90: |
| 2273 | + t_matrix.postRotate(90); |
| 2274 | + break; |
| 2275 | + case ExifInterface.ORIENTATION_TRANSVERSE: |
| 2276 | + t_matrix.postRotate(-90); |
| 2277 | + t_matrix.postScale(-1, 1); |
| 2278 | + break; |
| 2279 | + case ExifInterface.ORIENTATION_ROTATE_270: |
| 2280 | + t_matrix.postRotate(-90); |
| 2281 | + break; |
| 2282 | + } |
| 2283 | + |
| 2284 | + Bitmap t_scaled_bitmap = Bitmap.createBitmap(t_bitmap, 0, 0, (int)t_width, (int)t_height, t_matrix, true); |
| 2285 | + t_scaled_bitmap.compress(Bitmap.CompressFormat.JPEG, m_jpeg_quality, t_out); |
2226 | 2286 | } |
2227 | 2287 | else |
2228 | | - doPhotoPickerDone(t_out.toByteArray(), t_out.size()); |
| 2288 | + { |
| 2289 | + byte[] t_buffer = new byte[4096]; |
| 2290 | + int t_readcount; |
| 2291 | + while (-1 != (t_readcount = t_in.read(t_buffer))) |
| 2292 | + { |
| 2293 | + t_out.write(t_buffer, 0, t_readcount); |
| 2294 | + } |
| 2295 | + } |
| 2296 | + doPhotoPickerDone(t_out.toByteArray(), t_out.size()); |
2229 | 2297 |
|
2230 | 2298 | t_in.close(); |
2231 | 2299 | t_out.close(); |
|
0 commit comments