Example usage for android.graphics Bitmap getPixels

List of usage examples for android.graphics Bitmap getPixels

Introduction

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

Prototype

public void getPixels(@ColorInt int[] pixels, int offset, int stride, int x, int y, int width, int height) 

Source Link

Document

Returns in pixels[] a copy of the data in the bitmap.

Usage

From source file:Main.java

/**
 * Fills the bitmap's pixels of the specified Color to transparency.
 * /*from ww  w.j a v  a 2 s  .  c om*/
 * @param aBitmap bitmap to process
 * @param aColor color to fill
 * @return bmp
 */
@SuppressLint("NewApi")
public static Bitmap eraseBG(Bitmap aBitmap, int aColor) {
    int width = aBitmap.getWidth();
    int height = aBitmap.getHeight();
    Bitmap b = aBitmap.copy(Config.ARGB_8888, true);
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB_MR1) {
        b.setHasAlpha(true);
    }

    int[] pixels = new int[width * height];
    aBitmap.getPixels(pixels, 0, width, 0, 0, width, height);

    for (int i = 0; i < width * height; i++) {
        if (pixels[i] == aColor) {
            pixels[i] = 0;
        }
    }

    b.setPixels(pixels, 0, width, 0, 0, width, height);

    return b;
}

From source file:Main.java

/**
 * Apply the given tint color to the transformation map.
 * @param transformationMap A bitmap representing the transformation map.
 * @param tintColor Tint color to be applied.
 * @return A bitmap with the with the tint color set.
 *///from   w w w  .  j  a  v a 2  s  . c o m
public static Bitmap processTintTransformationMap(Bitmap transformationMap, int tintColor) {
    // tint color
    int[] t = new int[] { Color.red(tintColor), Color.green(tintColor), Color.blue(tintColor) };

    int width = transformationMap.getWidth();
    int height = transformationMap.getHeight();
    int[] pixels = new int[width * height];
    transformationMap.getPixels(pixels, 0, width, 0, 0, width, height);

    float[] transformation = new float[2];

    for (int i = 0; i < pixels.length; i++) {
        int color = pixels[i];
        int alpha = Color.alpha(color);
        transformation[0] = Color.red(color) / 255f;
        transformation[1] = Color.green(color) / 255f;
        pixels[i] = applyTransformation(t, alpha, transformation);
    }
    return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
}

From source file:Main.java

public static Bitmap handleImageNegative(Bitmap bm) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    int color;//from   ww  w .j ava  2  s  . c  o m
    int r, g, b, a;
    Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
    int[] oldPx = new int[width * height];
    int[] newPx = new int[width * height];
    bm.getPixels(oldPx, 0, width, 0, 0, width, height);
    for (int i = 0; i < width * height; i++) {
        color = oldPx[i];
        r = Color.red(color);
        g = Color.green(color);
        b = Color.blue(color);
        a = Color.alpha(color);

        r = 255 - r;
        g = 255 - g;
        b = 255 - b;
        if (r > 255) {
            r = 255;
        } 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;
        }
        newPx[i] = Color.argb(a, r, g, b);
    }
    bmp.setPixels(newPx, 0, width, 0, 0, width, height);
    return bmp;

}

From source file:Main.java

public static int getDominantColor(Bitmap bitmap) {
    if (null == bitmap)
        return Color.TRANSPARENT;

    int redBucket = 0;
    int greenBucket = 0;
    int blueBucket = 0;
    int alphaBucket = 0;

    boolean hasAlpha = bitmap.hasAlpha();
    int pixelCount = bitmap.getWidth() * bitmap.getHeight();
    int[] pixels = new int[pixelCount];
    bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());

    for (int y = 0, h = bitmap.getHeight(); y < h; y++) {
        for (int x = 0, w = bitmap.getWidth(); x < w; x++) {
            int color = pixels[x + y * w]; // x + y * barHeight
            redBucket += (color >> 16) & 0xFF; // Color.red
            greenBucket += (color >> 8) & 0xFF; // Color.greed
            blueBucket += (color & 0xFF); // Color.blue
            if (hasAlpha)
                alphaBucket += (color >>> 24); // Color.alpha
        }/*  w ww . ja v a2 s.c  o  m*/
    }

    return Color.argb((hasAlpha) ? (alphaBucket / pixelCount) : 255, redBucket / pixelCount,
            greenBucket / pixelCount, blueBucket / pixelCount);
}

From source file:Main.java

public static Bitmap handleImagePixelsOldPhoto(Bitmap bm) {
    Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
    int width = bm.getWidth();
    int height = bm.getHeight();
    int color = 0;
    int r, g, b, a, r1, g1, b1;

    int[] oldPx = new int[width * height];
    int[] newPx = new int[width * height];

    bm.getPixels(oldPx, 0, bm.getWidth(), 0, 0, width, height);
    for (int i = 0; i < width * height; i++) {
        color = oldPx[i];//from  ww w  . j ava  2 s  . c om
        a = Color.alpha(color);
        r = Color.red(color);
        g = Color.green(color);
        b = Color.blue(color);

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

        if (r1 > 255) {
            r1 = 255;
        }
        if (g1 > 255) {
            g1 = 255;
        }
        if (b1 > 255) {
            b1 = 255;
        }

        newPx[i] = Color.argb(a, r1, g1, b1);
    }
    bmp.setPixels(newPx, 0, width, 0, 0, width, height);
    return bmp;
}

From source file:Main.java

public static Bitmap backsheetImage(Bitmap bm) {

    int width = bm.getWidth();
    int height = bm.getHeight();
    int color;//from ww w  . jav  a2 s.c  om
    int r, g, b, a;

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

    int[] oldPx = new int[width * height];
    int[] newPx = new int[width * height];

    bm.getPixels(oldPx, 0, width, 0, 0, width, height);

    for (int i = 0; i < oldPx.length; i++) {
        color = oldPx[i];
        r = Color.red(color);
        g = Color.green(color);
        b = Color.blue(color);
        a = Color.alpha(color);

        r = 255 - r;
        g = 255 - g;
        b = 255 - b;

        if (r > 255) {
            r = 255;
        } 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;
        }

        newPx[i] = Color.argb(a, r, g, b);
    }

    bitmap.setPixels(newPx, 0, width, 0, 0, width, height);
    return bitmap;
}

From source file:Main.java

public static Bitmap olderImage(Bitmap bm) {

    int width = bm.getWidth();
    int height = bm.getHeight();
    int color;//from w ww. ja v a2 s.c  o m
    int r, g, b, a;

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

    int[] oldPx = new int[width * height];
    int[] newPx = new int[width * height];

    bm.getPixels(oldPx, 0, width, 0, 0, width, height);

    for (int i = 0; i < oldPx.length; i++) {
        color = oldPx[i];
        r = Color.red(color);
        g = Color.green(color);
        b = Color.blue(color);
        a = Color.alpha(color);

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

        newPx[i] = Color.argb(a, r, g, b);
    }

    bitmap.setPixels(newPx, 0, width, 0, 0, width, height);
    return bitmap;
}

From source file:Main.java

public static Bitmap getBlur(Bitmap bitmap) {
    int iterations = 1;
    int radius = 8;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int[] inPixels = new int[width * height];
    int[] outPixels = new int[width * height];
    Bitmap blured = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.getPixels(inPixels, 0, width, 0, 0, width, height);
    for (int i = 0; i < iterations; i++) {
        blur(inPixels, outPixels, width, height, radius);
        blur(outPixels, inPixels, height, width, radius);
    }// ww w  .  ja  v  a 2  s .  c  o  m
    blured.setPixels(inPixels, 0, width, 0, 0, width, height);
    return blured;
}

From source file:Main.java

public static Bitmap applyFilter(Bitmap bmpOriginal) {
    int width = bmpOriginal.getWidth();
    int height = bmpOriginal.getHeight();

    int core[][] = { { 1, 1, 1 }, { 1, 2, 1 }, { 1, 1, 1 } };

    int[] result = new int[height * width];
    int[][] pixels2D = new int[height][width];
    for (int i = 0; i < height; i++) {
        bmpOriginal.getPixels(pixels2D[i], 0, width, 0, i, width, 1);
    }//from www .  ja v  a 2s.  c om

    int count = 0;
    int R, G, B;
    for (int i = 0; i < height; i++)
        for (int j = 0; j < width; j++) {
            R = dotProduct(i, j, 0, pixels2D, core) / 10 + 20;
            G = dotProduct(i, j, 1, pixels2D, core) / 10 + 20;
            B = dotProduct(i, j, 2, pixels2D, core) / 10 + 20;

            result[count] = Color.rgb(R, G, B);
            count++;
        }

    Bitmap bmpFiltered = Bitmap.createBitmap(result, 0, width, width, height, Bitmap.Config.RGB_565);
    return bmpFiltered;
}

From source file:Main.java

public static Bitmap reliefImage(Bitmap bm) {

    int width = bm.getWidth();
    int height = bm.getHeight();
    int color;//from   w  w  w  .ja  v a  2s  .  c  om
    int r, g, b, a;

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

    int[] oldPx = new int[width * height];
    int[] newPx = new int[width * height];

    bm.getPixels(oldPx, 0, width, 0, 0, width, height);

    for (int i = 1; i < oldPx.length; i++) {
        color = oldPx[i - 1];
        r = Color.red(color);
        g = Color.green(color);
        b = Color.blue(color);
        a = Color.alpha(color);

        int nextColor = oldPx[i];
        int r1 = Color.red(nextColor);
        int g1 = Color.green(nextColor);
        int b1 = Color.blue(nextColor);

        r = r1 - r + 127;
        g = g1 - g + 127;
        b = b1 - b + 127;

        if (r > 255) {
            r = 255;
        } 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;
        }

        newPx[i] = Color.argb(a, r, g, b);
    }

    bitmap.setPixels(newPx, 0, width, 0, 0, width, height);
    return bitmap;
}