Android a seekbar for adjusting bitmap brightness

久未见 提交于 2021-02-08 06:41:07

问题


I have tryed many codes, and looked at Android developers but couldn't make it work. I want the seek bar to adjust my bitmap image brightness


回答1:


check the example given at Increase/Decrease Brightness of Image

and do as shown below on seekbar progress changes

 int brightness;
 SeekBar seekbarbrightness=(SeekBar)findViewById(R.id.seekBar1);
 seekbarbrightness.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

         @Override
         public void onStopTrackingTouch(SeekBar arg0) {
          // TODO Auto-generated method stub

         }

         @Override
         public void onStartTrackingTouch(SeekBar arg0) {
          // TODO Auto-generated method stub

         }

         @Override
         public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {

             // TODO Auto-generated method stub

             brightness=progress;
             Bitmap bitmap=doBrightness(originalbitmap,brightness);
             imageView.setImageBitmap(bitmap);
         }
        });



回答2:


check the example given at Increase/Decrease Brightness of Image

     int brightness;
 SeekBar seekBarBrightness=(SeekBar)findViewById(R.id.seekBar1);
 seekBarBrightness.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

     @Override
     public void onStopTrackingTouch(SeekBar arg0) {


     }

     @Override
     public void onStartTrackingTouch(SeekBar arg0) {


     }

     @Override
     public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {

       Bitmap newBitMap  =   doBrightness(bitMap,progress);

       imageView.setImageBitmap(newBitMap);

     }
    });




    public static Bitmap doBrightness(Bitmap src, int value)
    {
        // image size
        int width = src.getWidth();
        int height = src.getHeight();
        // create output bitmap
        Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
        // color information
        int A, R, G, B;
        int pixel;

        // scan through all pixels
        for (int x = 0; x < width; ++x)
            {
                for (int y = 0; y < height; ++y)
                    {
                        // get pixel color
                        pixel = src.getPixel(x, y);
                        A = Color.alpha(pixel);
                        R = Color.red(pixel);
                        G = Color.green(pixel);
                        B = Color.blue(pixel);

                        // increase/decrease each channel
                        R += value;
                        if (R > 255)
                            {
                                R = 255;
                            } else if (R < 0)
                            {
                                R = 0;
                            }

                        G += value;
                        if (G > 255)
                            {
                                G = 255;
                            } else if (G < 0)
                            {
                                G = 0;
                            }

                        B += value;
                        if (B > 255)
                            {
                                B = 255;
                            } else if (B < 0)
                            {
                                B = 0;
                            }

                        // apply new pixel color to output bitmap
                        bmOut.setPixel(x, y, Color.argb(A, R, G, B));
                    }
            }

        // return final image
        return bmOut;
    }



回答3:


Use this example

SeekBar sb_value = (SeekBar) findViewById(R.id.sb_value);
        final ImageView im_brightness = (ImageView) findViewById(R.id.im_brightness);
        sb_value.setProgress(125);
        sb_value.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                im_brightness.setColorFilter(setBrightness(progress));

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

setBrightness method:

public static PorterDuffColorFilter setBrightness(int progress) {
        if (progress >=    100)
        {
            int value = (int) (progress-100) * 255 / 100;

            return new PorterDuffColorFilter(Color.argb(value, 255, 255, 255), PorterDuff.Mode.SRC_OVER);

        }
        else
        {
            int value = (int) (100-progress) * 255 / 100;
            return new PorterDuffColorFilter(Color.argb(value, 0, 0, 0), PorterDuff.Mode.SRC_ATOP);


        }
    }


来源:https://stackoverflow.com/questions/11663011/android-a-seekbar-for-adjusting-bitmap-brightness

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!