在安卓中将位图转换为深褐色

11
有没有办法将位图转换为棕褐色? 我知道将其转换为灰度是通过设置ColorMatrix中的setSaturation来实现的。 但是棕褐色怎么做呢?

@EpicPandaForce:您的要求是什么?您需要一种不同的算法吗? - aminography
@aminography 我想给这里某个已有的答案250声望,因为它解决了我今天遇到的问题;但在我能颁发该奖励之前仍需16小时。 - EpicPandaForce
哦,干得好,伙计 :-) - aminography
3个回答

21

如果你有一个图像实例,那么你可以使用ColorMatrix将其绘制成棕褐色。让我描述一下如何使用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);
}

示例项目已从Bitbucket迁移到GitHub。请查看发布版本以下载APK二进制文件进行测试,无需编译。

输入图像描述


我该如何获取更多的ColorMatric效果,比如霓虹、Indiglow、水绿、鲜艳等等?期待相关链接/提示/指南... - Abdul Wahab
你需要找到相应的颜色矩阵值来获得这些效果。很抱歉,我不知道任何可以找到它的资源。如果你找到了有用的东西,请在这里发布。谢谢。 - rude
2
我在ColorMatrix中找到了这个,并为我所需的颜色效果设置了类似的值。 http://docs.rainmeter.net/tips/colormatrix-guide - Abdul Wahab
其实很简单,只需根据颜色的RGB值找到相应的数值,就可以得到任何想要的颜色。一旦饱和度设置为0,它将把像素的亮度与新颜色结合起来,然后就可以正常工作了。虽然我正在回答一个6年前的问题... :D - EpicPandaForce

5

我知道答案,但如果有其他更好的解决方案,欢迎分享。

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

1
当图像像素更多时,处理时间会更长,是否还有其他方法? - blackjack
这个方法在透明像素上失败了。 - Rab Ross

1
我对原帖作者的回答进行了改进。与ColorMatrix方法相比,这个运行速度很快,但产生了更好的棕色调。(在我看来)
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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接