2012-03-23 41 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))))); 
}