black And White Effect Bitmap - Android Graphics

Android examples for Graphics:Bitmap Effect

Description

black And White Effect Bitmap

Demo Code


//package com.java2s;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;

public class Main {

    public static Bitmap blackAndWhiteEffect(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Bitmap base = Bitmap/* ww  w  .  ja v  a2s. co m*/
                .createBitmap(width, height, bitmap.getConfig());

        Canvas canvas = new Canvas(base);
        canvas.drawBitmap(bitmap, new Matrix(), new Paint());

        for (int i = 0; i < width; i++)//
        {
            for (int j = 0; j < height; j++) {
                int color = bitmap.getPixel(i, j);

                int r = Color.red(color);
                int g = Color.green(color);
                int b = Color.blue(color);
                int a = Color.alpha(color);

                int avg = (r + g + b) / 3;//RGB

                if (avg >= 100)//100
                {
                    base.setPixel(i, j, Color.argb(a, 255, 255, 255));//     
                } else {
                    base.setPixel(i, j, Color.argb(a, 0, 0, 0));// 
                }
            }
        }
        return base;
    }
}

Related Tutorials