Enhancing ImageView brightness programmatically

前端 未结 5 1953
不知归路
不知归路 2021-01-03 10:56

I have an android app in which I am increasing brightness of image with the code below. But this is very slow so does anyone knows a fast way to enhance image brightness of

相关标签:
5条回答
  • 2021-01-03 11:08

    I have got a work around for this issue i made the image brighter and then shown it in an imageview. The code i used is given below:

    foto.setColorFilter(brightIt(100));//foto is my ImageView
    //and below is the brightIt func
    public static ColorMatrixColorFilter brightIt(int fb) {
    ColorMatrix cmB = new ColorMatrix();
    cmB.set(new float[] { 
        1, 0, 0, 0, fb,
        0, 1, 0, 0, fb,
        0, 0, 1, 0, fb,
        0, 0, 0, 1, 0   });
    
    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.set(cmB);
    //Canvas c = new Canvas(b2);
    //Paint paint = new Paint();
    ColorMatrixColorFilter f = new ColorMatrixColorFilter(colorMatrix);
    //paint.setColorFilter(f);   
    return f;
    }
    
    0 讨论(0)
  • 2021-01-03 11:19

    You can place a semi-transparent view on top of your ImageView. How: a FrameView with two children, the 1st one is the ImageView and the 2nd is a View (just View) with a semi-transparent background. Then you will have to modify the background opacity programmatically.

    0 讨论(0)
  • 2021-01-03 11:19

    The above answers didnt work for me as I had an imageview with image set using bitmap. So this is what it worked:

        private ColorMatrixColorFilter brightness(float value) {
            ColorMatrix cmB = new ColorMatrix();
            cmB.set(new float[]{
                    1, 0, 0, 0, value,
                    0, 1, 0, 0, value,
                    0, 0, 1, 0, value,
                    0, 0, 0, 1, 0});
            return new ColorMatrixColorFilter(cmB);
        }
    

    Have a seek bar with controls the brightness.Its range is from -255 to 255.

     sbBrightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    if(fromUser){
                        photoFilterView.getSource().setColorFilter(brightness(progress));
    
                   /*     Filter brightness=new Filter();
                        brightness.addSubFilter(new BrightnessSubFilter(progress));
    
                        Bitmap ouputImage = brightness.processFilter(bm);
                        photoFilterView.getSource().setImageBitmap(ouputImage);*/
                    }
                }
    
                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
    
                }
    
                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            sbBrightness.setVisibility(View.GONE);
                        }
                    },1000);
    
                }
            });
    

    Hope it helps some.

    0 讨论(0)
  • 2021-01-03 11:26

    I can't exactly remember where i got this from but i use this (with negative value to get something darker, put some positive value to get something bright)

    drawable.setColorFilter(applyLightness(-30));
    
    
    public static PorterDuffColorFilter applyLightness(int progress)
    {
        if (progress > 0)
        {
            int value = (int) progress * 255 / 100;
            return new PorterDuffColorFilter(Color.argb(value, 255, 255, 255), Mode.SRC_OVER);
        }
        else
        {
            int value = (int) (progress * -1) * 255 / 100;
            return new PorterDuffColorFilter(Color.argb(value, 0, 0, 0), Mode.SRC_ATOP);
        }
    }
    

    edit : found where i took this from : Adjusting Lightness using ColorMatrix

    0 讨论(0)
  • 2021-01-03 11:27

    You can use the following code to enhance the image :

    public static Bitmap enhanceImage(Bitmap mBitmap, 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 mEnhancedBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), mBitmap
            .getConfig());
        Canvas canvas = new Canvas(mEnhancedBitmap);
        Paint paint = new Paint();
        paint.setColorFilter(new ColorMatrixColorFilter(cm));
        canvas.drawBitmap(mBitmap, 0, 0, paint);
        return mEnhancedBitmap;
    }
    

    note :
    contrast : 0 to 10 brightness : -255 to 255

    0 讨论(0)
提交回复
热议问题