JAVA Android:在位图上使黑色字母/文本变暗

时间:2015-08-19 17:19:28

标签: java android

我想让位图上的所有黑色文本变暗以过滤位图,经过研究后我发现了这个:

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

我目前的挑战是将其应用于位图以使黑色文本变暗。 请帮助我。

1 个答案:

答案 0 :(得分:2)

我能够找到我的问题的答案 https://stackoverflow.com/a/17887577/5220210http://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;
}

希望它有所帮助。