1

我正在开发一个应用程序,用户可以在其中更改图像亮度、对比度、饱和度等。我可以使用下面的代码分别完成所有这些操作。

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

问题是:在增加或减少图像的亮度和增加或减少饱和度值后,图像颜色会完全改变。

我必须实现这一点,如果我反转到亮度和饱和度的搜索栏的初始值,我应该在第一次获得实际图像。

例如,如果将亮度值从 0 增加到 100,然后将饱和度值从 0 更改为 80。然后我将亮度和饱和度值反转为 0。然后我应该得到第一次的原始图像。

但是现在图像颜色在反转其值时不会反转。请帮忙。提前谢谢。

4

1 回答 1

0

这是因为最大和最小亮度(以及饱和度和其他参数)值。例如,如果当前亮度为 200 并且 ins 增加 100,则亮度值变为 255(不是 300),因为 255 是最大值。如果当前亮度(现在为 255)减少 100,则变为 255 - 100 = 155,并且变得不等于初始值(200)。为避免这种情况,您应该存储初始位图并且不更改它,而是每次更改并为初始位图应用颜色过滤器,而不是修改后的位图ColorMatrixColorFilterpostConcat()

因此,您应该为您的和方法创建全局值ColorMatrix并对其进行更改,然后始终将其应用于初始位图。changeBrightness()adjustSaturation()

于 2017-09-15T07:26:24.217 回答