图片怀旧特效处理

1、怀旧特效算法:

r = 0.393r +0.769g+0.189b
g = 0.349r + 0.686g +0.168b
b = 0.272r +0.534g +0.131b

2、算法的一般实现:

 public Bitmap render(Bitmap bitmap)
  {
    if(bitmap == null)return null;

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels ,0 , width , 0 , 0 , width , height);
    for(int i=0 ; i<height ; i++)
    {
      for(int j=0 ; j<width ; j++)
      {
        int pixel = pixels[i*width +j];
        int r = (pixel & 0x00ff0000)>>16;
        int g = (pixel & 0x0000ff00)>>8;
        int b = (pixel & 0x000000ff);

        int newR =(int)( 0.393*r +0.769*g+0.189*b);
        int newG =(int)( 0.349*r + 0.686*g +0.168*b);
        int newB =(int)( 0.272*r +0.534*g +0.131*b);

        newR = newR>255 ? 255:newR;
        newG = newG>255 ? 255:newG;
        newB = newB>255 ? 255:newB;
        pixels[i*width +j] = (pixel & 0xff000000) + (newR<<16)+ (newG<<8) +newB;
      }
    }

3、采用ColorMatrix 实现

 public Bitmap renderColorMatrix(Bitmap bitmap)
  {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Bitmap buffer = Bitmap.createBitmap(width , height , Config.ARGB_8888);
    Canvas canvas = new Canvas(buffer);
    Paint paint = new Paint();
    ColorMatrix matrix = new ColorMatrix();
    float[] array = new float[]{
    0.393f,0.769f,0.189f,0,0,
    0.349f,0.686f,0.168f,0,0,
    0.272f,0.534f,0.131f,0,0,
    0,0,0,1,0
    };
    matrix.set(array);
    paint.setColorFilter(new ColorMatrixColorFilter(matrix));
    canvas.drawBitmap(bitmap , 0,0,paint);
    return buffer;
  }
posted @ 2012-09-20 16:08  lipeil  阅读(388)  评论(0编辑  收藏  举报