COCOS 2D屏幕截图在Android中是黑色的

前端之家收集整理的这篇文章主要介绍了COCOS 2D屏幕截图在Android中是黑色的前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用以下代码.图像被保存,但它是黑色的.
请看我的代码,告诉我我做错了什么.
我在菜单中使用这个代码.
  1. case R.id.id_menu_Save:
  2.  
  3. Bitmap bmp = SavePixels(0,800,400,CCDirector.sharedDirector().gl);
  4.  
  5. File file = new File("/sdcard/test.jpg");
  6. try
  7. {
  8. file.createNewFile();
  9. FileOutputStream fos = new FileOutputStream(file);
  10. bmp.compress(CompressFormat.JPEG,100,fos);
  11.  
  12. Toast.makeText(getApplicationContext(),"Image Saved",0).show();
  13. Log.i("Menu Save Button","Image saved as JPEG");
  14. }
  15.  
  16. catch (Exception e)
  17. {
  18. e.printStackTrace();
  19. }
  20.  
  21. break;

这是我的保存图像功能.

  1. public static Bitmap SavePixels(int x,int y,int w,int h,GL10 gl)
  2. {
  3. int b[]=new int[w*(y+h)];
  4. int bt[]=new int[w*h];
  5. IntBuffer ib=IntBuffer.wrap(b);
  6. ib.position(0);
  7. gl.glReadPixels(x,w,y+h,GL10.GL_RGBA,GL10.GL_UNSIGNED_BYTE,ib);
  8.  
  9. for(int i=0,k=0; i<h; i++,k++)
  10. {//remember,that OpenGL bitmap is incompatible with Android bitmap
  11. //and so,some correction need.
  12. for(int j=0; j<w; j++)
  13. {
  14. int pix=b[i*w+j];
  15. int pb=(pix>>16)&0xff;
  16. int pr=(pix<<16)&0x00ff0000;
  17. int pix1=(pix&0xff00ff00) | pr | pb;
  18. bt[(h-k-1)*w+j]=pix1;
  19. }
  20. }
  21.  
  22. Bitmap sb = Bitmap.createBitmap(bt,h,Bitmap.Config.ARGB_8888);
  23. return sb;
  24. }

除此之外,我想要的是指出我的方向正确.喜欢如果我得到屏幕的像素,我应该探索什么类/实体?

只需将SavePixelx方法更改为:
  1. public static Bitmap SavePixels(int x,k++)
  2. {
  3. //remember,that OpenGL bitmap is incompatible with Android bitmap
  4. //and so,some correction need.
  5. for(int j=0; j<w; j++)
  6. {
  7. int pix=b[i*w+j];
  8. int pb=(pix>>16)&0xff;
  9. int pr=(pix<<16)&0xffff0000;
  10. int pix1=(pix&0xff00ff00) | pr | pb;
  11. bt[(h-k-1)*w+j]=pix1;
  12. }
  13. }
  14.  
  15. Bitmap sb = Bitmap.createBitmap(bt,Bitmap.Config.ARGB_8888);
  16. return sb;
  17. }

猜你在找的Cocos2d-x相关文章