java - [Cocos2d]如何从GlSurfaceView创建位图

标签 java android bitmap glsurfaceview cocos2d-android

如何在 Cocos2d 中截取 Glsurfaceview 的屏幕截图。我尝试使用 GLsurfaceView

使用以下代码
    GlsurfaceView glv=CCDirector.sharedDirector().getOpenGLView();
    glv.setDrawingCacheEnabled(true);
    Bitmap bitmap=glv.getDrawingCache();

但它返回透明图像。

最佳答案

我从这个 anddev forum question 得到了答案我附上了代码,希望有人会觉得这有帮助

请将此代码放在渲染器类的 onDraw 方法中。

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();
    }              
}

关于java - [Cocos2d]如何从GlSurfaceView创建位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7554553/

相关文章:

eclipse - 无法让 Android ApiDemos 在 Eclipse 中工作

android - 如何在android中释放位图内存

java - Android 位图内存不足

java - 如何替换图像的颜色?

java - 将结果分组到List中,List是Mybatis(Mapper)和Java中的属性

java - 我的小程序不显示在我的浏览器 (Google Chrome) 中

android - 试图获取 android 手机的更新位置,但代码给我带来了问题

java - 如何在 java/android 中的 GRPC 中导入 .proto 文件中的数据类型?

android - 如何给位图一个唯一的文件名

java - 如何让我的程序运行一次后仍然运行