2010-11-10 58 views

回答

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 2013-05-06 08:47:56

+1

查看此主要https://gist.github.com/pmcfernandes/8261130 – pedrofernandes 2014-01-04 21:49:03

+0

此方法在透明像素上失败 – 2015-03-11 10:33:04

18

如果您有图像的情况下,那么你可以使用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。请检查Release section下载APK二进制文件,无需编译即可测试。

enter image description here

+0

如何获得ColorMatric以获得更多效果,如霓虹灯,indiglow,aqua,vivid等, 等待相关链接/提示/指导... – 2012-12-01 16:47:42

+0

你需要找到相应的颜色矩阵值来获得这些效果。对不起,但我不知道任何资源,你可以找到它。如果您发现任何有用的信息,请在此发布。谢谢 – rude 2012-12-03 22:06:16

+2

我发现这个ColorMatrix,并为我需要的颜色效果设置simillar值。 http://docs.rainmeter.net/tips/colormatrix-guide – 2012-12-05 12:11:28

0

我上the OP's answer提高。与the ColorMatrix method相比,这款产品运行速度快,但产生更好的棕色色调。 (在我看来)

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