音视频开发之ColorFilter对View进行换色

574 阅读2分钟

我们先通过颜色矩阵设置固定的值来对普通图片的颜色修改,实现黑白、暖色、冷色三种变化,然后在 通过动态的调整色调、饱和度、以及亮度进行实现图片的颜色调节。下面开启我们这一小节的旅程。 首先我们可以直接给View的Bitamp设置ColorFilter,来实现颜色的变化。

//黑白模式 Gray=R*0.3+G*0.59+B*0.11 
float[] mBWMatrix = {
0.3f,0.59f,0.11f,0,0,
0.3f,0.59f,0.11f,0,0,
0.3f,0.59f,0.11f,0,0,
0,0,0,1,0};
//暖色调的处理可以增加红绿通道的值
float[] mWarmMatrix = {
2,0,0,0,0,
0,2,0,0,0,
0,0,1,0,0,
0,0,0,1,0};
//冷色调的处理可以通过单一增加蓝色通道的值
float[] mCoolMatrix = {
1,0,0,0,0,
0,1,0,0,0,
0,0,2,0,0,
0,0,0,1,0};
public void setImageMatrix(float[] mColorMatrix) 
{ Bitmap bmp =
Bitmap.createBitmap(mBitmap.getWidth(),mBitmap.getHeight(),Bitmap.Config.ARGB_88 
88);
ColorMatrix colorMatrix = new ColorMatrix(); 
colorMatrix.set(mColorMatrix);
Canvas canvas = new Canvas(bmp); 
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix)); 
canvas.drawBitmap(mBitmap,0,0,paint);
ivImage.setImageBitmap(bmp);
}

我们也可以直接通过修改颜色的饱和度来实现黑白色

public static Bitmap handleImageEffect(Bitmap oriBmp, float hue, float 
saturation, float lum) {
Bitmap bmp = Bitmap.createBitmap(oriBmp.getWidth(), oriBmp.getHeight(), 
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp); 
Paint paint = new Paint();
ColorMatrix saturationMatrix = new ColorMatrix(); 
saturationMatrix.setSaturation(saturation);
paint.setColorFilter(new ColorMatrixColorFilter(saturationMatrix)); 
canvas.drawBitmap(oriBmp, 0, 0, paint);
return bmp;
}

效果如下:

ColorMatrix也提供了矩阵相乘的功能,这样就可以同时图片修改色调、饱和度、亮度。

private float mSaturaion =1; 
private float mLum=1; 
private float mHue=0; 
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean 
fromUser) {
int id = seekBar.getId(); 
switch (id){
case R.id.sb_hue:
mHue = (progress - midValue) * 1.0F / midValue * 180; 
break;
case R.id.sb_saturation:
mSaturaion = progress*1.0f/midValue; 
break;
case R.id.sb_lum:
mLum = progress*1.0f/midValue; 
break;
}
Log.d(TAG, "onProgressChanged: midValue="+midValue+" mhue="+mHue+" 
msaturation="+mSaturaion+" mlum="+mLum+" progress="+progress);
ivImage.setImageBitmap(handleImageEffect(mBitmap,mHue,mSaturaion,mLum));
}
/**
* 色调、饱和度、亮度 通过ColorMatrix的PostConcat相乘,对原始图片进行变换处理
* @param oriBmp
* @param hue 色调调节范围 -180度至180度 * @param saturation
* @param lum
* @return
*/
public static Bitmap handleImageEffect(Bitmap oriBmp, float hue, float 
saturation, float lum) {
Bitmap bmp = Bitmap.createBitmap(oriBmp.getWidth(), oriBmp.getHeight(), 
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp); 
Paint paint = new Paint();
//调节色调
Log.i(TAG, "handleImageEffect: 色调 rotate="+hue); 
ColorMatrix hueMatrix = new ColorMatrix(); 
hueMatrix.setRotate(0, hue);//围绕red旋转 hue角度
hueMatrix.setRotate(1, hue);//围绕green旋转 hue角度
hueMatrix.setRotate(2, hue);//围绕blue旋转 hue角度
//调节饱和度
Log.i(TAG, "handleImageEffect: 饱和度saturation="+saturation); 
ColorMatrix saturationMatrix = new ColorMatrix(); 
saturationMatrix.setSaturation(saturation);
//调节亮度
Log.i(TAG, "handleImageEffect: 亮度lum="+lum); 
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(oriBmp, 0, 0, paint);
return bmp;
}

效果如下: