以编程方式增强ImageView亮度

时间:2014-10-22 12:09:16

标签: android optimization android-imageview

我有一个Android应用程序,其中我使用下面的代码增加图像的亮度。但这非常慢,所以有人知道在android中增强imageview图像亮度的快速方法。请记住,这是提高图像视图亮度而不是屏幕亮度

 public static Bitmap doBrightness(Bitmap src, int value) {
    //Log.e("Brightness", "Changing brightnhjh");

    int width = src.getWidth();
    int height = src.getHeight();
    Bitmap bmout = Bitmap.createBitmap(width, height, src.getConfig());
    int A, R, G, B;
    int pixel;
    for (int i = 0; i < width; i=i++) {
        for (int j = 0; j < height; j=j++) {
            pixel = src.getPixel(i, j);
            A = Color.alpha(pixel);
            R = Color.red(pixel);
            G = Color.green(pixel);
            B = Color.blue(pixel);
            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;
            }
            bmout.setPixel(i, j, Color.argb(A, R, G, B));
        }
    }
    return bmout;

}

这是imageview

imageview.setImageBitmap(doBrightness(image, 40));

5 个答案:

答案 0 :(得分:6)

您可以使用以下代码来增强图像:

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

注意:
对比度:0到10 亮度:-255至255

答案 1 :(得分:3)

我已经解决了这个问题,我使图像更亮,然后在imageview中显示它。我使用的代码如下:

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

答案 2 :(得分:1)

我无法准确记住我从哪里得到这个但是我使用它(带负值以获得更深的东西,放一些正值以获得明亮的东西)

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

编辑:找到我从哪里获取此内容:Adjusting Lightness using ColorMatrix

答案 3 :(得分:0)

您可以在ImageView顶部放置半透明视图。方法:一个有两个孩子的FrameView,第一个是ImageView,第二个是半透明背景的View(只是View)。然后,您必须以编程方式修改背景不透明度。

答案 4 :(得分:0)

上述答案对我不起作用,因为我有一个使用位图设置图像的imageview。 这就是它的工作原理:

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

具有一个可控制亮度的搜索栏。其范围为-255至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);

            }
        });

希望对您有所帮助。