1

请帮我对位图图像数据执行图像效果。

我搜索以下代码以应用照片效果。但我不知道究竟应该传递什么值才能生效。

代码是..

 public Bitmap createEffect(Bitmap src, int depth, double red, double green, double blue) {
    // image size
    int width = src.getWidth();
    int height = src.getHeight();
    // create output bitmap
    Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
    // constant grayscale
    final double GS_RED = 0.3;
    final double GS_GREEN = 0.59;
    final double GS_BLUE = 0.11;
    // color information
    int A, R, G, B;
    int pixel;

    // scan through all pixels
    for(int x = 0; x < width; ++x) {
        for(int y = 0; y < height; ++y) {
            // get pixel color
            pixel = src.getPixel(x, y);
            // get color on each channel
            A = Color.alpha(pixel);
            R = Color.red(pixel);
            G = Color.green(pixel);
            B = Color.blue(pixel);
            // apply grayscale sample
            B = G = R = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B);

            // apply intensity level for sepid-toning on each channel
            R += (depth * red);
            if(R > 255) { R = 255; }

            G += (depth * green);
            if(G > 255) { G = 255; }

            B += (depth * blue);
            if(B > 255) { B = 255; }

            // set new pixel color to output image
            bmOut.setPixel(x, y, Color.argb(A, R, G, B));
        }
    }

    // return final image
    return bmOut;
}

我想像这个应用程序一样工作。

有以下效果图

第一个效果第二种效应 第三效应 第四种效应 第六效应

4

1 回答 1

2

如果您阅读代码,您会看到 src 中的每个像素首先转换为灰度 [0-255] 通过使用灰度值作为基础并添加深度*颜色,此值将转换回颜色像素。

所以如果你想给一个位图一个绿色的色调,这样做:

Bitmap result = createEffect(src,50,0,1,0);

这将使位图更环保。

要反转颜色(如在上一个示例中),请使用此(未经测试)函数:

public Bitmap invert(Bitmap src) {
    // image size
    int width = src.getWidth();
    int height = src.getHeight();
    // create output bitmap
    Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
    // color information
    int A, R, G, B;
    int pixel;

    // scan through all pixels
    for(int x = 0; x < width; ++x) {
        for(int y = 0; y < height; ++y) {
            // get pixel color
            pixel = src.getPixel(x, y);
            // get color on each channel
            A = Color.alpha(pixel);
            R = Color.red(pixel);
            G = Color.green(pixel);
            B = Color.blue(pixel);
            // set new pixel color to output image
            bmOut.setPixel(x, y, Color.argb(A, 255-R, 255-G, 255-B));
        }
    }

    // return final image
    return bmOut;
}
于 2012-04-16T08:03:07.977 回答