ColorMatrix的使用
来源:互联网 发布:杭州雅思考试 知乎 编辑:程序博客网 时间:2024/05/22 19:10
花了二天的时间,自己写了一个Colormatrix的工具类,实现了一个美图秀秀效果。下面的几个算法我也不是很清楚,很敬佩那些研究算法的大牛们!
public class ImageHelper { public static Bitmap HandleImageEffect(Bitmap bitmap,float hue,float saturate,float lum){ Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), Bitmap.Config.ARGB_8888); //创建画布 Canvas canvas = new Canvas(newBitmap); //创建画笔 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); //拿到ColorMatrix的对象 ColorMatrix hueMatrix = new ColorMatrix(); //设置色调,0代表红色,1代表绿色,2代表蓝色 hueMatrix.setRotate(0,hue); hueMatrix.setRotate(1,hue); hueMatrix.setRotate(2,hue); ColorMatrix saturateMatrix = new ColorMatrix(); //设置饱和度 saturateMatrix.setSaturation(saturate); ColorMatrix lumMatrix = new ColorMatrix(); //设置亮度 lumMatrix.setScale(lum,lum,lum,1); ColorMatrix ImageMatrix = new ColorMatrix(); //将3个一起使用 ImageMatrix.postConcat(hueMatrix); ImageMatrix.postConcat(saturateMatrix); ImageMatrix.postConcat(lumMatrix); paint.setColorFilter(new ColorMatrixColorFilter(ImageMatrix)); canvas.drawBitmap(bitmap,0,0,paint); return newBitmap; }
//下面3个小Demo通过改变像素点,来实现效果 //底片效果 public static Bitmap handleNegative(Bitmap bm){ int width = bm.getWidth(); int height = bm.getHeight(); int color; int r,g,b,a; //原来照片的像素 int oldPix[] = new int[width*height]; //新生成的像素 int newPix[] = new int[width*height]; //创建新的位图,注意:传进来的位图参数不能更改 Bitmap bitmap = Bitmap.createBitmap( width,height, Bitmap.Config.ARGB_8888); //得到原图的像素点 bm.getPixels(oldPix,0,width,0,0,width,height); for (int i = 0; i < width*height; i++) { color=oldPix[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; //保证每个像素点在0-255之间 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; } newPix[i] = Color.argb(a,r,g,b); } bitmap.setPixels(newPix,0,width,0,0,width,height); return bitmap; } //复古 public static Bitmap handleImagePixelsOldPhoto(Bitmap bm) { int width = bm.getWidth(); int height = bm.getHeight(); int color; int r,g,b,a,r1,g1,b1,a1; int oldPix[] = new int[width*height]; int newPix[] = new int[width*height]; Bitmap bitmap = Bitmap.createBitmap( width,height, Bitmap.Config.ARGB_8888); bm.getPixels(oldPix,0,width,0,0,width,height); for (int i = 0; i < width*height; i++) { color=oldPix[i]; r = Color.red(color); g= Color.green(color); b = Color.blue(color); a = Color.alpha(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; }else if(r1<0){ r1 =0; } if(g1>255){ g1=255; }else if(g1<0){ g1=0; } if(b1>255){ b1=255; }else if(b1<0){ b1 =0; } newPix[i] = Color.argb(a,r1,g1,b1); } bitmap.setPixels(newPix,0,width,0,0,width,height); return bitmap; } //浮雕 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); //注意:浮雕效果图需要前一位的像素点,所以从i=1开始并不是0 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; }
下面是我的布局:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"><LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" > <ImageView android:layout_width="0dp" android:id="@+id/imageView1" android:layout_height="match_parent" android:layout_weight="1"/> <ImageView android:layout_width="0dp" android:id="@+id/imageView2" android:layout_height="match_parent" android:layout_weight="1"/></LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" > <ImageView android:id="@+id/imageView3" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <ImageView android:layout_width="0dp" android:id="@+id/imageView4" android:layout_height="match_parent" android:layout_weight="1"/> </LinearLayout></LinearLayout>
布局写完之后就要通过java代码去调用我们的工具类了。
package com.yu.colormetrix;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.widget.ImageView;/** * Created by Administrator on 2016/3/31. */public class Cang extends Activity { private ImageView imageView1; private ImageView imageView2; private ImageView imageView3; private ImageView imageView4; private Bitmap bitmap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.cang); //原图 imageView1= (ImageView) findViewById(R.id.imageView1); //底片效果 imageView2= (ImageView) findViewById(R.id.imageView2); //复古 imageView3= (ImageView) findViewById(R.id.imageView3); //浮雕 imageView4= (ImageView) findViewById(R.id.imageView4); bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test2); imageView1.setImageBitmap(bitmap); imageView2.setImageBitmap(ImageHelper.handleNegative(bitmap)); imageView3.setImageBitmap(ImageHelper.handleImagePixelsOldPhoto(bitmap)); imageView4.setImageBitmap(ImageHelper.handleImagePixelsRelief(bitmap)); }}最后我们来看一下最终的效果图,这里我们使用的是大家所熟悉的仓老师美图.![这里写图片描述](http://img.blog.csdn.net/20160331172715386)
1 0
- ColorMatrix的使用
- ColorMatrix的使用
- 颜色矩阵ColorMatrix的使用
- 使用colorMatrix
- 使用ColorMatrix改变图片的饱和度
- Android 矩阵简述 ColorMatrix 的使用
- android matrix&colormatrix 使用
- C# 使用ColorMatrix时各种效果的参数
- ColorMatrix的含义
- As3的ColorMatrix类
- GDI+ ColorMatrix的揭秘
- colorMatrix
- ColorMatrix
- ColorMatrix
- ColorMatrix
- GDI+ ColorMatrix的完全揭秘
- GDI+ ColorMatrix的完全揭秘
- Android中ColorMatrix的学习
- iOS给UILabel设置缩进、行距、字体颜色
- unix域socket绑定路径不创建文件问题[c语言]
- Android中横竖屏切换时,onConfigurationChanged方法不被调用
- 二维码的使用(java版)
- java中GZIPOutputStream 流的使用(EOFException)
- ColorMatrix的使用
- 整体二分 hdu5412 CRB and Queries
- Android蓝牙开发详解
- python 正则匹配电话与ip
- Android AlarmManager实现不间断轮询服务
- [20160331] Sleep Recorder !
- hbase shell - 使用filter进行scan
- VB作业
- 对三级标签理解的一些修正