2012-03-23 56 views
1

我有兩個位圖topBitmap和bottomBitmap,我需要在android中使用顏色減淡來混合兩個位圖。在PorterDuffXfermode中找不到顏色閃避。沒有使用Canvas的方法嗎?顏色減淡混合位圖

請讓我知道如何在android.Thanks中預先使用顏色閃避模式混合兩個位圖。

回答

3

嗨我發現這個方法從SO帖子的某個地方。這個方法有兩個圖像,並使用顏色創建一個圖像躲閃

public Bitmap ColorDodgeBlend(Bitmap source, Bitmap layer) { 
      Bitmap base = source.copy(Config.ARGB_8888, true); 
      Bitmap blend = layer.copy(Config.ARGB_8888, false); 

      IntBuffer buffBase = IntBuffer.allocate(base.getWidth() * base.getHeight()); 
      base.copyPixelsToBuffer(buffBase); 
      buffBase.rewind(); 

      IntBuffer buffBlend = IntBuffer.allocate(blend.getWidth() * blend.getHeight()); 
      blend.copyPixelsToBuffer(buffBlend); 
      buffBlend.rewind(); 

      IntBuffer buffOut = IntBuffer.allocate(base.getWidth() * base.getHeight()); 
      buffOut.rewind(); 

      while (buffOut.position() < buffOut.limit()) { 

       int filterInt = buffBlend.get(); 
       int srcInt = buffBase.get(); 

       int redValueFilter = Color.red(filterInt); 
       int greenValueFilter = Color.green(filterInt); 
       int blueValueFilter = Color.blue(filterInt); 

       int redValueSrc = Color.red(srcInt); 
       int greenValueSrc = Color.green(srcInt); 
       int blueValueSrc = Color.blue(srcInt); 

       int redValueFinal = colordodge(redValueFilter, redValueSrc); 
       int greenValueFinal = colordodge(greenValueFilter, greenValueSrc); 
       int blueValueFinal = colordodge(blueValueFilter, blueValueSrc); 


       int pixel = Color.argb(255, redValueFinal, greenValueFinal, blueValueFinal); 


       buffOut.put(pixel); 
      } 

      buffOut.rewind(); 

      base.copyPixelsFromBuffer(buffOut); 
      blend.recycle(); 

      return base; 
     } 

這裏的方法做,使色彩躲閃像素獲得的效果

private int colordodge(int in1, int in2) { 
      float image = (float)in2; 
      float mask = (float)in1; 
      return ((int) ((image == 255) ? image:Math.min(255, (((long)mask << 8)/(255 - image))))); 
     } 

我想創建原始的草圖圖像照片,使用這種方法我能夠創建彩色全格式的圖像卡通化,但我需要在黑色和白色的鉛筆素描格式。如果您有任何想法,請分享。

希望這種方法是將這個代碼鉛筆素描對你有用

1

,這個工作對我來說

public Bitmap Changetosketch(Bitmap bmp) { 
    Bitmap Copy, Invert, Result; 
    Copy = toGrayscale(bmp); 
    Invert = createInvertedBitmap(Copy); 
    Invert = Blur.blur(this, Invert); 
    Result = ColorDodgeBlend(Invert, Copy); 
    return Result; 
} 
public static Bitmap toGrayscale(Bitmap src) 
{ 
    final double GS_RED = 0.299; 
    final double GS_GREEN = 0.587; 
    final double GS_BLUE = 0.114; 

    // create output bitmap 
    Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig()); 
    // pixel information 
    int A, R, G, B; 
    int pixel; 

    // get image size 
    int width = src.getWidth(); 
    int height = src.getHeight(); 

    // scan through every single pixel 
    for (int x = 0; x < width; ++x) { 
     for (int y = 0; y < height; ++y) { 
      // get one pixel color 
      pixel = src.getPixel(x, y); 
      // retrieve color of all channels 
      A = Color.alpha(pixel); 
      R = Color.red(pixel); 
      G = Color.green(pixel); 
      B = Color.blue(pixel); 
      // take conversion up to one single value 
      R = G = B = (int) (GS_RED * R + GS_GREEN * G + GS_BLUE * B); 
      // set new pixel color to output bitmap 
      bmOut.setPixel(x, y, Color.argb(A, R, G, B)); 
     } 
    } 

    // return final image 
    return bmOut; 
} 

public static Bitmap createInvertedBitmap(Bitmap src) { 
    Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig()); 
    // color info 
    int A, R, G, B; 
    int pixelColor; 
    // image size 
    int height = src.getHeight(); 
    int width = src.getWidth(); 

    // scan through every pixel 
    for (int y = 0; y < height; y++) { 
     for (int x = 0; x < width; x++) { 
      // get one pixel 
      pixelColor = src.getPixel(x, y); 
      // saving alpha channel 
      A = Color.alpha(pixelColor); 
      // inverting byte for each R/G/B channel 
      R = 255 - Color.red(pixelColor); 
      G = 255 - Color.green(pixelColor); 
      B = 255 - Color.blue(pixelColor); 
      // set newly-inverted pixel to output image 
      bmOut.setPixel(x, y, Color.argb(A, R, G, B)); 
     } 
    } 
    return bmOut; 
} 
public Bitmap ColorDodgeBlend(Bitmap source, Bitmap layer) { 
    Bitmap base = source.copy(Bitmap.Config.ARGB_8888, true); 
    Bitmap blend = layer.copy(Bitmap.Config.ARGB_8888, false); 

    IntBuffer buffBase = IntBuffer.allocate(base.getWidth() * base.getHeight()); 
    base.copyPixelsToBuffer(buffBase); 
    buffBase.rewind(); 

    IntBuffer buffBlend = IntBuffer.allocate(blend.getWidth() * blend.getHeight()); 
    blend.copyPixelsToBuffer(buffBlend); 
    buffBlend.rewind(); 

    IntBuffer buffOut = IntBuffer.allocate(base.getWidth() * base.getHeight()); 
    buffOut.rewind(); 

    while (buffOut.position() < buffOut.limit()) { 

     int filterInt = buffBlend.get(); 
     int srcInt = buffBase.get(); 

     int redValueFilter = Color.red(filterInt); 
     int greenValueFilter = Color.green(filterInt); 
     int blueValueFilter = Color.blue(filterInt); 

     int redValueSrc = Color.red(srcInt); 
     int greenValueSrc = Color.green(srcInt); 
     int blueValueSrc = Color.blue(srcInt); 

     int redValueFinal = colordodge(redValueFilter, redValueSrc); 
     int greenValueFinal = colordodge(greenValueFilter, greenValueSrc); 
     int blueValueFinal = colordodge(blueValueFilter, blueValueSrc); 


     int pixel = Color.argb(255, redValueFinal, greenValueFinal, blueValueFinal); 


     buffOut.put(pixel); 
    } 

    buffOut.rewind(); 

    base.copyPixelsFromBuffer(buffOut); 
    blend.recycle(); 

    return base; 
} 

private int colordodge(int in1, int in2) { 
    float image = (float)in2; 
    float mask = (float)in1; 
    return ((int) ((image == 255) ? image:Math.min(255, (((long)mask << 8)/(255 - image))))); 
}