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

/**
 * Get the hashcode for a bitmap.//from   w w  w  .  j a va2 s . c  o  m
 */
private static String hashBitmap(Bitmap bmp) {
    int[] allpixels = new int[bmp.getHeight() * bmp.getWidth()];
    bmp.getPixels(allpixels, 0, bmp.getWidth(), 0, 0, bmp.getWidth(), bmp.getHeight());
    return Integer.toHexString(Arrays.hashCode(allpixels));
}

From source file:Main.java

@Deprecated
public static Bitmap setAlpha(Bitmap source, int number) {
    int[] argb = new int[source.getWidth() * source.getHeight()];
    source.getPixels(argb, 0, source.getWidth(), 0, 0, source.getWidth(), source.getHeight());
    number = number * 255 / 100;//w  w w.  jav  a2 s  .  co m
    for (int i = 0; i < argb.length; i++) {
        argb[i] = (number << 24) | (argb[i] & 0x00FFFFFF);
    }
    return Bitmap.createBitmap(argb, source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
}

From source file:Main.java

public static byte[] bitmapToNV21(int inputWidth, int inputHeight, Bitmap bitmap) {
    int[] argb = new int[inputWidth * inputHeight];
    bitmap.getPixels(argb, 0, inputWidth, 0, 0, inputWidth, inputHeight);
    byte[] yuv = ARGBtoYUV420SemiPlanar(argb, inputWidth, inputHeight);
    bitmap.recycle();//from ww  w .  j av a2  s  .  c  om
    return yuv;
}

From source file:Main.java

public static byte[] getNV21(int inputWidth, int inputHeight, Bitmap scaled) {

    int[] argb = new int[inputWidth * inputHeight];
    scaled.getPixels(argb, 0, inputWidth, 0, 0, inputWidth, inputHeight);
    byte[] yuv = new byte[inputWidth * inputHeight * 3 / 2];
    encodeYUV420SP(yuv, argb, inputWidth, inputHeight);
    return yuv;//  www .j  a v a2 s.  co m
}

From source file:Main.java

public static int[] getPixels(Bitmap bit) {
    int w = bit.getWidth(), h = bit.getHeight();
    int pixels[] = new int[w * h];
    bit.getPixels(pixels, 0, w, 0, 0, w, h);
    return pixels;
}

From source file:Main.java

public static int[] getBitmapPixels(@NonNull Bitmap bitmap, int x, int y, int width, int height) {
    int[] pixels = new int[bitmap.getWidth() * bitmap.getHeight()];
    bitmap.getPixels(pixels, 0, bitmap.getWidth(), x, y, width, height);
    final int[] subsetPixels = new int[width * height];
    for (int row = 0; row < height; row++) {
        System.arraycopy(pixels, (row * bitmap.getWidth()), subsetPixels, row * width, width);
    }//from  w  w  w. j av a 2  s. c  o m
    return subsetPixels;
}

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  w  w  .  j  av a2  s  .  co 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;
        }//from w w  w .  j a v a  2 s.  com
    }
    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 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;/*from  w  ww .  j  a  v  a 2 s .  c om*/
            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

public static int[] bitmapToIntArray(Bitmap bitmap) {
    final int bitmapWidth = bitmap.getWidth();
    final int bitmapHeight = bitmap.getHeight();

    int[] colors = new int[bitmapWidth * bitmapHeight];
    bitmap.getPixels(colors, 0, bitmapWidth, 0, 0, bitmapWidth, bitmapHeight);

    return colors;
}