在 Android 中将位图转换为棕褐色

发布于 2024-10-01 03:21:04 字数 73 浏览 0 评论 0 原文

有什么方法可以将位图转换为棕褐色吗? 我知道转换为灰度就是在ColorMatrix中设置setSaturation。 但是棕褐色呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

原来分手还会想你 2024-10-08 03:21:04

如果您有图像实例,则可以使用 ColorMartix 以棕褐色绘制它。让我描述一下如何使用 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二进制文件进行测试,无需编译。

在此处输入图像描述

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

清音悠歌 2024-10-08 03:21:04

我知道答案,但也许如果有人有其他更好的解决方案..

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

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;
}
⊕婉儿 2024-10-08 03:21:04

我改进了OP的答案。与 ColorMatrix 方法< /a>,但会产生更好的棕色色调。 (在我看来)

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文