2011-09-26 68 views
4

如何在Cocos2d中获取Glsurfaceview的屏幕截图。我试着用下面的代码使用GLsurfaceView[Cocos2d]如何从GlSurfaceView创建位图

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

但它返回透明图像。

+0

我得到了[it](http://www.anddev.org/how_to_get_opengl_screenshot__useful_programing_hint-t829.html) – DroidBot

+2

您可以回答自己的问题并接受它。 –

回答

4

我得到的答案从这个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(); 
    }    
} 
+1

在哪里放这个代码? GLSurfaceView类中的 ? – Noman

+1

@ kariyachan ..它不在这里工作。我的屏幕截图是黑色的 – Noman

+0

@kariyachan ..这段代码错了。它在CreateBitmap函数中给出错误。 – Noman

2

这里是解决方案:

if (MainImageProcessingActivity.capture) { 
     int width = MainImageProcessingActivity.w; 
     int height = MainImageProcessingActivity.h; 
     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); 
     MainImageProcessingActivity.captureBmp = bitmap.copy(Bitmap.Config.ARGB_8888,false); 
     MainImageProcessingActivity.capture=false; 
    } 

把代码onDrawFrame下(GL10 GL)方法,它的工作!

+0

嗨,彼得,感谢您的代码。我在我的渲染器类和内部onDraw方法中使用此代码..它工作正常..... – harikrishnan

+0

@Peter如果图像是高度宽度的横向类型,并且您正在将其加载到GLSurfaceView中,该怎么办?我想这是采取整个设备屏幕上面的代码创建新的位图。 –

+0

尝试解决此问题:http://stackoverflow.com/questions/32601187/how-to-save-bitmap-from-glsurfaceview-only-bitmap-not-whole-texture –