我正在开发一个应用程序,用户可以在其中更改图像亮度,对比度,饱和度等。我可以使用下面的代码单独完成所有这些。
seekBar_brightness.setMax(512);
seekBar_brightness.setProgress(255);
seekBar_brightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progress = progress - 255;
Log.e("ImageColor_bri global", "" + bitmap_global);
Bitmap new_bm = changeBrightness(bitmap_global, (float) progress);
Log.e("ImageColor_bri local", "" + new_bm);
imageView.setImageBitmap(new_bm);
bitmap_local = new_bm;
// imageView.getDrawable().setColorFilter(ColorFilterGenerator.adjustBrightness(progress));//foto is my ImageView
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
seekbar_saturation.setMax(512);
seekbar_saturation.setProgress(255);
//seekbar_saturation.setThumbOffset(255);
seekbar_saturation.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progress = progress-255;
// imageView.buildDrawingCache();
// Bitmap bimap = imageView.getDrawingCache();
// imageView.setColorFilter(value);
Log.e("ImageColor_satu global", "" + bitmap_global);
Bitmap new_bm = adjustSaturation(bitmap_global, progress);
Log.e("ImageColor_satu local", "" + new_bm);
imageView.setImageBitmap(new_bm);
bitmap_local = new_bm;
// ColorMatrix matrix = new ColorMatrix();
// matrix.setSaturation(progress);
// ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
// imageView.getDrawable().setColorFilter(ColorFilterGenerator.adjustSaturation(progress));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
public static Bitmap changeBrightness(Bitmap bmp, float brightness) {
ColorMatrix cm = new ColorMatrix(new float[]
{
1, 0, 0, 0, brightness,
0, 1, 0, 0, brightness,
0, 0, 1, 0, brightness,
0, 0, 0, 1, 0
});
Bitmap ret = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
Canvas canvas = new Canvas(ret);
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bmp, 0, 0, paint);
return ret;
}
public static Bitmap adjustSaturation(Bitmap bmp, float value) {
value = cleanValue(value, 100);
if (value == 0) {
return bmp;
}
float x = 1 + ((value > 0) ? 3 * value / 100 : value / 100);
float lumR = 0.3086f;
float lumG = 0.6094f;
float lumB = 0.0820f;
float[] mat = new float[]
{
lumR * (1 - x) + x, lumG * (1 - x), lumB * (1 - x), 0, 0,
lumR * (1 - x), lumG * (1 - x) + x, lumB * (1 - x), 0, 0,
lumR * (1 - x), lumG * (1 - x), lumB * (1 - x) + x, 0, 0,
0, 0, 0, 1, 0,
0, 0, 0, 0, 1
};
ColorMatrix cm = new ColorMatrix(mat);
Bitmap ret = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
Canvas canvas = new Canvas(ret);
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bmp, 0, 0, paint);
return ret;
}
问题是:在增加或减少图像亮度并增加或减少饱和度值后,完全改变图像颜色。
我必须实现这一点,如果我反转到亮度和饱和度的搜索条的初始值,我应该在第一次得到实际图像。
例如如果将亮度值从0增加到100然后将饱和度值从0更改为80.然后我将亮度和饱和度值反转为0.然后我应该得到原始图像因为它是第一次。
但是现在图像颜色在扭转其值时不会反转。请帮忙。提前完成。
答案 0 :(得分:0)
这是因为最大和最小亮度(以及饱和度和其他参数)值。例如。如果当前亮度为200并且ins增加100,则亮度值变为255(而不是300),因为255是最大值。如果当前亮度(现在为255)减少100,则变为255 - 100 = 155并且不等于初始值(200)。为了避免这种情况,您应该存储初始位图并且不更改它,但是通过ColorMatrixColorFilter
更改postConcat()
并且每次都应用初始位图的颜色过滤器,但不应用于修改后的位图。
因此,您应该为ColorMatrix
创建全局值,并在changeBrightness()
和adjustSaturation()
方法中进行更改,然后始终将其应用于初始位图。