Author Topic: How to make a screen snap  (Read 3011 times)

Offline tao

  • byte
  • *
  • Posts: 16
    • View Profile
How to make a screen snap
« on: March 17, 2014, 02:02:29 pm »
Hi,

I'm trying to create a snapshot of current frame. The way I'm thinking is like,

1) Create a FrameBuffer, and setRenderTarget() to a self-created Texture.
2) Call World.renderScene(fb) and World.draw(fb), and the current frame will render to the above Texture.
3) Then I can use the texture and display is as a snapshot or save it.

Is that OK?

Thanks.

Offline Irony

  • long
  • ***
  • Posts: 151
    • View Profile
Re: How to make a screen snap
« Reply #1 on: March 17, 2014, 02:38:03 pm »
What do you need it for? Just for screenshots? There are easier ways for that :)
Most devices have a certain button combination or a gesture to capture the screen (google it), or if you are using Eclipse, switch to DDMS view, there you have a screen capture button.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How to make a screen snap
« Reply #2 on: March 17, 2014, 03:02:39 pm »
You can't save the texture, because you can't access the data rendered into it. It's better to use http://www.jpct.net/jpct-ae/doc/com/threed/jpct/FrameBuffer.html#getPixels() instead and convert the int[]-array into an image that can then be saved.

Offline tao

  • byte
  • *
  • Posts: 16
    • View Profile
Re: How to make a screen snap
« Reply #3 on: March 18, 2014, 02:05:24 am »
Thanks Irony and Egon, I'm going to use snapshot as some 'cool' effect in game  :), not for saving actually. What I want is like rendering screen content to a texture and displaying it in a photo frame. So am I doing the right way?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How to make a screen snap
« Reply #4 on: March 18, 2014, 06:40:46 am »
In that case....Yes. Just make sure that you are using OpenGL ES 2.0 mode for this. With 1.x, rendering into a texture is limited and not very reliable.

Offline tao

  • byte
  • *
  • Posts: 16
    • View Profile
Re: How to make a screen snap
« Reply #5 on: March 19, 2014, 04:50:59 am »
Hi Egon,

Here is my snap shot code.

   public void createScreenSnap() {
      final int width = /* Screen Width */
      final int height = /* Screen Height */
      if (texScreenSnap == null) {
         texScreenSnap = new Texture(width, height);
      }
      FrameBuffer fb = new FrameBuffer(width, height); /* Exception Here !!!! */
      fb.setRenderTarget(texScreenSnap);
      fb.clear(RGBColor.BLACK);
      if (world != null){         
         world.renderScene(fb);
         world.draw(fb);
      }
      fb.dispose();
   }

I get follow exception at [new FrameBuffer(width, height)].

03-19 09:07:39.325: E/AndroidRuntime(28372): java.lang.RuntimeException: [ 1395191259321 ] - ERROR: java.lang.RuntimeException: [ 1395191259319 ] - ERROR: java.lang.RuntimeException: [ 1395191259318 ] - ERROR: java.lang.RuntimeException: [ 1395191259316 ] - ERROR: Failed to load and compile vertex shaders!

03-19 09:07:39.325: E/AndroidRuntime(28372):    at com.threed.jpct.FrameBuffer.<init>(FrameBuffer.java:94)

Am I making something wrong?

Thanks,

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How to make a screen snap
« Reply #6 on: March 19, 2014, 10:29:19 am »
To use OpenGL ES2.0, you have to setup your gl context accordingly. Have a look at the HelloShader example to see how to do this.

Offline sushobhit

  • long
  • ***
  • Posts: 109
  • future is now
    • View Profile
    • ANDROID APPS
Re: How to make a screen snap
« Reply #7 on: March 19, 2014, 01:25:16 pm »
I am also stuck kindof same issue :

I think we both could get on if this is Solved :
Bitmap bitmap;
mGLView.setDrawingCacheEnabled(true);
bitmap = mGLView.getDrawingCache(); // Here you get the screen cap of the View
// then compress it...
bitmap.compress(android.graphics.Compression.PNG,100,outputstream);

Offline sushobhit

  • long
  • ***
  • Posts: 109
  • future is now
    • View Profile
    • ANDROID APPS
Re: How to make a screen snap
« Reply #8 on: March 19, 2014, 01:26:54 pm »
?? this is not working just getting a blank output
if this could be somehow rectified I think the issue could be resolved..

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How to make a screen snap
« Reply #9 on: March 19, 2014, 03:29:00 pm »
No, that's different. The OT asks for a way to get the rendered image and reuse it in the app, which is a basic render-to-texture effect. You want to externalize the rendered image. I've already posted an answer to your actual thread about this topic.