下面的函数成功地改变了图片的对比度和亮度。
Bitmap bmp;
ImageView alteredImageView;
...
public void drawAlteredImage(float contr,float bright) {
Bitmap alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
Canvas canvas = new Canvas(alteredBitmap);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.set(new float[] {
contr, 0, 0, 0, bright,
0, contr, 0, 0, bright,
0, 0, contr, 0, bright,
0, 0, 0, 1, 0 });
paint.setColorFilter(new ColorMatrixColorFilter(cm));
Matrix matrix = new Matrix();
canvas.drawBitmap(bmp, matrix, paint);
alteredImageView.setImageBitmap(alteredBitmap);
}
但是当我将 setSaturation 方法添加到 ColorMatrix 时,对比度和亮度改变停止工作。编码:
public void drawAlteredImage(float contr,float bright,float satur) {
Bitmap alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
Canvas canvas = new Canvas(alteredBitmap);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.set(new float[] {
contr, 0, 0, 0, bright,
0, contr, 0, 0, bright,
0, 0, contr, 0, bright,
0, 0, 0, 1, 0 });
cm.setSaturation(satur);
paint.setColorFilter(new ColorMatrixColorFilter(cm));
Matrix matrix = new Matrix();
canvas.drawBitmap(bmp, matrix, paint);
alteredImageView.setImageBitmap(alteredBitmap);
}
在这种情况下,仅应用饱和效应。为什么会出现这个问题?我该如何解决?
最佳答案
setSaturation(...)
方法替换矩阵中的所有值。您可以创建一个组合了这两种操作的矩阵,也可以使用两个单独的 drawBitmap(...)
调用来执行这些操作。
尝试:
cm.setSaturation(satur);
final float m[] = cm.getArray();
final float c = contr;
cm.set(new float[] {
m[ 0] * c, m[ 1] * c, m[ 2] * c, m[ 3] * c, m[ 4] * c + bright,
m[ 5] * c, m[ 6] * c, m[ 7] * c, m[ 8] * c, m[ 9] * c + bright,
m[10] * c, m[11] * c, m[12] * c, m[13] * c, m[14] * c + bright,
m[15] , m[16] , m[17] , m[18] , m[19] });
关于android - 将 Saturation 和 ColorFilter 同时设置为图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7413867/