2011-12-16 16 views
12

Estoy usando el siguiente código. La imagen se guarda pero es NEGRA.
Por favor, vea mi código y dígame dónde estoy haciendo mal.
Estoy usando este código en el Menú.La captura de pantalla COCOS 2D es negra en 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; 

Esta es mi función Guardar imagen.

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

Aparte de lo anterior, lo que quiero de usted es apuntarme en la dirección correcta. Como si tuviera que obtener los píxeles de la pantalla, ¿qué clase/entidad debería explorar?

+0

no he tratado alguna vez de hacer esto, pero yo supongo aplicaciones necesitan algún permiso para ser capaz de leer la pantalla. –

+0

gl.glReadPixels (x, 0, w, y + h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib); Esta línea está dando el error ... ¿Qué debería hacer? –

Respuesta

2

Sólo cambia el método SavePixelx para el siguiente:

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

gl.glReadPixels (x, 0, w, y + h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib); Esta línea está dando el error ... ¿Qué debería hacer? –

+0

¿Cuál es el gl aquí? –

2

sólo tratar de cambiar GL10.GL_RGB o hacer cambios en bitmap.config. Puede ser un trabajo.

+0

gl.glReadPixels (x, 0, w, y + h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib); Esta línea está dando el error ... ¿Qué debería hacer? –

Cuestiones relacionadas