handle Old Image Effect - Android Graphics

Android examples for Graphics:Bitmap Effect

Description

handle Old Image Effect

Demo Code


//package com.java2s;
import android.graphics.Bitmap;

import android.graphics.Color;

public class Main {

    public static Bitmap handleOldImgEffect(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        int oldPix[] = new int[width * height];
        int newPix[] = new int[width * height];

        Bitmap bmp = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_8888);

        bitmap.getPixels(oldPix, 0, width, 0, 0, width, height);

        for (int i = 0; i < width * height; i++) {
            int a, r, g, b;
            r = Color.red(oldPix[i]);
            g = Color.green(oldPix[i]);
            b = Color.blue(oldPix[i]);
            a = Color.alpha(oldPix[i]);

            r = (int) (0.393 * r + 0.769 * g + 0.189 * b);
            g = (int) (0.349 * r + 0.686 * g + 0.168 * b);
            b = (int) (0.272 * r + 0.534 * g + 0.131 * b);

            if (r > 255) {
                r = 255;/*from  ww w.  j a  va  2s .  c o m*/
            } else if (r < 0) {
                r = 0;
            }

            if (g > 255) {
                g = 255;
            } else if (g < 0) {
                g = 0;
            }

            if (b > 255) {
                b = 255;
            } else if (b < 0) {
                b = 0;
            }

            newPix[i] = Color.argb(a, r, g, b);
        }
        bmp.setPixels(newPix, 0, width, 0, 0, width, height);

        return bmp;
    }
}

Related Tutorials