图片处理--浮雕特效

1、算法

第一步:从右下到左上遍历图片像素

第二步:获取当前像素和 左上点像素,分别取它们RGB的差值

第三步:获取三个差值中绝对值最大的一个作为灰度值

第四步:将该灰度值加128 ,并且作像素安全处理(即gray = gray>255?255:gray , gray = gray<0 ? 0 : gray)

第五步:使用该灰度值创建图片

2、实现代码

public Bitmap render(Bitmap bitmap)
    {
        if (bitmap == null)
            return null;
//        bitmap = toGray(bitmap);
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        int[] pixels = new int[width * height];
        bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
        for (int i = height - 1; i > 0; i--)
        {
            for (int j = width - 1; j > 0; j--)
            {
                int pixel = pixels[i * width + j];
                int leftUpPixel = pixels[(i - 1) * width + j - 1];
                int r = (pixel & 0x00ff0000) >> 16;
                int g = (pixel & 0x0000ff00) >> 8;
                int b = (pixel & 0x000000ff);
                
                int leftUpR = (leftUpPixel & 0x00ff0000) >> 16;
                int leftUpG = (leftUpPixel & 0x0000ff00) >> 8;
                int leftUpB = (leftUpPixel & 0x000000ff);
                
                r = r - leftUpR;
                g = g - leftUpG;
                b = b - leftUpB;
                
                int maxDiff = r;
                if(Math.abs(maxDiff) < Math.abs(g));
                {
                    maxDiff = g;
                }
                
                if(Math.abs(maxDiff) < Math.abs(b))
                {
                    maxDiff = b;
                }
                
                int gray = maxDiff +128;        
                if(gray>255)gray = 255;
                if(gray <0)gray = 0;

                pixels[i * width + j] = (pixel & 0xff000000) + (gray << 16) + (gray << 8) + gray;
            }
        }

        return Bitmap.createBitmap(pixels, width, height, Config.ARGB_8888);
    }
posted @ 2012-09-28 09:25  lipeil  阅读(678)  评论(0编辑  收藏  举报