android调整图片的色相,饱和度,灰度
/**
* 调整图片的色相,饱和度,灰度
*
* @param srcBitmap
* @param rotate
* @param saturation
* @param scale
* @return
*/
public static Bitmap changeImageTheme(Bitmap srcBitmap, float rotate, float saturation, float scale) {
//调整色相
ColorMatrix rotateMatrix = new ColorMatrix();
rotateMatrix.setRotate(0, rotate);
rotateMatrix.setRotate(1, rotate);
rotateMatrix.setRotate(2, rotate);
//调整色彩饱和度
ColorMatrix saturationMatrix = new ColorMatrix();
saturationMatrix.setSaturation(saturation);
//调整灰度
ColorMatrix scaleMatrix = new ColorMatrix();
scaleMatrix.setScale(scale, scale, scale, 1);
//叠加效果
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.postConcat(rotateMatrix);
colorMatrix.postConcat(saturationMatrix);
colorMatrix.postConcat(scaleMatrix);
//创建一个大小相同的空白Bitmap
Bitmap dstBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), Bitmap.Config.ARGB_8888);
//载入Canvas,Paint
Canvas canvas = new Canvas(dstBitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
//绘图
canvas.drawBitmap(srcBitmap, 0, 0, paint);
return dstBitmap;
}
/**
* 通过更改图片像素点的RGBA值,生成底片效果
*
* @param scrBitmap
* @return
*/
public static Bitmap beautyImage(Bitmap scrBitmap) {
int width = scrBitmap.getWidth();
int height = scrBitmap.getHeight();
int count = width * height;
int[] oldPixels = new int[count];
int[] newPixels = new int[count];
scrBitmap.getPixels(oldPixels, 0, width, 0, 0, width, height);
for (int i = 0; i < oldPixels.length; i++) {
int pixel = oldPixels[i];
int r = Color.red(pixel);
int g = Color.green(pixel);
int b = Color.blue(pixel);
r = 255 - r;
g = 255 - g;
b = 255 - b;
if (r > 255) {
r = 255;
}
if (g > 255) {
g = 255;
}
if (b > 255) {
b = 255;
}
if (r < 0) {
r = 0;
}
if (g < 0) {
g = 0;
}
if (b < 0) {
b = 0;
}
newPixels[i] = Color.rgb(r, g, b);
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(newPixels, 0, width, 0, 0, width, height);
return bitmap;
}
// 黑白
public static final float colormatrix_heibai[] = {0.8f, 1.6f, 0.2f, 0,
-163.9f, 0.8f, 1.6f, 0.2f, 0, -163.9f, 0.8f, 1.6f, 0.2f, 0,
-163.9f, 0, 0, 0, 1.0f, 0};
// 怀旧
public static final float colormatrix_huaijiu[] = {0.2f, 0.5f, 0.1f, 0,
40.8f, 0.2f, 0.5f, 0.1f, 0, 40.8f, 0.2f, 0.5f, 0.1f, 0, 40.8f, 0,
0, 0, 1, 0};
// 哥特
public static final float colormatrix_gete[] = {1.9f, -0.3f, -0.2f, 0,
-87.0f, -0.2f, 1.7f, -0.1f, 0, -87.0f, -0.1f, -0.6f, 2.0f, 0,
-87.0f, 0, 0, 0, 1.0f, 0};
// 淡雅
public static final float colormatrix_danya[] = {0.6f, 0.3f, 0.1f, 0,
73.3f, 0.2f, 0.7f, 0.1f, 0, 73.3f, 0.2f, 0.3f, 0.4f, 0, 73.3f, 0,
0, 0, 1.0f, 0};
// 蓝调
public static final float colormatrix_landiao[] = {2.1f, -1.4f, 0.6f,
0.0f, -71.0f, -0.3f, 2.0f, -0.3f, 0.0f, -71.0f, -1.1f, -0.2f, 2.6f,
0.0f, -71.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f};
// 光晕
public static final float colormatrix_guangyun[] = {0.9f, 0, 0, 0, 64.9f,
0, 0.9f, 0, 0, 64.9f, 0, 0, 0.9f, 0, 64.9f, 0, 0, 0, 1.0f, 0};
// 梦幻
public static final float colormatrix_menghuan[] = {0.8f, 0.3f, 0.1f,
0.0f, 46.5f, 0.1f, 0.9f, 0.0f, 0.0f, 46.5f, 0.1f, 0.3f, 0.7f, 0.0f,
46.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f};
// 酒红
public static final float colormatrix_jiuhong[] = {1.2f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.9f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.8f, 0.0f, 0.0f,
0, 0, 0, 1.0f, 0};
// 胶片
public static final float colormatrix_fanse[] = {-1.0f, 0.0f, 0.0f, 0.0f,
255.0f, 0.0f, -1.0f, 0.0f, 0.0f, 255.0f, 0.0f, 0.0f, -1.0f, 0.0f,
255.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f};
// 湖光掠影
public static final float colormatrix_huguang[] = {0.8f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.9f, 0.0f, 0.0f,
0, 0, 0, 1.0f, 0};
// 褐片
public static final float colormatrix_hepian[] = {1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.8f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.8f, 0.0f, 0.0f,
0, 0, 0, 1.0f, 0};
// 复古
public static final float colormatrix_fugu[] = {0.9f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.8f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f,
0, 0, 0, 1.0f, 0};
// 泛黄
public static final float colormatrix_huan_huang[] = {1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f,
0.0f, 0, 0, 0, 1.0f, 0};
// 传统
public static final float colormatrix_chuan_tong[] = {1.0f, 0.0f, 0.0f, 0,
-10f, 0.0f, 1.0f, 0.0f, 0, -10f, 0.0f, 0.0f, 1.0f, 0, -10f, 0, 0,
0, 1, 0};
// 胶片2
public static final float colormatrix_jiao_pian[] = {0.71f, 0.2f, 0.0f,
0.0f, 60.0f, 0.0f, 0.94f, 0.0f, 0.0f, 60.0f, 0.0f, 0.0f, 0.62f,
0.0f, 60.0f, 0, 0, 0, 1.0f, 0};
// 锐色
public static final float colormatrix_ruise[] = {4.8f, -1.0f, -0.1f, 0,
-388.4f, -0.5f, 4.4f, -0.1f, 0, -388.4f, -0.5f, -1.0f, 5.2f, 0,
-388.4f, 0, 0, 0, 1.0f, 0};
// 清宁
public static final float colormatrix_qingning[] = {0.9f, 0, 0, 0, 0, 0,
1.1f, 0, 0, 0, 0, 0, 0.9f, 0, 0, 0, 0, 0, 1.0f, 0};
// 浪漫
public static final float colormatrix_langman[] = {0.9f, 0, 0, 0, 63.0f,
0, 0.9f, 0, 0, 63.0f, 0, 0, 0.9f, 0, 63.0f, 0, 0, 0, 1.0f, 0};
// 夜色
public static final float colormatrix_yese[] = {1.0f, 0.0f, 0.0f, 0.0f,
-66.6f, 0.0f, 1.1f, 0.0f, 0.0f, -66.6f, 0.0f, 0.0f, 1.0f, 0.0f,
-66.6f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f};