首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android: Chroma视频关键性能

Android: Chroma视频关键性能
EN

Stack Overflow用户
提问于 2014-09-04 08:45:49
回答 1查看 1.2K关注 0票数 2

我有一个视频流,并喜欢在它上应用色度键效应。我尝试了这个GPU库,但是它比我自己的代码还要慢:

代码语言:javascript
复制
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;
    }
}

如何提高此代码的性能?

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-04 08:58:29

建议将此操作移到本机库(C/C++)。您可以将整个Bitmap对象传递给本机库函数,并在不复制像素的情况下修改Bitmap的内容。甚至可以在汇编程序中进行优化。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25661036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档