Android:视频的色度键性能

时间:2014-09-04 08:45:50

标签: java android video effect chromakey

我有一个video stream并且喜欢对它应用色度键效果。我尝试了这个GPU library,但它比我自己的代码更慢:

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

如何改善此代码的性能?

我已经将所有对象创建移出以重用内存,但在高端平板电脑上它仍然很慢。

1 个答案:

答案 0 :(得分:0)

建议您将此操作移至本机库(C / C ++)。您可以将整个Bitmap对象传递给本机库函数,并修改Bitmap的内容,而无需来回复制像素。甚至可以应用汇编程序中的优化。

或者更简单的尝试优化您的代码。