2011-09-26 67 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 –