We first set a fixed value of the color matrix to ordinary image color modification, black and white, warm color, cool color three changes, and then through the dynamic adjustment of tone, saturation, and brightness to achieve the image color adjustment. This section of our journey begins. First, we can set ColorFilter directly to the View Bitamp to realize the color change.
Gray=R*0.3+G*0.59+B*0.11
float[] mBWMatrix = {
0.3 f.0.59 f.0.11 f.0.0.0.3 f.0.59 f.0.11 f.0.0.0.3 f.0.59 f.0.11 f.0.0.0.0.0.1.0};
// Warm color treatment can increase the value of the red and green channels
float[] mWarmMatrix = {
2.0.0.0.0.0.2.0.0.0.0.0.1.0.0.0.0.0.1.0};
// Cold tone processing can be done by simply increasing the value of the blue channel
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);
}
Copy the code
We can also change the saturation of the color directly to black and white
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;
}
Copy the code
The effect is as follows:
ColorMatrix also provides matrix multiplication so that you can modify the tone, saturation, and brightness of the image at the same time.
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.0 F / midValue * 180;
break;
case R.id.sb_saturation:
mSaturaion = progress*1.0 f/midValue;
break;
case R.id.sb_lum:
mLum = progress*1.0 f/midValue;
break;
}
Log.d(TAG, "onProgressChanged: midValue="+midValue+" mhue="+mHue+" msaturation="+mSaturaion+" mlum="+mLum+" progress="+progress);
ivImage.setImageBitmap(handleImageEffect(mBitmap,mHue,mSaturaion,mLum));
}
/** * Color, saturation and brightness are multiplied by PostConcat of ColorMatrix, Transform the original image * @param oriBmp * @param Hue adjustment range -180 degrees to 180 degrees * @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();
// Adjust the tone
Log.i(TAG, "HandleImageEffect: tonal rotate ="+hue);
ColorMatrix hueMatrix = new ColorMatrix();
hueMatrix.setRotate(0, hue);// Rotate the hue Angle around red
hueMatrix.setRotate(1, hue);// Rotate the hue Angle around green
hueMatrix.setRotate(2, hue);// Rotate the hue Angle around blue
// Adjust the saturation
Log.i(TAG, "HandleImageEffect: saturation="+saturation);
ColorMatrix saturationMatrix = new ColorMatrix();
saturationMatrix.setSaturation(saturation);
// Adjust the brightness
Log.i(TAG, "HandleImageEffect: brightness 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;
}
Copy the code
The effect is as follows: