2016-10-08 51 views
0

当我写这样的代码..Android的OpenGL的保存黑色图像

@Override 
public void onDrawFrame(GL10 gl) { 
    /* codes */ 
    saveBitmap(takeScreenshot(gl)); 
} 

它的工作完美(采取截图并保存位图到SD卡)

当我想用按钮作为触发

Button btn; 
     @Override 
     public void onCreate{ 
      btn.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        saveBitmap(takeScreenshot(currGL10)); 
       } 
      }); 
     } 

     @Override 
     public void onDrawFrame(GL10 gl) { 
      /* codes */ 
      currGL10 = gl; 
     } 

其保存唯一的黑人形象。我不明白,我失去了什么,像这样使用。谢谢

+0

解决方法:捕获onDrawFrame中的位图,并在点击按钮时将其保存。 –

+0

我试过这个,它正在工作..我每次都无法捕获位图,因为我的程序多次使用onDrawFrame(),导致性能下降..(takeScreenshot嵌套for循环)..谢谢 – Huseyin

+1

机会是'saveBitmap()'产生'glReadPixels()'调用,它需要一个当前的OpenGL上下文。所以你不能从渲染线程以外的线程调用它。例如在这里看到一个解释:http://stackoverflow.com/questions/30094705/glclearcolor-not-working-correct-android-opengl。这不是一个用例,而是一个基本问题。 –

回答

0
use this code its work fine . 
    @Override 
    public void onDrawFrame(GL10 arg0) { 

     imageBitmap = takeScreenshot(arg0); 

    } 

**take a screenshot of open GlSurfaceView** 

public Bitmap takeScreenshot(GL10 mGL) { 

     final int mWidth = b_width; 
     final int mHeight = b_height; 

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

     // Convert upside down mirror-reversed image to right-side up normal 
     // image. 
     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.eraseColor(Color.argb(0, 255, 255, 255)); 
     mBitmap.copyPixelsFromBuffer(ibt); 
     return mBitmap; 
    } 
相关问题