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:
点击下载链接
阅读全文
0 0
- android图片效果处理
- android中几种常用的图片处理效果
- android图片特效处理之光晕效果
- android图片特效处理之光照效果
- android图片特效处理之锐化效果
- android图片特效处理之模糊效果
- android图片特效处理之怀旧效果
- android图片特效处理之光晕效果
- android图片特效处理之锐化效果
- android图片特效处理之模糊效果
- android图片特效处理之怀旧效果
- android图片特效处理之光照效果
- android图片特效处理之模糊效果
- android图片特效处理之锐化效果
- android图片特效处理之光照效果
- android图片特效处理之模糊效果
- android图片特效处理之锐化效果
- android图片特效处理之光照效果
- 刷新页面后,数据保留在原页面方法(session信息共享,ajax局部刷新)
- Invocation of init method failed; nested exception is org.hibernate.MappingException
- 关于java中的Properties类
- TensorFlow 安装
- Java中事务的特性
- android图片效果处理
- 《深入理解java虚拟机》学习-第三章-内存分配策略
- 如何在PHP方面进一步提高
- JAVA-数组去重,并且计算每个值的重复次数
- 最大子矩阵问题----悬线法
- Git@OSC实现从本地添加项目到远程仓库
- 【VC】中自定义消息实现
- Java反射机制知识总结
- 算法题目---圆圈中最后剩下的数字