1

I want to darken all the black text on a bitmap to filter the the bitmap and after research I found this:

private static void setContrast(ColorMatrix cm, float contrast) {
                float scale = contrast + 1.f;
                   float translate = (-.5f * scale + .5f) * 255.f;
                cm.set(new float[] {
                       scale, 0, 0, 0, translate,
                       0, scale, 0, 0, translate,
                       0, 0, scale, 0, translate,
                       0, 0, 0, 1, 0 });
        }

My present challenge is applying it on the bitmap to darken the black texts. Kindly assist me.

Andrea Robinson
  • 225
  • 4
  • 23

1 Answers1

3

I was able to find an answer to my question using https://stackoverflow.com/a/17887577/5220210 and http://android.okhelp.cz/bitmap-set-contrast-and-brightness-android/

public static Bitmap darkenText(Bitmap bmp,  float contrast)
{
    ColorMatrix cm = new ColorMatrix();
     float scale = contrast + 1.f;
     float translate = (-.5f * scale + .5f) * 255.f;
  cm.set(new float[] {
         scale, 0, 0, 0, translate,
         0, scale, 0, 0, translate,
         0, 0, scale, 0, translate,
         0, 0, 0, 1, 0 });

    Bitmap ret = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());

    Canvas canvas = new Canvas(ret);

    Paint paint = new Paint();
    paint.setColorFilter(new ColorMatrixColorFilter(cm));
    canvas.drawBitmap(bmp, 0, 0, paint);

    return ret;
}

Hope it helps someone.

Community
  • 1
  • 1
Andrea Robinson
  • 225
  • 4
  • 23