Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 中級開發 >> Android OpenGL下截圖代碼

Android OpenGL下截圖代碼

編輯:中級開發

Android平台如何在OpenGL下截圖呢? 如果是一個FPS類的游戲可能常規的方式截圖,由於android系統底層讀取framebuffer的效率不是很高,嘗嘗截圖出來的游戲可能由於刷新問題,產生上半部分和下半部分不匹配的問題,在GL中我們可以使用下面這個代碼來解決。

     public static Bitmap SavePixels(int x, int y, int w, int h, GL10 gl)
    { 
         int b[]=new int[w*h];
         int bt[]=new int[w*h];
         IntBuffer ib=IntBuffer.wrap(b);
         ib.position(0);
         gl.glReadPixels(x, y, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);
         for(int i=0; i<h; i++)
         { 
              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-i-1)*w+j]=pix1;
              }
         }                 
         Bitmap sb=Bitmap.createBitmap(bt, w, h, true);
         return sb;
    }

  上面是一個簡單的GL讀取RGBA分量的方法,然後生成android通用的Bitmap對象,

    public static void SavePNG(int x, int y, int w, int h, String fileName, GL10 gl)
    {
                Bitmap bmp=SavePixels(x,y,w,h,gl);
                try
                {
                        FileOutputStream fos=new FileOutputStream("/sdcard/android123/"+fileName); //android123提示大家,如何2.2或更高的系統sdcard路徑為/mnt/sdcard/
                        bmp.compress(CompressFormat.PNG, 100, fos); //保存為png格式,質量100%
                        try
                        {
                                fos.flush();
                        }
                        catch (IOException e)
                        {
                                   e.printStackTrace();
                        }
                        try
                        {
                                fos.close();
                        }
                        catch (IOException e)
                        {
                                 e.printStackTrace();
                        }
                       
                }
                catch (FileNotFoundException e)
                {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }               
}

如何調用呢,直接執行這個靜態的方法SavePNG即可,比如SavePNG(0, 0, Config.SCREEN_W, Config.SCREEN_H, "cwj.png", gl); 最後一個參數為你的OpenGL對象。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved