android图片效果处理

来源:互联网 发布:linux 删除全部内容 编辑:程序博客网 时间:2024/05/08 23:43

一:无图无真相,先上底片效果图:

老照片效果



浮雕效果

高斯模糊效果


按钮的动画效果,感兴趣的可以看看我的文章:
菜单按钮

接下来看看代码:

其实所有的效果无非都是根据公式去改变每个像素点RBG值
胶卷底片 的公式为:   newR=(int)(255-pixR)
newG=(int)(255-pixG)
newB=(int)(255-pixB)

private Bitmap dipian(Bitmap bmp)    {        long start = System.currentTimeMillis();        int width = bmp.getWidth();        int height = bmp.getHeight();        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);        int pixColor = 0;        int pixR = 0;        int pixG = 0;        int pixB = 0;        int newR = 0;        int newG = 0;        int newB = 0;        int[] pixels = new int[width * height];        bmp.getPixels(pixels, 0, width, 0, 0, width, height);        for (int i = 0; i < height; i++)        {            for (int k = 0; k < width; k++)            {                pixColor = pixels[width * i + k];                pixR = Color.red(pixColor);                pixG = Color.green(pixColor);                pixB = Color.blue(pixColor);                newR = (int) (255-pixR);                newG = (int) (255-pixG);                newB = (int) (255-pixB);                int newColor = Color.argb(255, newR > 255 ? 255 : newR, newG > 255 ? 255 : newG, newB > 255 ? 255 : newB);                pixels[width * i + k] = newColor;            }        }        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);        long end = System.currentTimeMillis();        Log.e("bm", "used time="+(end - start));        return bitmap;    }

老照片公式为:
newR = (int) (0.393 * pixR + 0.769 * pixG + 0.189 * pixB);                newG = (int) (0.349 * pixR + 0.686 * pixG + 0.168 * pixB);                newB = (int) (0.272 * pixR + 0.534 * pixG + 0.131 * pixB);

 private Bitmap oldRemeber(Bitmap bmp)    {        // 速度测试        long start = System.currentTimeMillis();        int width = bmp.getWidth();        int height = bmp.getHeight();        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);        int pixColor = 0;        int pixR = 0;        int pixG = 0;        int pixB = 0;        int newR = 0;        int newG = 0;        int newB = 0;        int[] pixels = new int[width * height];        bmp.getPixels(pixels, 0, width, 0, 0, width, height);        for (int i = 0; i < height; i++)        {            for (int k = 0; k < width; k++)            {                pixColor = pixels[width * i + k];                pixR = Color.red(pixColor);                pixG = Color.green(pixColor);                pixB = Color.blue(pixColor);                newR = (int) (0.393 * pixR + 0.769 * pixG + 0.189 * pixB);                newG = (int) (0.349 * pixR + 0.686 * pixG + 0.168 * pixB);                newB = (int) (0.272 * pixR + 0.534 * pixG + 0.131 * pixB);                int newColor = Color.argb(255, newR > 255 ? 255 : newR, newG > 255 ? 255 : newG, newB > 255 ? 255 : newB);                pixels[width * i + k] = newColor;            }        }        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);        long end = System.currentTimeMillis();        Log.e("bm", "used time="+(end - start));        return bitmap;    }

浮雕效果的公式:前一个像素点的RGB值减去当前的+127
 r = (r - r1 + 127);            g = (g - g1 + 127);            b = (b - b1 + 127);

public  Bitmap fudiao(Bitmap bm){        int Width = bm.getWidth();        int Height = bm.getHeight();        Bitmap bitmap = Bitmap.createBitmap(Width, Height, Bitmap.Config.ARGB_8888);        int color = 0,colorBefore = 0;        int a,r,g,b;        int r1,g1,b1;        int[] oldPx = new int[Width * Height];        int[] newPx = new int[Width * Height];        bm.getPixels(oldPx, 0, Width, 0, 0, Width, Height);        for(int i = 1; i < Width * Height; i++){            colorBefore = oldPx[i - 1];            a = Color.alpha(colorBefore);            r = Color.red(colorBefore);            g = Color.green(colorBefore);            b = Color.blue(colorBefore);            color = oldPx[i];            r1 = Color.red(color);            g1 = Color.green(color);            b1 = Color.blue(color);            r = (r - r1 + 127);            g = (g - g1 + 127);            b = (b - b1 + 127);            //检查各点像素值是否超出范围            if(r > 255){                r = 255;            }            if(g > 255){                g = 255;            }            if(b > 255){                b = 255;            }            newPx[i] = Color.argb(a, r, g, b);        }        bitmap.setPixels(newPx, 0, Width, 0, 0, Width, Height);        return bitmap;    }

高斯糊化我用的是android自带的RenderScript类来实现的:
用这个类需要在build.gradle的default config进行配置
//高斯效果renderscript配置        renderscriptTargetApi 24        renderscriptSupportModeEnabled 

实现代码:
//用android 3.0提供的Renderscript类实现    public static Bitmap blurBitmap(Context context, Bitmap bitmap) {        //用需要创建高斯模糊bitmap创建一个空的bitmap        Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);        // 初始化Renderscript,该类提供了RenderScript context,创建其他RS类之前必须先创建这个类,其控制RenderScript的初始化,资源管理及释放        RenderScript rs = RenderScript.create(context);        // 创建高斯模糊对象        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));        // 创建Allocations,此类是将数据传递给RenderScript内核的主要方 法,并制定一个后备类型存储给定类型        Allocation allIn = Allocation.createFromBitmap(rs, bitmap);        Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);        //设定模糊度(注:Radius最大只能设置25.f)        blurScript.setRadius(15.f);        // Perform the Renderscript        blurScript.setInput(allIn);        blurScript.forEach(allOut);        // Copy the final bitmap created by the out Allocation to the outBitmap        allOut.copyTo(outBitmap);        // recycle the original bitmap        // bitmap.recycle();        // After finishing everything, we destroy the Renderscript.        rs.destroy();        return outBitmap;    }

代码就这么多了,还有其它的效果,大家可按公式修改下就可以了:
接下来献上完整demo:
点击下载链接

原创粉丝点击