翻译 - 如何解决Cocos2d Android截图黑色问题?
阿神
阿神 2017-04-17 11:27:57
[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;

}

除此之外,我还希望大家不吝赐教,指点我一条正确的方向。比如我想获取屏幕的像素,我需要应用到哪些类和实体呢?

原问题:COCOS 2D screen shot is black in Android

阿神
阿神

闭关修行中......

全部回复(1)
迷茫

答案
jeet:只需要修改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;
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号