how to apply photo effect in Bitmap image in android

5.6k Views Asked by At

Please help me to perform the image effect on bitmap image data.

I search following code to apply photo effect. but i don't know about what exactly value should be pass for effect.

The code is ..

 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;
}

I want to work like this application.

There are following pic with effect

First effectsecond effect Third effect fourth effect sixth effect

1

There are 1 best solutions below

4
On BEST ANSWER

If you read the code you can see that every pixel in src gets first converted to grayscale [0-255] This value is converted back to a color pixel by using the grayscale value as a base and adding depth*color.

So if you want to give a bitmap a green tint do it like this:

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

this will make the bitmap greener.

To invert the colors (like in your last exmaple) use this (untested) function:

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;
}