It looks like you're new here. If you want to get involved, click one of these buttons!
public void onDrawFrame(GL10 gl) { GiderosApplication.getInstance().onDrawFrame(); JavaNativeBridge.onDrawFrame(); if(MyActivity.sRequireCapture) { int width = MyActivity.sRequireCaptureWidth; int height = MyActivity.sRequireCaptureHeight; int screenshotSize = width * height; ByteBuffer bb = ByteBuffer.allocateDirect(screenshotSize * 4); bb.order(ByteOrder.nativeOrder()); gl.glReadPixels(0, 0, width, height, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb); int pixelsBuffer[] = new int[screenshotSize]; bb.asIntBuffer().get(pixelsBuffer); bb = null; Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); bitmap.setPixels(pixelsBuffer, screenshotSize-width, -width, 0, 0, width, height); pixelsBuffer = null; short sBuffer[] = new short[screenshotSize]; ShortBuffer sb = ShortBuffer.wrap(sBuffer); bitmap.copyPixelsToBuffer(sb); //Making created bitmap (from OpenGL points) compatible with Android bitmap for (int i = 0; i < screenshotSize; ++i) { short v = sBuffer[i]; sBuffer[i] = (short) (((v&0x1f) << 11) | (v&0x7e0) | ((v&0xf800) >> 11)); } sb.rewind(); bitmap.copyPixelsFromBuffer(sb); sLastScreenShoot = bitmap; //save to sd card String extStorageDirectory = Environment.getExternalStorageDirectory().toString() + "//Android//data//" + TOSTRING(ANDROID_PACKAGE_NAME) + "//"; File dir = new File(extStorageDirectory); if (!(dir.exists() && dir.isDirectory())) { dir.mkdirs(); } System.out.println("Capture screen to " + extStorageDirectory + MyActivity.sRequireCaptureFilename + ".png"); OutputStream outStream = null; File file = new File(extStorageDirectory, MyActivity.sRequireCaptureFilename + ".png"); try { outStream = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); outStream.flush(); outStream.close(); } catch(Exception e) { System.out.println("Fail to write screenshot"); e.printStackTrace(); } MyActivity.sRequireCapture = false; } } |
Likes: Teranth
Comments
I need this feature because sometimes I have to capture bug on android phone (capture screen on android phone is not good like iOS).
@atilim: In onDrawFrame, how can I get screen-size, which return the same result as application:getDeviceWidth, application:getDeviceHeight ?
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Niclas
https://deluxepixel.com
Unfortunately I always get black empty screenshots. What am I doing wrong?
Here is a code sample I use: