2017-04-17 74 views
0

我正在使用OpenGL渲染方法捕捉screenshot.but在NEXUS 7上花費7到8秒時間來讀取像素信息。我正在使用此代碼來讀取像素信息並將其保存爲位圖。OpenGL截圖在android上佔用太多時間?

public Bitmap grabPixels(GL10 mGL) { 

     final int mWidth = mViewWidth; 
     final int mHeight = mViewHeight; 

     IntBuffer ib = IntBuffer.allocate(mWidth * mHeight); 
     IntBuffer ibt = IntBuffer.allocate(mWidth * mHeight); 
     mGL.glReadPixels(0, 0, mWidth, mHeight, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib); 

     for (int i = 0; i < mHeight; i++) { 
      for (int j = 0; j < mWidth; j++) { 
       ibt.put((mHeight - i - 1) * mWidth + j, ib.get(i * mWidth + j)); 
      } 
     } 

     Bitmap mBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888); 
     mBitmap.copyPixelsFromBuffer(ibt); 
     return mBitmap; 

} 

如何提高讀取像素信息的速度並將其轉換爲位圖?

+0

您是否測量過哪些命令需要花費時間?讀取像素或存儲位圖? – BDL

+0

@BDL存儲位圖花費太多時間 以下代碼需要太多時間: - for(int i = 0; i

+0

然後我會編輯這個問題到「爲什麼在android中存儲圖像需要這麼長時間」,並編輯出所有OpenGL-es的東西,因爲它們看起來不相關。它可以讓你得到更多的關注,因爲人們立刻發現不需要OpenGL知識。 (這只是一個建議) – BDL

回答

0

I wrote another answer about saving GIFs on gamedev, which might be useful.

你不能調用另一個線程glReadPixels()。但是很可能(如你所說)保存大部分時間的圖像的過程。因此,解決方案是在單獨的線程上進行保存。

new Thread(new Runnable() { 
    @Override 
    public void run() { 
     ... save screenshot ... 
    } 
}).start(); 

之前有人用乾草叉來到我身邊。那麼是的你可以在另一個線程上調用glReadPixels()。但是它需要一個共享上下文,當調用eglCreateContext()時可以指定該上下文。

EGLContext shared = ... 
EGLDisplay display = ... 
EGLConfig eglConfig = ... 

int[] attrib_list = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE }; 
EGLContext context = egl.eglCreateContext(display, eglConfig, shared, attrib_list); 

但是在開始之前挖掘到共享環境中(可以是本身有疼痛感),那麼請確保您的瓶頸是不保存圖像,它最有可能是。