Skip to content

Commit ee83234

Browse files
committed
Merge pull request livecode#3511 from livecodeian/bugfix-13636
[[ Bug 13636 ]] Implement screen snapshot on Android
2 parents d5dd6fa + c9681cd commit ee83234

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

docs/notes/bugfix-13636.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# export snapshot not working on Android

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,21 @@ public void onListPickerDone(int p_index, boolean p_done)
10521052
doProcess(false);
10531053
}
10541054

1055+
////////////////////////////////////////////////////////////////////////////////
1056+
1057+
// Return a bitmap snapshot of the specified region of the root view
1058+
public Object getSnapshotBitmapAtSize(int x, int y, int width, int height, int sizeWidth, int sizeHeight)
1059+
{
1060+
Bitmap t_bitmap = Bitmap.createBitmap(sizeWidth, sizeHeight, Bitmap.Config.ARGB_8888);
1061+
Canvas t_canvas = new Canvas(t_bitmap);
1062+
t_canvas.scale((float)sizeWidth / (float)width, (float)sizeHeight / (float)height);
1063+
t_canvas.translate((float)-x, (float)-y);
1064+
1065+
getActivity().getWindow().getDecorView().getRootView().draw(t_canvas);
1066+
1067+
return t_bitmap;
1068+
}
1069+
10551070
////////////////////////////////////////////////////////////////////////////////
10561071

10571072
public String getSystemVersion()

engine/src/mblandroiddc.cpp

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,54 @@ void MCScreenDC::setbeep(uint4 property, int4 beep)
453453

454454
MCImageBitmap *MCScreenDC::snapshot(MCRectangle &r, uint4 window, MCStringRef displayname, MCPoint *size)
455455
{
456-
return NULL;
456+
// scale rectangle from logical -> device coords
457+
MCRectangle t_rect;
458+
t_rect = logicaltoscreenrect(r);
459+
460+
// don't scale size - we want the bitmap to be sized to logical coords.
461+
int16_t t_size_width, t_size_height;
462+
if (size != nil)
463+
{
464+
t_size_width = size->x;
465+
t_size_height = size->y;
466+
}
467+
else
468+
{
469+
t_size_width = r.width;
470+
t_size_height = r.height;
471+
}
472+
473+
jobject t_bitmap;
474+
// get snapshot image as java Bitmap object
475+
MCAndroidEngineRemoteCall("getSnapshotBitmapAtSize", "oiiiiii", &t_bitmap, t_rect.x, t_rect.y, t_rect.width, t_rect.height, t_size_width, t_size_height);
476+
if (t_bitmap == nil)
477+
return nil;
478+
479+
// read Bitmap info & data into MCImageBitmap struct
480+
JNIEnv *env;
481+
env = MCJavaGetThreadEnv();
482+
AndroidBitmapInfo t_info;
483+
AndroidBitmap_getInfo(env, t_bitmap, &t_info);
484+
485+
MCImageBitmap t_imagebitmap;
486+
t_imagebitmap.width = t_info.width;
487+
t_imagebitmap.height = t_info.height;
488+
t_imagebitmap.stride = t_info.stride;
489+
490+
if (AndroidBitmap_lockPixels(env, t_bitmap, (void**)&t_imagebitmap.data) < 0)
491+
return nil;
492+
493+
MCImageBitmapCheckTransparency(&t_imagebitmap);
494+
495+
MCImageBitmap *t_copy;
496+
t_copy = nil;
497+
498+
// return a copy of the image bitmap
499+
MCImageCopyBitmap(&t_imagebitmap, t_copy);
500+
501+
AndroidBitmap_unlockPixels(env, t_bitmap);
502+
503+
return t_copy;
457504
}
458505

459506
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)