2016-06-23 56 views
0

我修改了this project,它使用GLSurfaceView和Effects來顯示ViewPager,其中一些效果應用於圖像。Android GLSurfaceView保存位圖導致異常

此外,我創建了覆蓋位圖,該效果已應用後放置在每個圖像上。

此時,該應用程序工作正常。但是現在我必須將顯示的圖像保存在文件中,當按下按鈕時。

所以就用這樣的代碼:

private Bitmap createBitmapFromGLSurface(int x, int y, int w, int h, GL10 gl) 
      throws OutOfMemoryError { 
     int bitmapBuffer[] = new int[w * h]; 
     int bitmapSource[] = new int[w * h]; 
     IntBuffer intBuffer = IntBuffer.wrap(bitmapBuffer); 
     intBuffer.position(0); 

     try { 
      gl.glReadPixels(x, y, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, intBuffer); 
      int offset1, offset2; 
      for (int i = 0; i < h; i++) { 
       offset1 = i * w; 
       offset2 = (h - i - 1) * w; 
       for (int j = 0; j < w; j++) { 
        int texturePixel = bitmapBuffer[offset1 + j]; 
        int blue = (texturePixel >> 16) & 0xff; 
        int red = (texturePixel << 16) & 0x00ff0000; 
        int pixel = (texturePixel & 0xff00ff00) | red | blue; 
        bitmapSource[offset2 + j] = pixel; 
       } 
      } 
     } catch (GLException e) { 
      e.printStackTrace(); 
      return null; 
     } 

     return Bitmap.createBitmap(bitmapSource, w, h, Bitmap.Config.ARGB_8888); 
    } 

以獲得位圖。當按下按鈕時,我調用這個方法:

protected void onClick() { 
     read = true; 
     mEffectView.requestRender(); 
} 

迫使渲染,所以我生成位圖,並將其保存在使用的AsyncTask的文件。 讀取被用作onDrawFrame(GL10 gl)中的半焦以在我想要保存時只生成位圖。

保存一張圖片效果很好。當我保存的第二個,然後我改變頁面,這個錯誤出現:

A/Bitmap: Failed to acquire strong reference to pixels 
A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 20475 (GLThread 9540) 

的另一個問題是,疊加,雖然顯示出來,不會被保存在圖像中。 這是我如何應用它:

EffectFactory effectFactory = mEffectContext.getFactory(); 
overlayEffect = effectFactory.createEffect(EffectFactory.EFFECT_BITMAPOVERLAY); 
overlayEffect.setParameter("bitmap", overlay); 

影響應用

mEffect.apply(mTextures[0], mImageWidth, mImageHeight, mTextures[1]); 
overlayEffect.apply(mTextures[1], mImageWidth, mImageHeight, mTextures[2]); 

隨着mEffect保存圖像時是唯一的效果看得見。

我在做什麼錯了?

編輯 我解決了最後一個問題:我發現,你必須發佈重新你每次使用的每一種效果對象被稱爲mEffectView.requestRender()

回答

2

顯然,使用

overlayEffect = effectFactory.createEffect(EffectFactory.EFFECT_BITMAPOVERLAY); 
overlayEffect.setParameter("bitmap", overlay); 

傳遞的位圖回收時!

所以我解決了問題,通過它的一個副本:

overlayEffect = effectFactory.createEffect(EffectFactory.EFFECT_BITMAPOVERLAY); 
overlayEffect.setParameter("bitmap", overlay.copy(overlay.getConfig(), false)); 

希望這將幫助別人!