Skip to content

Commit 1edce58

Browse files
committed
Exposed screenshot public API and added iOS implementation
1 parent 012d1bc commit 1edce58

File tree

6 files changed

+79
-1
lines changed

6 files changed

+79
-1
lines changed

CodenameOne/src/com/codename1/impl/CodenameOneImplementation.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5699,6 +5699,7 @@ public void capturePhoto(ActionListener response) {
56995699
*
57005700
* @return An image of the screen, or null if it failed.
57015701
* @since 7.0
5702+
* @deprecated replaced by screenshot()
57025703
*/
57035704
public Image captureScreen() {
57045705
Form form = getCurrentForm();

CodenameOne/src/com/codename1/testing/TestUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,8 @@ public static boolean screenshotTest(String screenshotName) {
584584
return true;
585585
}
586586

587-
Image mute = Image.createImage(Display.getInstance().getDisplayWidth(), Display.getInstance().getDisplayHeight());
587+
588+
Image mute = Display.getInstance().captureScreen();
588589
Display.getInstance().getCurrent().paintComponent(mute.getGraphics(), true);
589590
screenshotName = screenshotName + ".png";
590591
if (Storage.getInstance().exists(screenshotName)) {

CodenameOne/src/com/codename1/ui/Display.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import com.codename1.ui.util.ImageIO;
6363
import com.codename1.util.AsyncResource;
6464
import com.codename1.util.RunnableWithResultSync;
65+
import com.codename1.util.SuccessCallback;
6566

6667
import java.io.IOException;
6768
import java.io.InputStream;
@@ -4995,12 +4996,25 @@ public void onCanInstallOnHomescreen(Runnable r) {
49954996
*
49964997
* @return An image of the screen, or null if it failed.
49974998
* @since 7.0
4999+
* @deprecated use screenshot(SuccessCallback) instead
49985000
*/
49995001
public Image captureScreen() {
50005002
return impl.captureScreen();
50015003
}
50025004

50035005
/**
5006+
* Captures a screenshot in the native layer which should include peer
5007+
* components as well.
5008+
*
5009+
* @param callback will be invoked on the EDT with a screenshot
5010+
* @since 7.0.211
5011+
*/
5012+
public void screenshot(SuccessCallback<Image> callback) {
5013+
impl.screenshot(callback);
5014+
}
5015+
5016+
5017+
/**
50045018
* Convenience method to schedule a task to run on the EDT after {@literal timeout}ms.
50055019
*
50065020
* @param timeout The timeout in milliseconds.

Ports/iOSPort/nativeSources/IOSNative.m

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5193,6 +5193,47 @@ void com_codename1_impl_ios_IOSNative_updatePersonWithRecordID___int_com_codenam
51935193
#endif
51945194
}
51955195

5196+
static UIImage* cn1_captureView(UIView *view) {
5197+
CGSize size = view.bounds.size;
5198+
if (size.width <= 0 || size.height <= 0) return nil;
5199+
5200+
// Prefer drawViewHierarchyInRect (renders with effects), fallback to CALayer
5201+
UIGraphicsBeginImageContextWithOptions(size, view.opaque, 0.0); // scale=0 → device scale
5202+
BOOL ok = NO;
5203+
if ([view respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
5204+
ok = [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
5205+
}
5206+
if (!ok) {
5207+
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
5208+
}
5209+
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
5210+
UIGraphicsEndImageContext();
5211+
return image;
5212+
}
5213+
5214+
void com_codename1_impl_ios_IOSNative_screenshot__(CN1_THREAD_STATE_MULTI_ARG JAVA_OBJECT instanceObject) {
5215+
/*dispatch_async(dispatch_get_main_queue(), ^{
5216+
UIView *view = [CodenameOne_GLViewController instance].view;
5217+
UIImage *img = cn1_captureView(view);
5218+
if (!img) {
5219+
return;
5220+
}
5221+
5222+
NSData *png = UIImagePNGRepresentation(img);
5223+
if (!png) {
5224+
return;
5225+
}
5226+
5227+
// Create Java byte[]
5228+
int len = (int)[png length];
5229+
JAVA_OBJECT byteArr = __NEW_ARRAY_JAVA_BYTE(CN1_THREAD_GET_STATE_PASS_ARG len);
5230+
5231+
memcpy((JAVA_ARRAY_BYTE*)((JAVA_ARRAY)byteArr)->data, (const jbyte*)[png bytes], len);
5232+
5233+
com_codename1_impl_ios_IOSImplementation_onScreenshot___byte_1ARRAY(CN1_THREAD_GET_STATE_PASS_ARG byteArr);
5234+
});*/
5235+
}
5236+
51965237

51975238
JAVA_LONG com_codename1_impl_ios_IOSNative_getPersonWithRecordID___int(CN1_THREAD_STATE_MULTI_ARG JAVA_OBJECT instanceObject, JAVA_INT recId) {
51985239
#ifdef INCLUDE_CONTACTS_USAGE

Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.codename1.location.Location;
3434
import com.codename1.ui.Component;
3535
import com.codename1.ui.Display;
36+
import com.codename1.ui.EncodedImage;
3637
import com.codename1.ui.Font;
3738
import com.codename1.ui.Image;
3839
import com.codename1.ui.PeerComponent;
@@ -300,6 +301,25 @@ public void addCookie(Cookie c) {
300301
}
301302
}
302303

304+
private static SuccessCallback<Image> screenshotCallback;
305+
306+
@Override
307+
public void screenshot(SuccessCallback<Image> callback) {
308+
screenshotCallback = callback;
309+
nativeInstance.screenshot();
310+
}
311+
312+
static void onScreenshot(final byte[] imageData) {
313+
if(screenshotCallback != null) {
314+
Display.getInstance().callSerially(new Runnable() {
315+
@Override
316+
public void run() {
317+
screenshotCallback.onSucess(EncodedImage.createImage(imageData));
318+
}
319+
});
320+
}
321+
}
322+
303323
/**
304324
* Used to enable/disable native cookies from native code.
305325
* @param cookiesArray

Ports/iOSPort/src/com/codename1/impl/ios/IOSNative.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,7 @@ native void nativeSetTransformMutable(
691691
// We go through java in order to use locking concurrency
692692
native void appendData(long peer, long data);
693693

694+
native void screenshot();
694695

695696
native void fillPolygonGlobal(int color, int alpha, int[] xPoints, int[] yPoints, int nPoints);
696697

0 commit comments

Comments
 (0)