Example usage for android.graphics Bitmap getPixel

List of usage examples for android.graphics Bitmap getPixel

Introduction

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

Prototype

@ColorInt
public int getPixel(int x, int y) 

Source Link

Document

Returns the Color at the specified location.

Usage

From source file:Main.java

public static float compareBitmaps(Bitmap a, Bitmap b) {
    int found = 0;

    for (int y = 0; y < a.getHeight(); y++) {
        for (int x = 0; x < a.getWidth(); x++) {
            if (a.getPixel(x, y) == a.getPixel(x, y)) {
                found++;// w w w  . j  a v  a2s .  c o  m
            }
        }
    }

    return found / (float) (a.getWidth() * a.getHeight());
}

From source file:Main.java

public static int filter(final ColorFilter filter, final int color) {
    final Paint paint = new Paint();
    paint.setColor(color);/*from w  w  w  .  jav a2 s  .c  o  m*/
    paint.setColorFilter(filter);
    final Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(bitmap);
    canvas.drawPaint(paint);
    return bitmap.getPixel(0, 0);
}

From source file:Main.java

/**
 * calculate hashcode for a bitmap./*from  w w  w.j  av  a2  s. com*/
 * only calculate part of the first row for performance purpose.
 * if you want to involve every pixels, use {@link #bitmap2hashFull(Bitmap)}.
 * @return hash code
 */
public static long bitmap2hash(Bitmap bitmap) {
    if (bitmap == null)
        return 0;
    long hash = 31;
    int end = bitmap.getWidth() / 5;
    for (int x = 0; x < end; x++) {
        hash *= (bitmap.getPixel(x, 0) + 31);
    }
    return hash;
}

From source file:Main.java

public static byte[][] bitmap2grayByteArry(Bitmap bm) {
    byte[][] grayImage = new byte[bm.getHeight()][bm.getWidth()];
    for (int i = 0; i < bm.getWidth(); i++) {
        for (int j = 0; j < bm.getHeight(); j++) {
            if (bm.getPixel(i, j) == Color.WHITE) {
                grayImage[j][i] = 0;//from ww w  . ja v a 2s . co  m
            } else {
                grayImage[j][i] = 1;
            }
        }
    }
    return grayImage;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static int getColor(final ColorDrawable colorDrawable) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        final Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
        final Canvas canvas = new Canvas(bitmap);
        colorDrawable.draw(canvas);//  www .ja v  a 2 s  .  co  m
        return bitmap.getPixel(0, 0);
    } else {
        return colorDrawable.getColor();
    }
}

From source file:Main.java

public static int getPixelsCount(Bitmap bitmap) {
    int hlen = bitmap.getWidth(), vlen = bitmap.getHeight();
    int toReturn = 0;
    for (int i = 0; i < vlen; i++) {
        for (int j = 0; j < hlen; j++) {
            if ((bitmap.getPixel(j, i) & 0xff) > 0) {
                toReturn++;/*  w w  w .  ja  va2s .co m*/
            }
        }
    }

    return toReturn;
}

From source file:Main.java

/**
 * calculate hashcode for a bitmap.//from   w ww.  ja  v a 2s .c  o m
 * all pixels in the bitmap is considered for the hashcode, and if you want
 * a better performance for the calculation, use {@link #bitmap2hash(Bitmap)}.
 * @return hash code
 */
public static long bitmap2hashFull(Bitmap bitmap) {
    if (bitmap == null)
        return 0;
    long hash = 31;
    for (int x = 0; x < bitmap.getWidth(); x++) {
        for (int y = 0; y < bitmap.getHeight(); y++) {
            hash *= (bitmap.getPixel(x, y) + 31);
        }
    }
    return hash;
}

From source file:Main.java

public static void invertImage(Bitmap b) {
    int A, R, G, B;
    int pixelColor;
    int height = b.getHeight();
    int width = b.getWidth();

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            pixelColor = b.getPixel(x, y);
            A = Color.alpha(pixelColor);

            R = 255 - Color.red(pixelColor);
            G = 255 - Color.green(pixelColor);
            B = 255 - Color.blue(pixelColor);

            b.setPixel(x, y, Color.argb(A, R, G, B));
        }//from  ww  w.j a va  2s.  c o m
    }
}

From source file:Main.java

public static int[][] getImageARGB(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    final int[][] rgbArray = new int[width][height];
    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            rgbArray[i][j] = bitmap.getPixel(i, j);
        }//from   ww  w. ja  v  a 2 s . c om
    }
    return rgbArray;
}

From source file:Main.java

public static byte[] getGrayscale(Bitmap bitmap) {
    if (bitmap == null)
        return null;
    byte[] ret = new byte[bitmap.getWidth() * bitmap.getHeight()];
    for (int j = 0; j < bitmap.getHeight(); ++j)
        for (int i = 0; i < bitmap.getWidth(); ++i) {
            int pixel = bitmap.getPixel(i, j);
            int red = ((pixel & 0x00FF0000) >> 16);
            int green = ((pixel & 0x0000FF00) >> 8);
            int blue = pixel & 0x000000FF;
            ret[j * bitmap.getWidth() + i] = (byte) ((299 * red + 587 * green + 114 * blue) / 1000);
        }//w ww  .j a  v  a  2  s .co m
    return ret;
}