11

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?

jball
  • 24,791
  • 9
  • 70
  • 92
user430926
  • 4,017
  • 13
  • 53
  • 77

3 Answers3

21

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

rude
  • 2,340
  • 2
  • 14
  • 19
  • how can I get ColorMatric for more effects, like neon, indiglow, aqua, vivid, etc, waiting for related link/hint/guide... – Abdul Wahab Dec 01 '12 at 16:47
  • You need find corresponding color matrix values to get these effect. Sorry but I don't know any resource where you can find it. Please post it here if you will find anything useful. Thanks – rude Dec 03 '12 at 22:06
  • 2
    I found this for ColorMatrix, and set simillar values for my required color effects. http://docs.rainmeter.net/tips/colormatrix-guide – Abdul Wahab Dec 05 '12 at 12:11
  • It's actually pretty easy, just find the values based on the color's RGB values and you'll get any color you want. Once the saturation is set to 0, it'll combine the brightness of a pixel with the new color, and it'll just work. Although I'm answering something that was asked 6 years ago... :D – EpicPandaForce Nov 16 '18 at 16:46
5

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;
}
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
user430926
  • 4,017
  • 13
  • 53
  • 77
1

I've improved on the OP's answer. This runs competitively fast when compared to the ColorMatrix method, but producing a nicer brown tone. (in my opinion)

public Bitmap toSepiaNice(Bitmap color) {
    int red, green, blue, pixel, gry;
    int height = color.getHeight();
    int width = color.getWidth();
    int depth = 20;

    Bitmap sepia = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    int[] pixels = new int[width * height];
    color.getPixels(pixels, 0, width, 0, 0, width, height);
    for (int i = 0; i < pixels.length; i++) {
        pixel = pixels[i];

        red = (pixel >> 16) & 0xFF;
        green = (pixel >> 8) & 0xFF;
        blue = pixel & 0xFF;

        red = green = blue = (red + green + blue) / 3;

        red += (depth * 2);
        green += depth;

        if (red > 255)
            red = 255;
        if (green > 255)
            green = 255;
        pixels[i] = (0xFF << 24) | (red << 16) | (green << 8) | blue;
    }
    sepia.setPixels(pixels, 0, width, 0, 0, width, height);
    return sepia;
}
Community
  • 1
  • 1
700 Software
  • 85,281
  • 83
  • 234
  • 341