Java Android:视频的色度键性能

Java Android:视频的色度键性能,java,android,video,effect,chromakey,Java,Android,Video,Effect,Chromakey,我有一个键盘,喜欢在上面应用色度键效果。我尝试过这个,但它比我自己的代码更慢: 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) {

我有一个键盘,喜欢在上面应用色度键效果。我尝试过这个,但它比我自己的代码更慢:

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;
    }
}
公共类ChromaKey{
int[]pix;
位图bm;
int picw,pich;
整数索引,cur_pix,red2,green2,blue2;
公共位图replaceIntervalColor(位图位图、int红色、int绿色、int蓝色)
{
if(位图!=null)
{
picw=bitmap.getWidth();
pich=bitmap.getHeight();
if(pix==null)
{
pix=新整数[picw*pich];
}
获取像素(像素,0,像素,0,0,像素,像素);
双倍距离;
对于(int y=0;y>>16);//Color.red(cur_-pix);
green2=(int)((cur_-pix&0x0000FF00)>>>>8);//Color.green(cur_-pix);
blue2=(int)(cur_-pix&0x000000FF);//Color.blue(cur_-pix);
//快速数学.sqrt
//资料来源:http://stackoverflow.com/a/13264441/956397
/*距离=Math.sqrt(
(红色2-红色)*(红色2-红色)
+(绿色2-绿色)*(绿色2-绿色)
+(蓝色2-蓝色)*(蓝色2-蓝色)
); */
距离=Double.longBitsToDouble((Double.doubleToRawLongBits((red2-红色)*(red2-红色)
+(绿色2-绿色)*(绿色2-绿色)

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


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

我使用
System.nanoTime()
进行了一些简单的性能测量,结果表明此代码没有问题。问题是网络连接不好。yo man。如何删除绿色?我应该将255设置为绿色位置吗?@Peter yes(红色=0,绿色=255,蓝色=0)如果你有完美的绿色和完美的灯光。否则你可能需要调整一点。感谢buddy提供的信息