Android: change image contrast and brightness on scroll scrolling

I would like to set the contrast and brightness of the image. Referring to How to programmatically change the contrast of a bitmap in android? which should be the fastest and most efficient way to do it, I have implemented the code like this:

code:

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) 
             {

             }                  
        });

      

Method:

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;
}

      

The parameter looks like this:

 @param bmp input bitmap
 * @param contrast 0..10 1 is default
 * @param brightness -255..255 0 is default
 * @return new bitmap

      

Question:

While the default value of brightness is 0, which is the middle of the range and therefore scrolls the brightness search bar, it works great, I would like to ask how to set the maximum value of the search and traverse parameter for contrast, since the default is 1, which is not a range medium.

When scrolling the seekbar_contrast pointer, the above code will render the image with negative color at progress = 0, and when scrolling to the right (larger seek value) it will produce a white image.

+3


source to share


1 answer


First of all, in the onProgressChanged

contrast seek listener, you need to do -255 for the seekbark luminance value.

To make the default contrast value placed in the middle, run the 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 increase the contrast, try using any non-linear function.

+1


source







All Articles