0

我正在開發一個應用程序,用戶可以在其中更改圖像亮度,對比度,飽和度等。我可以使用下面的代碼單獨完成所有這些操作。Android圖片顏色校正。如何更改Android中單個圖像的亮度,飽和度等?

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

問題是:增加或減小圖像的亮度和增加或減小飽和度值之後完全改變了圖像的顏色。

我必須做到這一點,如果我逆轉到seekbar的亮度和飽和度的初始值,我應該得到實際圖像時,它是在第一次。

例如如果增加亮度的從0到100的值,則飽和度的值從0至80.然後我扭轉亮度和飽和度值設置爲0改變然後我應該得到的原始圖像,因爲它是在第一次。

但是現在圖像顏色在逆轉其值時不會逆轉。請幫忙。提前Thanx。

回答

0

這是因爲最大和最小亮度(以及飽和度和其他參數)值。例如。如果當前亮度爲200並且ins增加100,則亮度值變爲255(不是300),因爲255是最大值。如果當前亮度(現在255)減少100,則它變爲255 - 100 = 155,並且不等於初始值(200)。爲了避免這種情況,您應該存儲初始位圖並且不會更改它,但通過postConcat()更改ColorMatrixColorFilter,並且每次對初始位圖應用顏色過濾器,但不適用於已修改的位圖。

所以,你應該爲ColorMatrix創造全球價值和改變它在你的changeBrightness()adjustSaturation()方法,然後總是把它應用到初始位圖。

相關問題