开发者

Setting Saturation and ColorFilter to image simultaneously

开发者 https://www.devze.com 2023-04-04 14:17 出处:网络
The following function changes contrast and brightness in the picture successfully. Bitmap bmp; ImageView alteredImageView;

The following function changes contrast and brightness in the picture successfully.

Bitmap bmp;
ImageView alteredImageView;
...
public void drawAlteredImage(float contr,float bright) {
    Bitmap alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
    Canvas canvas = new Canvas(alteredBitmap);
    Paint paint = new Paint();
    ColorMatrix cm = new ColorMatrix();

    cm.set(new float[] { 
            contr, 0, 0, 0, bright, 
            0, contr, 0, 0, bright, 
            0, 0, contr, 0, bright, 
            0, 0, 0, 1, 0 });

    paint.setColorFilter(new ColorMatrixColorFilter(cm));
    Matrix matrix = new Matrix();
    canvas.drawBitmap(bmp, matrix, paint);
    alteredImageView.setImageBitmap(alteredBitmap);
}

But when I added setSaturation method to ColorMatrix the contrast and brightness altering ceased to work. The code:

p开发者_JS百科ublic void drawAlteredImage(float contr,float bright,float satur) {
    Bitmap alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
    Canvas canvas = new Canvas(alteredBitmap);
    Paint paint = new Paint();
    ColorMatrix cm = new ColorMatrix();

    cm.set(new float[] { 
            contr, 0, 0, 0, bright, 
            0, contr, 0, 0, bright, 
            0, 0, contr, 0, bright, 
            0, 0, 0, 1, 0 });
    cm.setSaturation(satur);
    paint.setColorFilter(new ColorMatrixColorFilter(cm));
    Matrix matrix = new Matrix();
    canvas.drawBitmap(bmp, matrix, paint);
    alteredImageView.setImageBitmap(alteredBitmap);
}

Only the saturation effect is applying in this case. Why does this problem happening? How can I fix it?


The setSaturation(...) method replaces all the values in the matrix. You can either create a matrix that combines both operations, or do the operations with two separate drawBitmap(...) calls.

Try:

cm.setSaturation(satur);
final float m[] = cm.getArray();
final float c = contr;
cm.set(new float[] { 
        m[ 0] * c, m[ 1] * c, m[ 2] * c, m[ 3] * c, m[ 4] * c + bright, 
        m[ 5] * c, m[ 6] * c, m[ 7] * c, m[ 8] * c, m[ 9] * c + bright, 
        m[10] * c, m[11] * c, m[12] * c, m[13] * c, m[14] * c + bright, 
        m[15]    , m[16]    , m[17]    , m[18]    , m[19] }); 


Another option is to create a new ColorMatrix, set the saturation in this new ColorMatrix, then call postConcat on the Brightness/Contrast ColorMatrix. Something like:

 public void drawAlteredImage(float contr,float bright,float satur) {
        Bitmap alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
        Canvas canvas = new Canvas(alteredBitmap);
        Paint paint = new Paint();
        ColorMatrix cm = new ColorMatrix();

        cm.set(new float[] { 
            contr, 0, 0, 0, bright, 
            0, contr, 0, 0, bright, 
            0, 0, contr, 0, bright, 
            0, 0, 0, 1, 0 });

        ColorMatrix saturationCM = new ColorMatrix();
        saturationCM.setSaturation(satur);
        cm.postConcat(saturationCM);

        paint.setColorFilter(new ColorMatrixColorFilter(cm));
        Matrix matrix = new Matrix();
        canvas.drawBitmap(bmp, matrix, paint);
        alteredImageView.setImageBitmap(alteredBitmap);
}


If someone still looking for this , the easy way to change saturation, brightness, and contrast of an image is just use ImageFilterView which is the subclass of ImageView

source: https://developer.android.com/reference/kotlin/androidx/constraintlayout/utils/widget/ImageFilterView

example in kotlin:


val myImage = findViewById<ImageFilterView>(R.id.my_image)

myImage.setSaturation(1.2f)
myImage.setBrightness(1.5f)
myImage.setContrast(2f)

0

精彩评论

暂无评论...
验证码 换一张
取 消