Rajawali 教程06截图

源码地址 : https://github.com/MasDennis/Rajawali/issues/478

有人提供了一些代码从一个opengl渲染器中提取截图

http://stackoverflow.com/questions/3310990/taking-screenshot-of-android-opengl

改变场景渲染器中 onDrawFrame即

public void onDrawFrame(GL10 gl) {
    super.onDrawFrame(gl);
然后你可以粘贴以下代码:

if(screenshot){                     
        int screenshotSize = mViewportWidth * mViewportHeight;
        ByteBuffer bb = ByteBuffer.allocateDirect(screenshotSize * 4);
        bb.order(ByteOrder.nativeOrder());
        gl.glReadPixels(0, 0, mViewportWidth, mViewportHeight, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb);
        int pixelsBuffer[] = new int[screenshotSize];
        bb.asIntBuffer().get(pixelsBuffer);
        bb = null;
        Bitmap bitmap = Bitmap.createBitmap(mViewportWidth, mViewportHeight, Bitmap.Config.RGB_565);
        bitmap.setPixels(pixelsBuffer, screenshotSize-mViewportWidth, -mViewportWidth, 0, 0, mViewportWidth, mViewportHeight);
        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);
        lastScreenshot = bitmap;
        saveScreenshot(lastScreenshot);

        screenshot = false;
    }
就在您的渲染器创建下列方法:

public void takeScreenshot() {
    screenshot = true;
}

public void saveScreenshot(Bitmap screenshot){
    try {
        File file = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "openglscreenshots");
        file.mkdirs();
        String path = file.toString();
        String frametime = new DecimalFormat("##########").format(currentFrameTime);
           FileOutputStream out = new FileOutputStream(path + "/" + frametime + ".png");
           screenshot.compress(Bitmap.CompressFormat.PNG, 90, out);
    } catch (Exception e) {
           e.printStackTrace();
    }       
}
你可以从Activity中调用mRenderer.takeScreenshot()进行截图。 在此存储使用系统时间文件名,您可以使用自定义计数器等等。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值