title: Android: Changing image contrast and brightness upon scrolling seekbar
body: <p>I would like to set the contrast and brightness of a imageview. Referring from <a href="https://stackoverflow.com/questions/12891520/how-to-programmatically-change-contrast-of-a-bitmap-in-android">How to programmatically change contrast of a bitmap in android?</a>, which should be the most fastest and efficient way of doing so, I have implemented the code as follows:</p>
  
  <h2>Code:</h2>
  
  <pre><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) 
               {
  
               }                  
          });
  </code></pre>
  
  <h2>Method:</h2>
  
  <pre><code>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;
  }
  </code></pre>
  
  <h2>The param are as follows:</h2>
  
  <pre><code> @param bmp input bitmap
   * @param contrast 0..10 1 is default
   * @param brightness -255..255 0 is default
   * @return new bitmap
  </code></pre>
  
  <h2>Question:</h2>
  
  <p>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. </p>
  
  <p>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.</p>