Example usage for android.graphics ColorMatrix ColorMatrix

List of usage examples for android.graphics ColorMatrix ColorMatrix

Introduction

In this page you can find the example usage for android.graphics ColorMatrix ColorMatrix.

Prototype

public ColorMatrix() 

Source Link

Document

Create a new colormatrix initialized to identity (as if reset() had been called).

Usage

From source file:Main.java

public static Bitmap memoriesImage(Bitmap bm) {
    Bitmap bitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();

    float[] matrixs = new float[] { 0.393f, 0.769f, 0.189f, 0, 0, 0.349f, 0.686f, 0.168f, 0, 0, 0.272f, 0.534f,
            0.134f, 0, 0, 0, 0, 0, 1, 0 };

    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.set(matrixs);//from   w w w  .j  a v  a  2  s . c om
    paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));

    canvas.drawBitmap(bm, 0, 0, paint);

    return bitmap;
}

From source file:Main.java

public static Bitmap highSaturationImage(Bitmap bm) {
    Bitmap bitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();

    float[] matrixs = new float[] { 1.438f, -0.122f, -0.016f, 0, -0.03f, -0.062f, 1.378f, -0.016f, 0, 0.05f,
            -0.062f, -0.122f, 1.438f, 0, -0.02f, 0, 0, 0, 1, 0 };

    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.set(matrixs);// w w  w  .ja va  2s. c o  m
    paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));

    canvas.drawBitmap(bm, 0, 0, paint);

    return bitmap;
}

From source file:Main.java

public static Drawable toGreyDrawable(Drawable drawable) {
    int w = drawable.getMinimumWidth();
    int h = drawable.getMinimumHeight();
    if (w <= 0 || h <= 0) {
        return drawable;
    }/*from w w  w . j  a va  2s.  c om*/
    Rect bounds = drawable.getBounds();
    Bitmap grey = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(grey);
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    drawable.setColorFilter(new ColorMatrixColorFilter(cm));
    drawable.setBounds(0, 0, w, h);
    drawable.draw(c);
    drawable.clearColorFilter();
    drawable.setBounds(bounds);
    BitmapDrawable bd = new BitmapDrawable(grey);
    bd.setBounds(0, 0, w, h);
    return bd;
}

From source file:Main.java

/**
 * Method to remove color in a Bitmap, creating a gray scale image.
 *
 * @param bmpOriginal The original Bitmap.
 * @return The gray scale Bitmap./*ww w .  j a  v a  2s  . com*/
 */
public static Bitmap toGrayscale(Bitmap bmpOriginal) {
    Bitmap bmpGrayscale = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(),
            Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bmpGrayscale);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    ColorMatrixColorFilter colorMatrixColorFilter = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(colorMatrixColorFilter);
    canvas.drawBitmap(bmpOriginal, 0, 0, paint);

    return bmpGrayscale;
}

From source file:Main.java

public static ColorMatrixColorFilter createCcf(int r, int g, int b) {
    ColorMatrix cm = new ColorMatrix();
    cm.set(new float[] { 1, 0, 0, 0, r, 0, 1, 0, 0, g, 0, 0, 1, 0, b, 0, 0, 0, 1, 0 }); // last line is antialias
    return new ColorMatrixColorFilter(cm);
}

From source file:Main.java

public static Bitmap toGrayscale(Bitmap bmpOriginal) {
    int width, height;
    height = bmpOriginal.getHeight();/*from w  w w  . java2  s  . com*/
    width = bmpOriginal.getWidth();

    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    Canvas c = new Canvas(bmpGrayscale);
    Paint paint = new Paint();
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(f);
    c.drawBitmap(bmpOriginal, 0, 0, paint);
    return bmpGrayscale;
}

From source file:Main.java

/**
 * Convert bitmap to the grayscale/*www  .  ja  v a2 s.  co m*/
 * http://androidsnippets.com/convert-bitmap-to-grayscale
 *
 * @param bmpOriginal Original bitmap
 * @return Grayscale bitmap
 */
public static Bitmap toGrayscale(Bitmap bmpOriginal) {
    final int height = bmpOriginal.getHeight();
    final int width = bmpOriginal.getWidth();

    final Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    final Canvas c = new Canvas(bmpGrayscale);
    final Paint paint = new Paint();
    final ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    final ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(f);
    c.drawBitmap(bmpOriginal, 0, 0, paint);
    return bmpGrayscale;
}

From source file:Main.java

static public void setImageColor(ImageView view, Bitmap sourceBitmap, int rgbcolor)// ,Bitmap sourceBitmap)
{
    if (sourceBitmap != null) {
        float R = Color.red(rgbcolor);
        float G = Color.green(rgbcolor);
        float B = Color.blue(rgbcolor);

        Log.v("R:G:B", R + ":" + G + ":" + B); //

        float[] colorTransform = { R / 255f, 0, 0, 0, 0, // R color
                0, G / 255f, 0, 0, 0 // G color
                , 0, 0, B / 255f, 0, 0 // B color
                , 0, 0, 0, 1f, 0f };//from   ww w.  j av  a 2s  . c o m

        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setSaturation(0f); // Remove Colour

        colorMatrix.set(colorTransform);
        ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
        Paint paint = new Paint();
        paint.setColorFilter(colorFilter);

        Bitmap mutableBitmap = sourceBitmap.copy(Bitmap.Config.ARGB_8888, true);
        view.setImageBitmap(mutableBitmap);
        Canvas canvas = new Canvas(mutableBitmap);
        canvas.drawBitmap(mutableBitmap, 0, 0, paint);

    }
}

From source file:Main.java

public static Bitmap setSaturation(Bitmap srcBitmap, float sat) {
    Bitmap bitmap = Bitmap.createBitmap(srcBitmap);
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(sat);/*  ww w. jav a  2 s .  c o  m*/
    Paint paint = new Paint();
    paint.setColorFilter(new ColorMatrixColorFilter(cm));
    Canvas canvas = new Canvas(bitmap);
    canvas.drawBitmap(srcBitmap, 0, 0, paint);
    return bitmap;
}

From source file:Main.java

public static Bitmap toGrayscale(Bitmap bmpOriginal) {
    int width, height;
    height = bmpOriginal.getHeight();/*from w w w  .j av  a2  s. c om*/
    width = bmpOriginal.getWidth();
    try {
        Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bmpGrayscale);
        Paint paint = new Paint();
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0);
        ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
        paint.setColorFilter(f);
        c.drawBitmap(bmpOriginal, 0, 0, paint);
        return bmpGrayscale;
    } catch (OutOfMemoryError e) {
        return bmpOriginal;
    }
}