android 图片操作之色彩变换
2016-01-02 15:55
465 查看
本博客为 http://www.imooc.com/video/6480 的笔记
1 可以通过 ColorMatrix 类 ,操作底层维护的5*4的float颜色变换矩阵,
来改变图片的三基色,透明度等来操作图片.
2 通过获取图片上所有像素点的信息,通过科学公式的变换
来操作像素点来达到操作图片的效果
//图像处理的工具类
1 可以通过 ColorMatrix 类 ,操作底层维护的5*4的float颜色变换矩阵,
来改变图片的三基色,透明度等来操作图片.
2 通过获取图片上所有像素点的信息,通过科学公式的变换
来操作像素点来达到操作图片的效果
//图像处理的工具类
[code] /** * 图像处理的帮助类 * * */ public class ImageHelper { /** * 底部起始是操作了float类型的数组 * @param bm * @param hue * @param saturation * @param lum * @return */ public static Bitmap ImageEffect(Bitmap bm,float hue,float saturation,float lum){ Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bmp); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); ColorMatrix hueMatrix = new ColorMatrix(); hueMatrix.setRotate(0, hue); hueMatrix.setRotate(1, hue); hueMatrix.setRotate(2, hue); ColorMatrix saturationMatrix = new ColorMatrix(); saturationMatrix.setSaturation(saturation); ColorMatrix lumMatrix = new ColorMatrix(); lumMatrix.setScale(lum, lum, lum, 1); ColorMatrix imageMatrix = new ColorMatrix(); imageMatrix.postConcat(hueMatrix); imageMatrix.postConcat(saturationMatrix); imageMatrix.postConcat(lumMatrix); paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix)); canvas.drawBitmap(bm, 0, 0, paint); return bmp; } /** * * @param bitmap 原图片 * @param data 长度为20 的数组 数组中的值为0或者1 * @return 处理后的图片 */ public static Bitmap ImageEffect(Bitmap bitmap ,float[] data){ if(data==null) return null; if(data.length!=20) return null; //在内存中的图片 Bitmap bm=Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); ColorMatrix matrix=new ColorMatrix(data); Canvas mCanvas=new Canvas(bm); Paint mPaint=new Paint(); mPaint.setAntiAlias(true); mPaint.setDither(true); mPaint.setColorFilter(new ColorMatrixColorFilter(matrix)); mCanvas.drawBitmap(bm, 0, 0,mPaint); return bm; } /** * 将图片转换成底色的效果 * @param bitmap * @return */ public static Bitmap handleImageNegative(Bitmap bm){ int width = bm.getWidth(); int height = bm.getHeight(); int color; int r, g, b, a; Bitmap bmp = Bitmap.createBitmap(width, height , Bitmap.Config.ARGB_8888); int[] oldPx = new int[width * height]; int[] newPx = new int[width * height]; bm.getPixels(oldPx, 0, width, 0, 0, width, height); for (int i = 0; i < width * height; i++) { color = oldPx[i]; r = Color.red(color); g = Color.green(color); b = Color.blue(color); a = Color.alpha(color); //底片效果的转换公式 r = 255 - r; g = 255 - g; b = 255 - b; if (r > 255) { r = 255; } else if (r < 0) { r = 0; } if (g > 255) { g = 255; } else if (g < 0) { g = 0; } if (b > 255) { b = 255; } else if (b < 0) { b = 0; } newPx[i] = Color.argb(a, r, g, b); } bmp.setPixels(newPx, 0, width, 0, 0, width, height); return bmp; } /** * 老照片的处理效果 * @param bm * @return */ public static Bitmap handleImagePixelsOldPhoto(Bitmap bm) { Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888); int width = bm.getWidth(); int height = bm.getHeight(); int color = 0; int r, g, b, a, r1, g1, b1; int[] oldPx = new int[width * height]; int[] newPx = new int[width * height]; bm.getPixels(oldPx, 0, bm.getWidth(), 0, 0, width, height); for (int i = 0; i < width * height; i++) { color = oldPx[i]; a = Color.alpha(color); r = Color.red(color); g = Color.green(color); b = Color.blue(color); //老照片处理效果的公式 r1 = (int) (0.393 * r + 0.769 * g + 0.189 * b); g1 = (int) (0.349 * r + 0.686 * g + 0.168 * b); b1 = (int) (0.272 * r + 0.534 * g + 0.131 * b); if (r1 > 255) { r1 = 255; } if (g1 > 255) { g1 = 255; } if (b1 > 255) { b1 = 255; } newPx[i] = Color.argb(a, r1, g1, b1); } bmp.setPixels(newPx, 0, width, 0, 0, width, height); return bmp; } /** * 浮雕效果 */ public static Bitmap handleImagePixelsRelief(Bitmap bm) { Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888); int width = bm.getWidth(); int height = bm.getHeight(); 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, bm.getWidth(), 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); } bmp.setPixels(newPx, 0, width, 0, 0, width, height); return bmp; } }
相关文章推荐
- Android studio提交SVN需要忽略的文件
- android post提交数据
- Android 使用开源xUtils来实现多线程下载(非原创)
- Android应用最广的模式-单例模式
- Android 仿Path效果ArcMenu
- Android Studio使用
- android - activity和fragment生命周期
- Android——universal-imageloader开源库的使用
- Android中的FragmentManager的问题
- 【Android】自定义View -- 钟表
- 六款值得推荐的android(安卓)开源框架简介
- android 下多线程断点下载服务器文件
- java版android Handler机制模型
- Android菜鸟的成长笔记(27)——ViewPager的使用
- Android 多线程断点下载(非原创)
- 菜鸟初学android体验之——实现自定义简单标题栏的两种简单方法
- Android IPC机制(一)开启多进程
- Android存储文件的方法
- Android.mk编译.apk .so .jar .a第三方.apk .so .jar .a的方法
- android异步下载图片并显示水平进度条