2014-09-12 89 views
1

我目前正在使用下面的方法來創建一個模糊的截圖(它是在另一個線程上發現的解決方案的稍微改變的版本),它的工作原理,但它的生成相當慢,如果使用中游戲,它不是非常有用。任何人都可以提出一個更好的解希望這對其他人也有用。Android上更快的模糊屏幕截圖?

public static Bitmap blurredScreenshot(){ 
    CGSize winSize = CCDirector.sharedDirector().displaySize(); 
    int w = (int) winSize.width; 
    int h = (int) winSize.height; 
    int b[] = new int[w * h]; 
    int bt[] = new int[w * h]; 
    IntBuffer ib = IntBuffer.wrap(b); 
    ib.position(0); 

    GL10 gl = CCDirector.gl; 
    gl.glReadPixels(0, 0, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib); 

    for (int i = 0, k = 0; i < h; i++, k++) { 
     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 bitmap = Bitmap.createBitmap(bt, w, h, Bitmap.Config.ARGB_8888); 
    Bitmap scaledSmall = Bitmap.createScaledBitmap(bitmap, (int)w/5, (int)h/5, true); 
    Bitmap scaledBlur = Bitmap.createScaledBitmap(scaledSmall, w, h, true); 
    return scaledBlur; 
} 
+0

Renderscript ?? – Blackbelt 2014-09-12 15:25:26

回答

0

看看的的renderScript庫:http://developer.android.com/guide/topics/renderscript/compute.html

Bitmap screen = [screenshot bitmap]; 
RenderScript rs = RenderScript.create(context); 
Allocation input = Allocation.createFromBitmap(rs, screen, 
    Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT); 
Allocation output = Allocation.createTyped(rs, input.getType()); 
ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); 
script.setRadius(25); 
script.setInput(input); 

script.forEach(output); 
output.copyTo(screen); 

return screen; 

獲得更多模糊的技巧是將原始截圖縮放一半。您也可以進行多次傳球以增加模糊效果。在我的應用程序中,縮放到一半後我會做兩次。

+0

輝煌,看起來很有希望,並提供一些有用的建議!謝謝。 – Mateus 2014-09-12 18:36:23