I would like to set the contrast and brightness of a imageview. Referring from How to programmatically change contrast of a bitmap in android?, which should be the most fastest and efficient way of doing so, I have implemented the code as follows:
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
While the brightness default value is 0, which is the middle of the range and hence scrolling the brightness seekbar it is working perfectly, I would like to ask how to set for the seekbar max value and progress changes for the contrast, as the default value is 1, which is not the middle of the range.
Upon the scrolling of the seekbar_contrast, the code above would make the image showing "negative-color" at progress =0, while scrolling to right (bigger value of the seekbar) it produces a white image.
First of all, in onProgressChanged
listener of contrast seekbar you have to do -255 for value of brightness of seekbark.
In order to make default value of contrast be placed in middle go ahead with following code:
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);
}
If you want to do increasing of contrast smoother - try to use any non linear function.