Log in or register to post comments

How to make a screenshot?

April 28, 2011 - 9:05am #1

Hi,
I was trying to use these functions, but with no luck :(
public void SavePNG(int x, int y, int w, int h, String name, GL10 gl)
{
Bitmap bmp=SavePixels(x,y,w,h,gl);
try
{
FileOutputStream fos=new FileOutputStream("/sdcard/"+name);
bmp.compress(CompressFormat.PNG, 100, fos);
try
{
fos.flush();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
fos.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public Bitmap SavePixels(int x, int y, int w, int h, GL10 gl)
{
int b[]=new int[w*(y+h)];
int bt[]=new int[w*h];
IntBuffer ib=IntBuffer.wrap(b);
ib.position(0);
gl.glReadPixels(x, 0, w, y+h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);

for(int i=0, k=0; i {//remember, that OpenGL bitmap is incompatible with Android bitmap
//and so, some correction need.
for(int j=0; j {
int pix=b[i*w+j];
int pb=(pix>>16)&0xff;
int pr=(pix int pix1=(pix&0xff00ff00) | pr | pb;
bt[(h-k-1)*w+j]=pix1;
}
}

Bitmap sb=Bitmap.createBitmap(bt, w, h, Bitmap.Config.ARGB_8888);
return sb;
}
Please, if someone could help me with that.

How to make a screenshot?

April 27, 2015 - 10:36pm #20

Hi, 

were you able to get a screenshot  with cameraview in the background the overlayview in the front. when i tried this, i get black background.

If you could get a screen shot with cameraView and overlayView, please share it.

Thankyou

Pallavi

How to make a screenshot?

December 12, 2014 - 4:32am #19

Please reply how to take a snapshot in arview when video is playing from url.don't want to use any GUIButton

How to make a screenshot?

December 12, 2014 - 4:32am #18

Please reply how to take a snapshot in arview when video is playing from url.don't want to use any GUIButton

How to make a screenshot?

August 28, 2013 - 8:55am #17

vavikk wrote:

Thanks KIM!!! It worked, but each time I take a snapshot, I need to reboot the phone to see it in the Image Gallery. Thanks a lot!

Hi all,

even i am getting the same problem. i am using the following code. is there any problem in the code or it is android's problem. please help me out. 

private void saveScreenShot(int x, int y, int w, int h) {
        Bitmap bmp = grabPixels(x, y, w, h);
        try {
                      
			File directory = new File (Environment.getExternalStorageDirectory() + "/screenshot");
	        directory.mkdirs();

			String filename1 = "myscreenshot" + i + ".png";
			File yourFile = new File(directory, filename1);
			
			while (yourFile.exists())
			{
     		   	i++;   
	           	filename1 = "screenshot" + i + ".png"; 
		        yourFile = new File(directory, filename1);
		    }	
			
            yourFile.createNewFile();
             
            FileOutputStream fos = new FileOutputStream(yourFile);
            bmp.compress(CompressFormat.PNG, 100, fos);
  
            fos.flush();
             
            fos.close();
             
        } catch (Exception e) {
            DebugLog.LOGD(e.getStackTrace().toString());
        }
    }  
    private Bitmap grabPixels(int x, int y, int w, int h) {
        int b[] = new int[w * (y + h)];
        int bt[] = new int[w * h];
        IntBuffer ib = IntBuffer.wrap(b);
        ib.position(0);
         
        GLES20.glReadPixels(x, 0, w, y + h, 
                   GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, ib);
  
        for (int i = 0, k = 0; i < h; i++, k++) {
            for (int j = 0; j < w; j++) {
                int pix = b[i * w + j];
                int pb = (pix >> 16) & 0xff;
                int pr = (pix << 16) & 0x00ff0000;
                int pix1 = (pix & 0xff00ff00) | pr | pb;
                bt[(h - k - 1) * w + j] = pix1;
            }
        }  
        Bitmap sb = Bitmap.createBitmap(bt, w, h, Bitmap.Config.ARGB_8888);
        return sb;
    }

}

i have also added GLES20.glFinish(); in onDrawFrame() method after renderFrame(); and i am calling saveScreenShot() function after it.

How to make a screenshot?

May 7, 2013 - 3:00am #16

Thank you so much for the help

How to make a screenshot?

May 7, 2013 - 2:20am #15

Hi, you could just have a public method in VideoPlaybackRenderer.java, and call it from your VideoPlayback.java when you press the button;

for instance, in VideoPlaybackRenderer.java:

private boolean mTakeScreenshot = false;

public void takeScreenshot() {

  mTakeScreenshot = true;

}

 

Then, in the rendering method, you put the code to actually save the screenshot as explained my tutorial, with the only caveat of adding this:

if (mTakeScreenShot) {

.... do code to save screenshot

mTakeScreenshot = false;

}

Note: this is just about Java programming...

How to make a screenshot?

May 7, 2013 - 1:48am #14

Thanks AlessandroB, but how can i call from Videoplayback.java in which

i have the button there , which will trigger the screenshot when i click?

How to make a screenshot?

May 6, 2013 - 11:18pm #13

Hi jok_rodz,

have you tried following this tutorial ?

https://developer.vuforia.com/forum/faq/android-how-can-i-capture-ar-view

 

How to make a screenshot?

May 6, 2013 - 10:16pm #12

I used the code below but it seems that it saves as black png image..

How to make a screenshot?

February 12, 2013 - 12:43am #11

Hi, make sure you have the permission for external storage writing in the Android manifest; 

also, what do you mean by  "it shows all widgets, but without image from glsurface."

Do you mean that the opengl view is shown as a black rectangle ? or you don't see it at all ?

 

How to make a screenshot?

February 11, 2013 - 11:39pm #10

Hi,

I tried the code above to make a screenshot. I save the screen in sdcard, but the screen is not opened. "Error opening file".

And when I open this screen on my computer, it shows all widgets, but without image from glsurface.

Re: How to make a screenshot?

March 2, 2012 - 11:58am #9

Hi Kim,

I'm picking up a old thread, and I'm sure the problem would have been solved. I'm faced with the same issue. I followed your post and the code snippet you had provided.
This captures what I see in the camera view. Please advice how can I capture what is being displayed in the overlayView as well.

I intend to capture both the camera view and the text/buttons/images being displayed on the overlayView as well.

As you have mentioned in the post, i created the savePNG and SavePixel methods. I'm calling these from onDrawFrame method of the renderer. I think the problem is that I'm passing GL10 (gl) view being passed as parameter to onDrawFrame method.

Please advise.

Re: How to make a screenshot?

December 29, 2011 - 5:22am #8

Hi,

In general it's better to avoid using the "/sdcard" hardcoded path when accessing the external storage, because not every device uses this path, as Ksiva commented. There is a method to get the external storage directory that works for every device:
File rootSD = Environment.getExternalStorageDirectory();

@Kang:
On the other hand, it's possible that your problem is due to an access problem to the sd card, even if you added the permission on the manifest.xml.
You could use this snippet to check if you really can read or write on the SD card:

// Check the state of the external storage
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
    // We can read and write the media
    DebugLog.LOGD(TAG + " - Media Mounted");
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    // We can only read the media
    DebugLog.LOGD(TAG + " - Media Read Only");
    DebugLog.LOGE(TAG + " - Cannot create application directory on the Read Only external storage");
    throw new ExternalStorageWriteException();
    

} else {
    // Something else is wrong. It may be one of many other states, but all we need
    //  to know is we can neither read nor write
    DebugLog.LOGD(TAG + " - Cannot read nor write on external storage");
    throw new ExternalStorageAccessException();
}

NOTE: These Exceptions I use are just a subclass of Exception that I created to control the error behaviour related to the external storage access.

With this State value you can check which kind of access to the external memory do you have.
In my case it was useful to detect that when I had the device connected by cable, the SD card was mounted on the computer and not on the phone, so I couldn't access it.

Hope it was useful.

Infaust.

Re: How to make a screenshot?

December 28, 2011 - 5:15pm #7

hello All,
Can I make bitmap file in andoid device from system.drawing.bitmap ???

Re: How to make a screenshot?

September 25, 2011 - 8:14pm #6
ksiva wrote:

Are there any errors in the log? The only thing I can think of is perhaps your device uses /emmc instead of /sdcard. Add a print statement to the SavePNG method to make sure it is getting called.

- Kim

Thanks Kim.
I added print statements to the SavePNG and SavePixels methods and run the app. I looked into the log but there was no error, the methods were called. a part of the log below.

09-26 11:57:24.060: INFO/System.out(816): count: 87
09-26 11:57:24.090: INFO/System.out(816): count: 88
09-26 11:57:24.140: INFO/System.out(816): count: 89
09-26 11:57:24.170: INFO/System.out(816): count: 90
09-26 11:57:24.170: INFO/System.out(816): SavePNG called!!
09-26 11:57:24.170: INFO/System.out(816): SavePixels called
09-26 11:57:24.220: INFO/System.out(816): SavePNG finished
09-26 11:57:24.230: INFO/System.out(816): count: 91
09-26 11:57:24.250: INFO/System.out(816): count: 92

My test device is Nexus one. it uses /sdcard.
What's wrong with this?

Re: How to make a screenshot?

September 25, 2011 - 8:41am #5

Are there any errors in the log? The only thing I can think of is perhaps your device uses /emmc instead of /sdcard. Add a print statement to the SavePNG method to make sure it is getting called.

- Kim

Re: How to make a screenshot?

September 23, 2011 - 12:59am #4

Hi,

I tried the code above to make a screenshot.
I modified the AndroidManifest.xml file to contain the permission. I added the methods, SavePNG and SavePixels to VirtualButtonsRenderer.java and called SavePNG from onDrawFrame as follows.

/** Called to draw the current frame. */
public void onDrawFrame(GL10 gl)
{
if (!mIsActive)
return;

// Call our native function to render content
renderFrame();

if (count == 90)
SavePNG(0, 0, 100, 50, "test", gl);

count++;
}

To avoid repeated call, I added the int variable, count and only when the count is 90, SavePNG is called.

But, I didn't see any screenshot file in sdcard although I repeatedly run the app and I rebooted the phone.

Any help?

Re: How to make a screenshot?

April 28, 2011 - 10:56am #3

Thanks KIM!!!
It worked, but each time I take a snapshot, I need to reboot the phone to see it in the Image Gallery.
Thanks a lot!

Re: How to make a screenshot?

April 28, 2011 - 10:09am #2

I was able to get your methods to work with just a couple modifications. First off, make sure the AndroidManifest.xml file contains this permission:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Here's the modified code (creates the file before writing to it):

public void SavePNG(int x, int y, int w, int h, String name, GL10 gl)
{
    Bitmap bmp=SavePixels(x,y,w,h,gl);
    try
    {
        File file = new File("/sdcard", name);
        try
        {
            file.createNewFile();
        }
        catch (IOException e1)
        {
            e1.printStackTrace();
        }
        FileOutputStream fos=new FileOutputStream(file);
        bmp.compress(CompressFormat.PNG, 100, fos);
        try
        {
            fos.flush();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        try
        {
            fos.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

    }
    catch (FileNotFoundException e)
    {
        e.printStackTrace();
    } 
}


public Bitmap SavePixels(int x, int y, int w, int h, GL10 gl)
{ 
    int b[]=new int[w*(y+h)];
    int bt[]=new int[w*h];
    IntBuffer ib=IntBuffer.wrap(b);
    ib.position(0);
    gl.glReadPixels(x, 0, w, y+h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);

    for(int i=0, k=0; i<h; i++, k++)
    {//remember, that OpenGL bitmap is incompatible with Android bitmap
        //and so, some correction need. 
        for(int j=0; j<w; j++)
        {
            int pix=b[i*w+j];
            int pb=(pix>>16)&0xff;
            int pr=(pix<<16)&0x00ff0000;
            int pix1=(pix&0xff00ff00) | pr | pb;
            bt[(h-k-1)*w+j]=pix1;
        }
    }

    Bitmap sb=Bitmap.createBitmap(bt, w, h, Bitmap.Config.ARGB_8888);
    return sb;
}

Finally, make sure you call this from the GLThread. I added the methods to ImageTargetsRenderer.java and called SavePNG from onDrawFrame (don't call it every frame though!)

- Kim

Log in or register to post comments