2014-09-04 40 views
0

我有一個video stream,並且喜歡對其應用色度鍵效果。我想這GPU library但它更慢作爲我自己的代碼:Android:視頻的色度鍵性能

public class ChromaKey { 
    int[] pix; 
    Bitmap bm; 
    int picw, pich; 
    int index, cur_pix, red2, green2, blue2; 

    public Bitmap replaceIntervalColor(Bitmap bitmap,int red, int green, int blue) 
    { 
     if (bitmap != null) 
     { 
      picw = bitmap.getWidth(); 
      pich = bitmap.getHeight(); 
      if (pix == null) 
      { 
       pix = new int[picw * pich]; 
      } 
      bitmap.getPixels(pix, 0, picw, 0, 0, picw, pich); 

      double distance; 

      for (int y = 0; y < pich; y++) { 
       for (int x = 0; x < picw ; x++) { 
        index = y * picw + x; 
        cur_pix = pix[index]; 
        red2 = (int)((cur_pix & 0x00FF0000) >>> 16); // Color.red(cur_pix); 
        green2 = (int)((cur_pix & 0x0000FF00) >>> 8); //Color.green(cur_pix); 
        blue2 = (int)(cur_pix & 0x000000FF); //Color.blue(cur_pix); 
        // faster Math.sqrt 
        // Source: http://stackoverflow.com/a/13264441/956397 
        /* distance = Math.sqrt(
          (red2 - red) * (red2 - red) 
            + (green2 - green) * (green2 - green) 
            + (blue2 - blue) * (blue2 - blue) 
        ); */ 
        distance = Double.longBitsToDouble(((Double.doubleToRawLongBits((red2 - red) * (red2 - red) 
          + (green2 - green) * (green2 - green) 
          + (blue2 - blue) * (blue2 - blue)) >> 32) + 1072632448) << 31); 

        if (distance < 190) 
        { 
         pix[index] = Color.TRANSPARENT; 
        } 
       } 
      } 

      if (bm == null) 
      { 
       bm = Bitmap.createBitmap(picw, pich, Bitmap.Config.ARGB_4444); 
      } 
      bm.setPixels(pix, 0, picw, 0, 0, picw, pich); 
      return bm; 
     } 
     return null; 
    } 
} 

我怎樣才能改善這種代碼的性能?

我已將所有對象創建移出重用內存,但在高端平板電腦上仍然很慢。

回答

0

建議您將此操作移至本機庫(C/C++)。您可以將整個Bitmap對象傳遞給本機庫函數,並且可以修改位圖的內容而不需要來回複製像素。甚至可以應用匯編器中的優化。

或者更簡單的嘗試優化你的代碼。

+0

我使用'System.nanoTime()'做了一些簡單的性能測量,結果證明這段代碼沒問題。問題是一個糟糕的網絡連接。 – PiTheNumber 2014-09-05 14:23:28