Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 28bf1ea

Browse files
committed
[[ Bug 22663 ]] Support max width/height in Android mobilePickPhoto
This patch adds support for the max width and height parameters for `mobilePickPhoto` on android. A previous patch added some support, however, the behavior was quite different to iOS and the documentation was not updated.
1 parent 0115d5d commit 28bf1ea

File tree

4 files changed

+88
-20
lines changed

4 files changed

+88
-20
lines changed

docs/dictionary/command/mobilePickPhoto.lcdoc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,9 @@ the command returns with result cancel. Otherwise a new image object is
6262
created on the current card of the default stack containing the chosen
6363
image.
6464

65-
The <maxwidth> and <maxheight> parameters are only available on iOS
66-
devices and constrain the maximum size of an image. The chosen image is
67-
scaled down proportionally to fit within the size specified. If either
68-
size specified is 0, then the parameter is ignored.
65+
The <maxwidth> and <maxheight> parameters constrain the maximum size of an
66+
image. The chosen image is scaled down proportionally to fit within the size
67+
specified. If either size specified is 0, then the parameter is ignored.
6968

7069
>*Note:* The image object is cloned from the <templateImage>, so you can
7170
> use this to configure settings before calling the picker.

docs/notes/bugfix-22663.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Add support for max width and max height parameters to mobilePickPhoto on Android

engine/src/java/com/runrev/android/Engine.java

Lines changed: 83 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import java.lang.reflect.*;
6262
import java.util.*;
6363
import java.text.Collator;
64+
import java.lang.Math;
6465

6566
import java.security.KeyStore;
6667
import java.security.cert.CertificateFactory;
@@ -138,6 +139,7 @@ public class Engine extends View implements EngineApi
138139
private boolean m_new_intent;
139140

140141
private int m_photo_width, m_photo_height;
142+
private int m_jpeg_quality;
141143

142144
private int m_night_mode;
143145

@@ -241,6 +243,7 @@ public void onScreenOrientationChanged(int orientation)
241243

242244
m_photo_width = 0;
243245
m_photo_height = 0;
246+
m_jpeg_quality = 100;
244247

245248
m_night_mode =
246249
p_context.getResources().getConfiguration().uiMode &
@@ -2090,10 +2093,11 @@ private ArrayList<String> getPermissionsForGroup(String p_group_name)
20902093
}
20912094

20922095

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)
20942097
{
20952098
m_photo_width = p_width;
20962099
m_photo_height = p_height;
2100+
m_jpeg_quality = p_jpeg_quality;
20972101

20982102
if (p_source.contains("camera"))
20992103
showCamera(p_source);
@@ -2205,27 +2209,91 @@ else if (resultCode == Activity.RESULT_OK)
22052209
t_photo_uri = Uri.fromFile(m_temp_image_file);
22062210
else
22072211
t_photo_uri = data.getData();
2212+
22082213
InputStream t_in = ((LiveCodeActivity)getContext()).getContentResolver().openInputStream(t_photo_uri);
22092214
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+
22172216
// HH-2017-01-19: [[ Bug 11313 ]]Support maximum width and height of the image
22182217
if(m_photo_height > 0 && m_photo_width > 0)
22192218
{
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);
22262286
}
22272287
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());
22292297

22302298
t_in.close();
22312299
t_out.close();

engine/src/mblandroidcamera.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ bool MCAndroidPickPhoto(const char *p_source, int32_t p_max_width, int32_t p_max
8686
if (!MCAndroidCheckRuntimePermission(MCSTR("android.permission.CAMERA")))
8787
return false;
8888

89-
MCAndroidEngineCall("showPhotoPicker", "vsii", nil, p_source, p_max_width, p_max_height);
89+
MCAndroidEngineCall("showPhotoPicker", "vsiii", nil, p_source, p_max_width, p_max_height, MCjpegquality);
9090
// SN-2014-09-03: [[ Bug 13329 ]] MCAndroidPickPhoto's return value is ignored in 6.x,
9191
// but not in 7.0 - whence the failure in mobilePickPhoto
9292
return true;

0 commit comments

Comments
 (0)