android - COCOS2D截图在Android中是黑色的

标签 android screenshot cocos2d-android

我正在使用以下代码。图像已保存,但它是黑色的。
请查看我的代码并告诉我哪里做错了。
我在菜单中使用此代码。

case R.id.id_menu_Save:

            Bitmap bmp = SavePixels(0, 0, 800, 400, CCDirector.sharedDirector().gl);

            File file = new File("/sdcard/test.jpg");
            try
            {
                file.createNewFile();
                FileOutputStream fos = new FileOutputStream(file);
                bmp.compress(CompressFormat.JPEG, 100, fos);

                Toast.makeText(getApplicationContext(), "Image Saved", 0).show();
                Log.i("Menu Save Button", "Image saved as JPEG");
            }

            catch (Exception e)
            {
                e.printStackTrace();
            }

            break;

这是我的保存图像功能。

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

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

    Bitmap sb = Bitmap.createBitmap(bt, w, h, Bitmap.Config.ARGB_8888); 
    return sb;
}

除上述之外,我希望您能为我指明正确的方向。比如如果我必须获取屏幕的像素,我应该探索哪个类/实体?

最佳答案

只需将 SavePixelx 方法更改为以下内容:

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

     for(int i=0, k=0; i<h; i++, k++)
     {
          //remember, that OpenGL bitmap is incompatible with Android bitmap
          //and so, some correction need.        
          for(int j=0; j<w; j++)
          {
               int pix=b[i*w+j];
               int pb=(pix>>16)&0xff;
               int pr=(pix<<16)&0xffff0000;
               int pix1=(pix&0xff00ff00) | pr | pb;
               bt[(h-k-1)*w+j]=pix1;
          }
     }

    Bitmap sb = Bitmap.createBitmap(bt, w, h, Bitmap.Config.ARGB_8888); 
    return sb;
}

关于android - COCOS2D截图在Android中是黑色的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8531481/

相关文章:

java - 如何在java中进行碰撞检测

java - 如何设置CCSprite的框架

java - Activity关闭时内存未释放

android - 无法在 android marshmallow 6.0 中接听来电

ipad - iPad 上的核心动画截图

c++ - 如何使用 xlib 正确截屏?

android截屏并分享

Android 图像不显示在 imageview 中

java - 如何从较新的 Android API 版本实现接口(interface)

android - Cocos2d-Android - Sprite.getcontentsize , Sprite.getscale , Sprite.gettexture 有什么区别