我想设置图像视图的对比度和亮度。参考How to programmatically change contrast of a bitmap in android?,这应该是最快和最有效的方法,我实现了如下代码:
代码:
final SeekBar SeekBar_contrast = (SeekBar) dialog_preview.findViewById(R.id.SeekBar_contrast);
final SeekBar SeekBar_brightness = (SeekBar) dialog_preview.findViewById(R.id.SeekBar_brightness);
SeekBar_contrast.setMax(10);
SeekBar_brightness.setMax(510);
SeekBar_contrast.setProgress((1));
SeekBar_brightness.setProgress((255));
SeekBar_contrast.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
{
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
new_bm = changeBitmapContrastBrightness(bimap, (float)(progress-1),(float)(SeekBar_brightness.getProgress()-255));
cropImageView.setImageBitmap(new_bm);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
}
});
SeekBar_brightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
{
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
new_bm = changeBitmapContrastBrightness(bimap, (float)(SeekBar_contrast.getProgress()-1), (float)(progress-255));
cropImageView.setImageBitmap(new_bm);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
}
});
方法:
public static Bitmap changeBitmapContrastBrightness(Bitmap bmp, float contrast, float brightness)
{
ColorMatrix cm = new ColorMatrix(new float[]
{
contrast, 0, 0, 0, brightness,
0, contrast, 0, 0, brightness,
0, 0, contrast, 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;
}
参数如下:
@param bmp input bitmap
* @param contrast 0..10 1 is default
* @param brightness -255..255 0 is default
* @return new bitmap
问题:
虽然亮度默认值是0,这是范围的中间值,因此滚动亮度seekbar它工作得很好,但我想问一下如何设置seekbar max值以及对比度的进度更改,因为默认值是1,不在范围的中间。
在滚动seekbar_对比度时,上面的代码将使图像在progress=0时显示“负颜色”,同时向右滚动(seekbar的较大值)将生成白色图像。
最佳答案
首先,在对比度seekbar的侦听器中,必须对seekbark的亮度值执行-255。
为了使对比度的默认值位于中间,请执行以下代码:
SeekBar_contrast.setMax(10);
SeekBar_contrast.setProgress((5));
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (progress <= 5) {
progress = 1 - progress / 5;
} else {
progress = 1 + (progress / 10) * 9
}
new_bm = changeBitmapContrastBrightness(bimap, (float)(progress-1),(float)SeekBar_brightness.getProgress() - 255);
cropImageView.setImageBitmap(new_bm);
}
如果你想增加对比度平滑-尝试使用任何非线性函数。