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

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

    int[] sum = new int[3];
    for (int YY = 0; YY < width; ++YY) {
        for (int XX = 0; XX < height; ++XX) {
            int bitmapColor = pixels[(YY + XX * width)];
            sum[0] += Color.red(bitmapColor);
            sum[1] += Color.green(bitmapColor);
            sum[2] += Color.blue(bitmapColor);
        }/*w  ww  .ja v  a 2  s  .  co  m*/
    }

    return sum;
}

From source file:Main.java

/**
 * Return histogram of grayscaled image/*from w  w w. j a v  a2  s .c  o m*/
 */
public static int[] getHistogram(Bitmap bmp) {
    int[] histogram = new int[256];
    int width = bmp.getWidth();
    int height = bmp.getHeight();
    int[] pixels = new int[width * height];
    bmp.getPixels(pixels, 0, width, 0, 0, width, height);

    for (int i = 0; i < width * height; i++) {
        int c = Color.red(pixels[i]);
        histogram[c] += 1;
    }

    return histogram;
}

From source file:Main.java

public static byte[] rgb2YUV(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);

    int len = width * height;
    byte[] yuv = new byte[len * 3 / 2];
    int y, u, v;//from www  . ja  va 2  s  . c  o  m
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            int rgb = pixels[i * width + j] & 0x00FFFFFF;

            int r = rgb & 0xFF;
            int g = (rgb >> 8) & 0xFF;
            int b = (rgb >> 16) & 0xFF;

            y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
            u = ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
            v = ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;

            y = y < 16 ? 16 : (y > 255 ? 255 : y);
            u = u < 0 ? 0 : (u > 255 ? 255 : u);
            v = v < 0 ? 0 : (v > 255 ? 255 : v);

            yuv[i * width + j] = (byte) y;
            // yuv[len + (i >> 1) * width + (j & ~1) + 0] = (byte) u;
            // yuv[len + (i >> 1) * width + (j & ~1) + 1] = (byte) v;
        }
    }
    return yuv;
}

From source file:Main.java

public static int[] extractPixels(final Bitmap bitmap) {
    final int w = bitmap.getWidth();
    final int h = bitmap.getHeight();
    final int[] colors = new int[w * h];
    bitmap.getPixels(colors, 0, w, 0, 0, w, h);
    return colors;
}

From source file:Main.java

/**
 * Heuristic method to determine if the image looks empty. Works by taking a horisontal row of pixels from he
 * middle, and looks if they're all pure white.
 *
 * @param refinedImage A pre-processed image of the evolution cost. (should be pre-refined to replace all non
 *                     text colors with pure white)
 * @return true if the image is likely only white
 *//* www  .  j  av  a 2 s  .  co  m*/
private static boolean isOnlyWhite(Bitmap refinedImage) {
    int[] pixelArray = new int[refinedImage.getWidth()];

    //below code takes one line of pixels in the middle of the pixture from left to right
    refinedImage.getPixels(pixelArray, 0, refinedImage.getWidth(), 0, refinedImage.getHeight() / 2,
            refinedImage.getWidth(), 1);

    for (int pixel : pixelArray) {
        if (pixel != Color.rgb(255, 255, 255)) { // if pixel is not white
            return false;
        }
    }
    return true;
}

From source file:Main.java

public static Bitmap resize(Bitmap src, int w2, int h2) {
    int w1 = src.getWidth();
    int h1 = src.getHeight();
    int[] pxSource = new int[w1 * h1];
    int[] pxResult = new int[w2 * h2];

    src.getPixels(pxSource, 0, w1, 0, 0, w1, h1);
    double x_ratio = w1 / (double) w2;
    double y_ratio = h1 / (double) h2;
    double px, py;
    for (int i = 0; i < h2; i++) {
        for (int j = 0; j < w2; j++) {
            px = Math.floor(j * x_ratio);
            py = Math.floor(i * y_ratio);
            pxResult[(i * w2) + j] = pxSource[(int) ((py * w1) + px)];
        }//  ww w . j a va  2s.c o  m
    }

    return Bitmap.createBitmap(pxResult, w2, h2, Config.ARGB_8888);
}

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;//  w  w  w  .ja v  a  2 s .co  m
            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 effectWhiten(Bitmap bitmap, int threshold) {
    if (bitmap == null) {
        return bitmap;
    }//from w w w  . j a  va2  s .  c  o  m

    Bitmap dst = bitmap.copy(Bitmap.Config.ARGB_8888, true);

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

    for (int YY = 0; YY < width; ++YY) {
        for (int XX = 0; XX < height; ++XX) {
            int bitmapColor = pixels[(YY + XX * width)];
            int[] color = { Color.red(bitmapColor), Color.green(bitmapColor), Color.blue(bitmapColor) };

            if (color[0] > threshold && color[1] > threshold && color[2] > threshold) {
                color[0] = 255;
                color[1] = 255;
                color[2] = 255;
            }
            pixels[(YY + XX * width)] = Color.rgb(color[0], color[1], color[2]);
        }
    }

    dst.setPixels(pixels, 0, width, 0, 0, width, height);
    return dst;
}

From source file:Main.java

public static int[] getPixels(final Bitmap bit) {
    if (bit == null) {
        return null;
    }/* www.  ja va 2s .  c o  m*/
    int width = bit.getWidth();
    int height = bit.getHeight();
    int pixels[] = new int[width * height];
    bit.getPixels(pixels, 0, width, 0, 0, width, height);
    return pixels;
}

From source file:Main.java

private static int[] reduceColor(Bitmap bitmap) {
    sum = 0;//from   w w w .ja  v a 2 s  . co  m
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int[] grayValues = new int[width * height];
    int[] pix = new int[width * height];
    bitmap.getPixels(pix, 0, width, 0, 0, width, height);
    for (int i = 0; i < width; i++)
        for (int j = 0; j < height; j++) {
            int x = j * width + i;
            int r = (pix[x] >> 16) & 0xff;
            int g = (pix[x] >> 8) & 0xff;
            int b = pix[x] & 0xff;
            int grayValue = (r * 30 + g * 59 + b * 11) / 100;
            sum += grayValue;
            grayValues[x] = grayValue;
        }
    return grayValues;
}