Example usage for android.graphics Bitmap setPixels

List of usage examples for android.graphics Bitmap setPixels

Introduction

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

Prototype

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

Source Link

Document

Replace pixels in the bitmap with the colors in the array.

Usage

From source file:Main.java

public static void setPixels(final Bitmap bit, final int[] pixels, int w, int h) {
    bit.setPixels(pixels, 0, w, 0, 0, w, h);
}

From source file:Main.java

public static Bitmap applyShadingFilter(Bitmap source, int shadingColor) {
    int width = source.getWidth();
    int height = source.getHeight();
    int[] pixels = new int[width * height];
    source.getPixels(pixels, 0, width, 0, 0, width, height);

    int index = 0;
    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            index = y * width + x;//w  w  w . jav  a2 s  .c  o m
            pixels[index] &= shadingColor;
        }
    }
    Bitmap bmOut = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bmOut.setPixels(pixels, 0, width, 0, 0, width, height);
    return bmOut;
}

From source file:Main.java

protected static Bitmap getBitmapFromData(int[] pixels, int width, int height) {
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}

From source file:Main.java

public static Bitmap applySaturationFilter(Bitmap source, int level) {
    int width = source.getWidth();
    int height = source.getHeight();
    int[] pixels = new int[width * height];
    float[] HSV = new float[3];
    source.getPixels(pixels, 0, width, 0, 0, width, height);

    int index = 0;
    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            index = y * width + x;/*from ww  w.  jav  a2s  . c om*/
            Color.colorToHSV(pixels[index], HSV);
            HSV[1] *= level;
            HSV[1] = (float) Math.max(0.0, Math.min(HSV[1], 1.0));
            pixels[index] |= Color.HSVToColor(HSV);
        }
    }
    Bitmap bmOut = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bmOut.setPixels(pixels, 0, width, 0, 0, width, height);
    return bmOut;
}

From source file:Main.java

public static Bitmap changeColor(Bitmap bitmap, int oldColor, int newColor) {
    if (bitmap == null) {
        return bitmap;
    }/*from  w  w w  .j  av  a2s .c om*/

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
    for (int x = 0; x < pixels.length; ++x) {
        pixels[x] = (pixels[x] == oldColor) ? newColor : pixels[x];
    }
    Bitmap newBitmap = Bitmap.createBitmap(width, height, bitmap.getConfig());
    newBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return newBitmap;
}

From source file:Main.java

public static Bitmap convertToBlackWhite(Bitmap bmp) {
    int width = bmp.getWidth();
    int height = bmp.getHeight();
    int[] pixels = new int[width * height];

    bmp.getPixels(pixels, 0, width, 0, 0, width, height);
    int alpha = 0xFF << 24;
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            int grey = pixels[width * i + j];

            int red = ((grey & 0x00FF0000) >> 16);
            int green = ((grey & 0x0000FF00) >> 8);
            int blue = (grey & 0x000000FF);

            grey = (int) (red * 0.3 + green * 0.59 + blue * 0.11);
            grey = alpha | (grey << 16) | (grey << 8) | grey;
            pixels[width * i + j] = grey;
        }//from  w ww  .  j  a  va 2 s .c  o m
    }
    Bitmap newBmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    newBmp.setPixels(pixels, 0, width, 0, 0, width, height);
    return ThumbnailUtils.extractThumbnail(newBmp, 380, 460);
}

From source file:Main.java

public static Bitmap blackAndWhited(Bitmap in) {
    int width = in.getWidth();
    int height = in.getHeight();

    int[] pixels = new int[width * height];
    in.getPixels(pixels, 0, width, 0, 0, width, height);
    int alpha = 0xFF << 24;
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            int grey = pixels[width * i + j];
            int red = ((grey & 0x00FF0000) >> 16);
            int green = ((grey & 0x0000FF00) >> 8);
            int blue = (grey & 0x000000FF);
            grey = (int) (red * 0.3 + green * 0.59 + blue * 0.11);
            grey = alpha | (grey << 16) | (grey << 8) | grey;
            pixels[width * i + j] = grey;
        }/* w w w .  ja v  a2  s .c om*/
    }
    Bitmap newBmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    newBmp.setPixels(pixels, 0, width, 0, 0, width, height);
    return newBmp;
}

From source file:Main.java

public static Bitmap onlyColors(Bitmap bitmap, int[] colors) {
    if (bitmap == null) {
        return bitmap;
    }/*w ww  . j a va2s  .com*/
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
    boolean change;
    for (int x = 0; x < pixels.length; ++x) {
        change = true;
        for (int color : colors) {
            if (pixels[x] == color) {
                change = false;
            }
        }
        if (change) {
            pixels[x] = Color.TRANSPARENT;
        }
    }
    Bitmap newBitmap = Bitmap.createBitmap(width, height, bitmap.getConfig());
    newBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return newBitmap;
}

From source file:Main.java

public static Bitmap createBitmapWithAlphaMask(Bitmap bmpSource, Bitmap bmpMask) {
    int width = bmpSource.getWidth();
    int height = bmpSource.getHeight();
    int size = width * height;

    if (width != bmpMask.getWidth() || height != bmpMask.getHeight())
        bmpMask = resize(bmpMask, width, height);

    int[] result = new int[size];
    int[] mask = new int[size];
    bmpSource.getPixels(result, 0, width, 0, 0, width, height);
    bmpMask.getPixels(mask, 0, width, 0, 0, width, height);

    int alphaMask = 0xff000000;
    int colorMask = 0x00ffffff;
    for (int i = 0; i < size; i++) {
        result[i] = (mask[i] & alphaMask) | (result[i] & colorMask);
    }//from w  w  w .j  ava2 s  .c o  m

    // ensuring the bitmap is mutable
    Bitmap bmpResult = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    bmpResult.setPixels(result, 0, width, 0, 0, width, height);

    return bmpResult;
}

From source file:Main.java

public static Drawable createGradientColorAndCover(Context context, int coveredResID, int CoverColor) {
    //final Resources res=context.getResources();
    final Resources res = context.getResources();
    Bitmap bitmap = BitmapFactory.decodeResource(res, coveredResID);
    if (bitmap == null) {
        return null;
    }/* w ww .j  a va  2 s . c  o  m*/
    final int length = bitmap.getWidth();
    final int height = bitmap.getHeight();
    int[] shape = new int[length * height];
    int[] colors = new int[length * height];
    bitmap.getPixels(shape, 0, length, 0, 0, length, height);
    int color = CoverColor;
    for (int i = 0; i < length * height; i++) {
        float percent = ((float) i % length / length) * 0xff;
        int alpha = ((int) percent << 6 * 4);
        alpha &= shape[i] & 0xFF000000;
        colors[i] = (alpha) | (color & 0x00FFFFFF);
    }
    Bitmap newbitmap = Bitmap.createBitmap(length, height, Bitmap.Config.ARGB_8888);
    newbitmap.setPixels(colors, 0, length, 0, 0, length, height);
    Bitmap fooBitmap = Bitmap.createBitmap(length, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(fooBitmap);
    Paint paint = new Paint();
    canvas.drawBitmap(bitmap, 0, 0, paint);
    canvas.drawBitmap(newbitmap, 0, 0, paint);
    newbitmap.recycle();
    bitmap.recycle();
    return new BitmapDrawable(res, fooBitmap);
}