图像的色彩处理
一、ColorMatrix
在色彩处理中,通常使用以下三个角度描述的:色调、饱和度、亮度,在Android的系统中,封装了一个ColorMatrix类来处理图像的色彩。
API:
1、setRotate(int axis, float degrees)
设置颜色的色调。第一个参数系统分别使用0、1、2来代表Red、Green、Blues三种颜色的处理;而第二个参数就是需要处理的值
2、setSaturation(float sat)
设置颜色的饱和度
3、setScale(float rScale, float gScale, float bScale, float aScale)
设置颜色的亮度
4、preConcat(ColorMatrix prematrix)
将颜色矩阵的作用效果混合,达到叠加的处理效果。
使用:
设置好颜色矩阵以后,通过Paint类的setColorFilter()方法将颜色矩阵的效果作用于图像上,注意,Android系统是不允许直接修改原图的,所以必须通过原图创建一个同样大小的Bitmap,并将原图绘制到该Bitmap中,以一个副本的形式修改图像。
Demo:
public static Bitmap handleImage(Bitmap bm,float hue,float satuation,float lum){
Bitmap bitmap = Bitmap.createBitmap(bm.getWidth(),bm.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
//色调
android.graphics.ColorMatrix hueMatrix = new android.graphics.ColorMatrix();
hueMatrix.setRotate(0,hue);
hueMatrix.setRotate(1,hue);
hueMatrix.setRotate(2,hue);
//饱和度
android.graphics.ColorMatrix satuationMatrix = new android.graphics.ColorMatrix();
satuationMatrix.setSaturation(satuation);
//亮度
android.graphics.ColorMatrix lumMatrix = new android.graphics.ColorMatrix();
lumMatrix.setScale(lum,lum,lum,1);
//混合
android.graphics.ColorMatrix imageMatrix = new android.graphics.ColorMatrix();
imageMatrix.postConcat(hueMatrix);
imageMatrix.postConcat(satuationMatrix);
imageMatrix.postConcat(lumMatrix);
//设置画笔
paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
canvas.drawBitmap(bitmap,0,0,paint);
return bitmap;
}
二、像素点处理
含义:
像素点处理方式是通过改变图像的每个像素点的具体ARGB值来达到处理图像色彩的效果。
API:
1、getPixels(int[] pixels, int offset, int stride, int x, int y, int width, int height)
获取图像的像素值。
pixels 接收位图颜色值的数组
offset 写入到pixels[]中的第一个像素索引值
stride pixels[]中的行间距(多少为一行,通常是位图的width)
x 从位图中读取的第一个像素的x的坐标值
y 从位图中读取的第一个像素的y的坐标值
width 从每一行中读取的像素宽度
height 读取的行数
2、setPixels(int[] pixels, int offset, int stride, int x, int y, int width, int height)
设置图像的像素值,参数同上
Demo:
同样不能在原始图片上处理,要生成一个副本
public static Bitmap handleImagePixs(Bitmap bm){
int width = bm.getWidth();
int height =bm.getHeight();
int color,r,g,b,a;
Bitmap bitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
int[] oldPixs = new int[width*height];
int[] newPixs = new int[width*height];
//获取像素点
bm.getPixels(oldPixs,0,width,0,0,width,height);
//遍历所有像素点
for (int i = 0; i < oldPixs.length; i++) {
color = oldPixs[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;
}
//合成新的像素点
newPixs[i]=Color.argb(a,r,g,b);
}
//根据新的像素点生成新的图片
bitmap.setPixels(newPixs,0,width,0,0,width,height);
return bitmap;
}
图像的图形处理
一、Matrix
android系统处理图像的色彩变化是通过ColorMatrix处理,而处理图像的图形变化则通过Matrix处理。
API:
1、setRotate(float degrees)
旋转
2、setScale(float sx, float sy)
缩放
3、setSkew(float kx, float ky)
错切
4、setTranslate(float dx, float dy)
平移
5、setValues(float[] values)
把3x3矩阵的值复制到矩阵里面
运用:
多用于自定义View的时候图像变换的处理,在OnDraw()方法里面调用canvans.drawBitmap(Bitmap bitmap,Matrix matrix,Paint paint)。
二、像素块
含义:
把图像分成一个个小块,然后通过改变每一个图像块来实现对图像的图形变化。
API:
1、Canvas的 drawBitmapMesh(Bitmap bitmap, int meshWidth, int meshHeight, float[] verts, int vertOffset, int[] colors, int colorOffset, Paint paint)
bitmap 将要扭曲的图片
meshwidth 需要的横向网格数目
meshHeight 需要的纵向网格数目
verts 网格交叉点坐标数组
vertOffset verts数组中开始跳过的(x,y)坐标对的数目、
其中最重要的是verts数组
三、PoterDuffXformode
参考我的另一篇文章(http://blog.youkuaiyun.com/lepaitianshi/article/details/50560527)
四、Shader
参考我的另一篇文章
(http://blog.youkuaiyun.com/lepaitianshi/article/details/50560543)
五、Drawable
参考另一篇文章
(http://blog.youkuaiyun.com/lepaitianshi/article/details/50560575)