1

我试图实现一些图像过滤器,我使用这个代码

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        imageView=(ImageView) findViewById(R.id.imgView);
        bgr = BitmapFactory.decodeResource(getResources(), R.drawable.test02);


        int A, R, G, B;
        int pixel;

        // scan through all pixels
        for(int x = 0; x < bgr.getWidth(); ++x) 
        {
            for(int y = 0; y < bgr.getHeight(); ++y) 
            {
                // get pixel color
                pixel = bgr.getPixel(x, y);
                A = Color.alpha(pixel);
                R = Color.red(pixel);
                G = Color.green(pixel);
                B = Color.blue(pixel);

                // increase/decrease each channel
                R += value;
                if(R > 255) { R = 255; }
                else if(R < 0) { R = 0; }

                G += value;
                if(G > 255) { G = 255; }
                else if(G < 0) { G = 0; }

                B += value;
                if(B > 255) { B = 255; }
                else if(B < 0) { B = 0; }

                // apply new pixel color to output bitmap
                bgr.setPixel(x, y, Color.argb(A, R, G, B));
            }
            System.out.println("x");
        }

        imageView.setImageBitmap(bgr);

    }

问题是这是减慢是任何其他方式来做到这一点,或者让它更快..

4

2 回答 2

3

您应该阅读有关使用ColorMatrix. 该矩阵可以完全执行您手动执行的操作。在您的情况下,由于您只是向每个组件添加一个常量值,因此您的矩阵将具有a = g = m = s = 1e = j = o = value,并且矩阵的其余部分将为零。

然后您可以使用setColorFilter()将 aColorMatrixColorFilter应用于您的 ImageView。

于 2012-10-05T06:44:17.437 回答
0

也许使用按位运算证明更快?

for(int x = 0; x < bgr.getWidth(); ++x) 
    {
        for(int y = 0; y < bgr.getHeight(); ++y) 
        {
            // get pixel color
            pixel = bgr.getPixel(x, y);
    int alpha = (pixel & 0xff000000) >> 24;
    int R = (pixel & 0x00ff0000) >> 16;
        int G = (pixel & 0x0000ff00) >> 8;
        int B = (pixel & 0x000000ff);

            // increase/decrease each channel
            R += value;
            if(R > 255) { R = 255; }
            else if(R < 0) { R = 0; }

            G += value;
            if(G > 255) { G = 255; }
            else if(G < 0) { G = 0; }

            B += value;
            if(B > 255) { B = 255; }
            else if(B < 0) { B = 0; }

            // apply new pixel color to output bitmap
            bgr.setPixel(x, y, (alpha << 24) + (red << 16) + (green << 8) + blue);
        }

If that proves too slow aswell,using a ColorMatrix would be the best way to do it,even if i presume it does the exact same filtering,it probably is more efficient.

于 2012-10-05T07:34:25.603 回答