Taking a screenshot
Monkey Targets Forums/Android/Taking a screenshot
| ||
| Tried every solution from google in every combination.. dont know any further. This one crashes: public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
} with v = BBAndroidGame.AndroidGame().GetGameView()Many others, like this one, only show black image when saved: BBAndroidGame.GameView v = BBAndroidGame.AndroidGame().GetGameView(); v.setDrawingCacheEnabled(true); // this is the important code :) // Without it the view will have a dimension of 0,0 and the bitmap will be null v.measure(v.getWidth(), v.getHeight()); v.layout(0, 0, v.getWidth(), v.getHeight()); v.buildDrawingCache(true); Bitmap b = Bitmap.createBitmap(v.getDrawingCache()); v.setDrawingCacheEnabled(false); // clear drawing cache return b; Can anybody help me? =/ |
| ||
| Run it in the emulator and press PrintScreen ;) |
| ||
Function Screenshot:Image()
Local width:Int = mojo.app.DeviceWidth()
Local height:Int = mojo.app.DeviceHeight()
Local img:Image = mojo.graphics.CreateImage(width,height)
If img
Local mem:Int[] = New Int[width*height]
If mem
mojo.graphics.ReadPixels(mem,0,0,width,height)
img.WritePixels(mem,0,0,width,height)
Return img
Endif
Endif
Return Null
End
Function SaveScreenshot:Bool(filename:String)
' Import brl.databuffer
' Import brl.filestream
'
' example: SaveScreenshot("monkey://internal/screenshot_("+String(DeviceWidth())+"x"+String(DeviceHeight())+").raw")
Local width:Int = mojo.app.DeviceWidth()
Local height:Int = mojo.app.DeviceHeight()
Local mem:Int[] = New Int[width*height]
If mem
mojo.graphics.ReadPixels(mem,0,0,width,height)
Local buffer:DataBuffer = New DataBuffer(width*height*4)
If buffer
buffer.PokeInts(0,mem,0,width*height)
Local stream:FileStream = FileStream.Open(filename,"w")
If stream
stream.WriteAll(buffer,0,buffer.Length())
buffer.Discard()
Return True
Endif
Endif
Endif
Return False
End |
| ||
| With device connected to computer, find and run in the tools folder of your android_sdk the monitor app, select your android device, then click the Screen Capture camera icon. |
| ||
| Wow this is awesome :) I didn't know about that, thanks skid! |
| ||
| Press volume down and power together to take a snapshot off the screen on device |
| ||
| @Danilo: Thank you. In this case i would have to write things like jpeg compression myself, right? |
| ||
| I got it working this way, important is that you call the function in the render loop. Android / JAVA: public static Bitmap getBitmap(int x, int y, int w, int h) {
gxtkGraphics device = bb_graphics.g_renderDevice;
int width = w;
int height = w;
int[] pixels = new int[width*height];
device.ReadPixels(pixels, x, y, width, height, 0, width);
Bitmap bitmap=Bitmap.createBitmap( width,height,Bitmap.Config.ARGB_8888 );
device.Flush();
bitmap.setPixels( pixels,0,width,0,0,width, height );
return bitmap;
}
|