Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert bitmap to sepia in android

Is there any way to convert a Bitmap to sepia? I know to convert to grayScale is to set the setSaturation in ColorMatrix. But what about Sepia?

like image 576
user430926 Avatar asked Nov 10 '10 04:11

user430926


2 Answers

If you have instance of image then you can use ColorMartix to draw it in Sepia. Let me describe way how you can do this using Drawable.

public static void setSepiaColorFilter(Drawable drawable) {
  if (drawable == null)
    return;

  final ColorMatrix matrixA = new ColorMatrix();
  // making image B&W
  matrixA.setSaturation(0);

  final ColorMatrix matrixB = new ColorMatrix();
  // applying scales for RGB color values
  matrixB.setScale(1f, .95f, .82f, 1.0f);
  matrixA.setConcat(matrixB, matrixA);

  final ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrixA);
  drawable.setColorFilter(filter);
}

Sample project was moved from Bitbucket to GitHub. Please check Release section to download APK binary to test without compiling.

enter image description here

like image 124
rude Avatar answered Sep 22 '22 15:09

rude


I know the answer, but maybe if some have other better solution..

public Bitmap toSephia(Bitmap bmpOriginal)
{        
    int width, height, r,g, b, c, gry;
    height = bmpOriginal.getHeight();
    width = bmpOriginal.getWidth();
    int depth = 20;

    Bitmap bmpSephia = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bmpSephia);
    Paint paint = new Paint();
    ColorMatrix cm = new ColorMatrix();
    cm.setScale(.3f, .3f, .3f, 1.0f);   
    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(f);
    canvas.drawBitmap(bmpOriginal, 0, 0, paint);
    for(int x=0; x < width; x++) {
        for(int y=0; y < height; y++) {
            c = bmpOriginal.getPixel(x, y);

            r = Color.red(c);
            g = Color.green(c);
            b = Color.blue(c);

            gry = (r + g + b) / 3;
            r = g = b = gry;

            r = r + (depth * 2);
            g = g + depth;

            if(r > 255) {
              r = 255;
            }
            if(g > 255) {
              g = 255;
            }
            bmpSephia.setPixel(x, y, Color.rgb(r, g, b));
        }
    }      
    return bmpSephia;
}
like image 22
user430926 Avatar answered Sep 20 '22 15:09

user430926