[Cocos2D中]如何从GlSurfaceView创建位图 [英] [Cocos2d]How to Create bitmap from GlSurfaceView

查看:124
本文介绍了[Cocos2D中]如何从GlSurfaceView创建位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何采取屏幕快照Glsurfaceview的在cocos2d。我尝试使用下面的code GLsurfaceView

How can I take screen shot of Glsurfaceview in Cocos2d. I tried with following code using GLsurfaceView

    GlsurfaceView glv=CCDirector.sharedDirector().getOpenGLView();
    glv.setDrawingCacheEnabled(true);
    Bitmap bitmap=glv.getDrawingCache();

但它返回透明的形象。

but it return transparent image.

推荐答案

我是从这个 anddev论坛的问题我连着code随着这个​​希望有人会发现这是很有帮助的。

I got answer from this anddev forum question I attached code along with this hope somebody will find this helpful

请把里面的渲染器类的OnDraw方法将此code内启动。

Please Put this code inside renderer class onDraw Method inside starting.

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

    /*  remember, that OpenGL bitmap is incompatible with 
        Android bitmap and so, some correction need.
     */   
    for(int i=0; i<h; i++)
    {         
        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-i-1)*w+j]=pix1;
        }
    }              
    Bitmap sb=Bitmap.createBitmap(bt, w, h, true);
    return sb;
}

public static 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/my_app/"+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();
    }              
}

这篇关于[Cocos2D中]如何从GlSurfaceView创建位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆