How to manipulate pixels in a Bitmap?
<p>I am developing an Instagram like application for learning image processing and android. But I am stuck, I have a problem implementing Grayscale Filter in my application. I am trying a simple approach for now to convert individual pixels in a Bitmap to Grayscale.</p>

<p>Here's the whole class I am writing to apply various filters to an Image:</p>

<pre><code>package com.dosa2.photoeditor.ImageEffects;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;

public class ImageEffects {

    Bitmap bitmap;
    int width, height;

    public ImageEffects(Bitmap bitmap) {
        this.bitmap = bitmap;
        width = bitmap.getWidth();
        height = bitmap.getHeight();
    }

    public Bitmap toGrayscale() {

        Bitmap resultBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(resultBitmap);
        Paint p = new Paint();
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0);
        ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
        p.setColorFilter(f);
        c.drawBitmap(bitmap, 0, 0, p);

        return resultBitmap;
    }

    public Bitmap toGrayscale2() {
        Bitmap resultBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        for(int i=0;i&lt;height;i++) {
            for (int j=0;i&lt;width;j++) {
                int c = bitmap.getPixel(i,j);
                resultBitmap.setPixel(i, j, (Color.red(c)+Color.blue(c)+Color.green(c)/3));
            }
        }

        return resultBitmap;
    }
}
</code></pre>

<p>I have tried 2 methods to convert the Bitmap into Grayscale. The former seems to be working(but I am not able to understand it) and the latter is not. 
Can anyone help me out? And do mention if there's an easier way to manipulate Images in Android.</p>