颜色道奇混合位图

时间:2012-03-23 15:21:42

标签: java android

我有两个位图topBitmap和bottomBitmap,我需要在android中使用颜色减淡来混合两个位图。我在PorterDuffXfermode中找不到颜色闪避。有没有办法在没有使用Canvas的情况下使用ot?

请告诉我如何在android.Thanks中使用颜色躲闪模式混合两个位图。

2 个答案:

答案 0 :(得分: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 :(得分: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)))));
}