【问题标题】:COCOS 2D screen shot is black in AndroidAndroid中COCOS2D截图是黑色的
【发布时间】:2012-01-21 19:42:41
【问题描述】:

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

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

除了上面,我想从你那里给我指出正确的方向。比如如果我必须获取屏幕的像素,我应该探索什么类/实体?

【问题讨论】:

  • 我从未尝试过这样做,但我认为应用程序需要一些权限才能读取屏幕。
  • gl.glReadPixels(x, 0, w, y+h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);这一行给出了错误......我该怎么办?

标签: android screenshot cocos2d-android


【解决方案1】:

只需将 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;
}

【讨论】:

  • gl.glReadPixels(x, 0, w, y+h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);这一行给出了错误......我该怎么办?
【解决方案2】:

只需尝试更改 GL10.GL_RGB 或在 bitmap.config 中进行更改。这可能是工作。

【讨论】:

  • gl.glReadPixels(x, 0, w, y+h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);这一行给出了错误......我该怎么办?
猜你喜欢
  • 2012-01-16
  • 1970-01-01
  • 2022-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-09
相关资源
最近更新 更多