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 int hashBitmap(Bitmap bitmap) {
    int hash_result = 0;
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    hash_result = (hash_result << 7) ^ h;
    hash_result = (hash_result << 7) ^ w;
    for (int pixel = 0; pixel < 20; ++pixel) {
        int x = (pixel * 50) % w;
        int y = (pixel * 100) % h;
        hash_result = (hash_result << 7) ^ bitmap.getPixel(x, y);
    }/*from   ww  w  . j  a  va  2s.c om*/
    return hash_result;
}

From source file:Main.java

/**
 * @param x X coordinate of the region//  ww  w . j  av  a2  s. co  m
 * @param y Y coordinate of the region
 * @param bmp Source bitmap
 * @return color value of the region
 * */
static int computeAverageColor(int x, int y, Bitmap bmp) {
    int res[] = new int[3];
    for (int col = 0; col < 3; col++) {
        for (int i = x * STEP; i < (x + 1) * STEP; i++)
            for (int j = y * STEP; j < (y + 1) * STEP; j++) {
                switch (col) {
                case 0: //red
                    res[0] = res[0] + Color.red(bmp.getPixel(i, j));
                    break;

                case 1: //green
                    res[1] = res[1] + Color.green(bmp.getPixel(i, j));
                    break;

                case 2: //blue
                    res[2] = res[2] + Color.blue(bmp.getPixel(i, j));
                    break;
                }
            }
        res[col] = res[col] / (STEP * STEP);
    }

    return Color.rgb(res[0], res[1], res[2]);
}

From source file:Main.java

public static Bitmap doGray(Bitmap src) {
    final double GS_RED = 0.299;
    final double GS_GREEN = 0.587;
    final double GS_BLUE = 0.114;

    Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
    int A, R, G, B, pixel;
    int w = src.getWidth();
    int h = src.getHeight();
    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            pixel = src.getPixel(x, y);

            A = Color.alpha(pixel);
            R = Color.red(pixel);
            G = Color.green(pixel);
            B = Color.blue(pixel);
            R = G = B = (int) (GS_RED * R + GS_GREEN * G + GS_BLUE * B);
            bmOut.setPixel(x, y, Color.argb(A, R, G, B));
        }//from   ww w  .j a  v  a 2 s . com
    }
    return bmOut;
}

From source file:Main.java

public static Bitmap toThreshold(Bitmap bmpOriginal) {
    int imageWidth = bmpOriginal.getWidth();
    int imageHeight = bmpOriginal.getHeight();
    Bitmap bmpThreshold = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.RGB_565);
    int[] buffer = new int[imageWidth];

    int grayVal;/*from   w  ww .  j a  va2s . c o m*/
    for (int row = 0; row < imageHeight; row++) {
        for (int col = 0; col < imageWidth; col++) {
            grayVal = rgbToGray(bmpOriginal.getPixel(col, row));
            if (grayVal > 125)
                buffer[col] = Color.rgb(255, 255, 255);
            else
                buffer[col] = Color.rgb(0, 0, 0);
        }
        bmpThreshold.setPixels(buffer, 0, imageWidth, 0, row, imageWidth, 1);
    }
    return bmpThreshold;
}

From source file:Main.java

public static int[] getPixelsFromBitmap(Bitmap bm, int x, int y, int width, int height) {
    int[] pixels = new int[width * height];

    int tSize = byteSizeOf(bm);
    int size = width * height;
    int tWidth = bm.getWidth();
    int tHeight = bm.getHeight();
    int a = x;//from  w  w  w  .  j a  v  a2  s.c om
    int b = y;
    int j = 0;
    for (int i = a + b * tWidth; i < size; i++) {
        if (a == x + width) {
            a = x;
            b++;
        }
        pixels[j++] = bm.getPixel(a, b);
        a++;
    }

    return pixels;
}

From source file:com.calgen.udacity.lego.ui.ColorUtils.java

/**
 * Determines if a given bitmap is dark. This extracts a palette inline so should not be called
 * with a large image!! If palette fails then check the color of the specified pixel
 *//*  ww  w  .j a v  a  2s  .c o  m*/
public static boolean isDark(@NonNull Bitmap bitmap, int backupPixelX, int backupPixelY) {
    // first try palette with a small color quanta
    Palette palette = Palette.from(bitmap).maximumColorCount(3).generate();
    if (palette.getSwatches().size() > 0) {
        return isDark(palette) == IS_DARK;
    } else {
        // if palette failed, then check the color of the specified pixel
        return isDark(bitmap.getPixel(backupPixelX, backupPixelY));
    }
}

From source file:Main.java

public static Bitmap doSherpiaEffect(Bitmap src, int depth, double red, double green, double blue) {
    int width = src.getWidth();
    int height = src.getHeight();
    Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
    final double GS_RED = 0.3;
    final double GS_GREEN = 0.59;
    final double GS_BLUE = 0.11;
    int A, R, G, B;
    int pixel;/*  w  ww  .j  a v  a  2  s  .  co  m*/

    for (int x = 0; x < width; ++x) {
        for (int y = 0; y < height; ++y) {
            pixel = src.getPixel(x, y);
            A = Color.alpha(pixel);
            R = Color.red(pixel);
            G = Color.green(pixel);
            B = Color.blue(pixel);
            B = G = R = (int) (GS_RED * R + GS_GREEN * G + GS_BLUE * B);

            R += (depth * red);
            if (R > 255) {
                R = 255;
            }

            G += (depth * green);
            if (G > 255) {
                G = 255;
            }

            B += (depth * blue);
            if (B > 255) {
                B = 255;
            }

            bmOut.setPixel(x, y, Color.argb(A, R, G, B));
        }
    }
    return bmOut;
}

From source file:Main.java

public static int getColorOnBitmap(Bitmap src, Rect serchRect, int defaultColor) {
    int width = serchRect.width();
    int height = serchRect.height();

    //        Log.d("getColorOnBitmap", String.format("bitmap width = (%s), height = (%s), Rect = (%s)", src.getWidth()
    //                                                   , src.getHeight(), serchRect.toString()));

    if (width == 0 || height == 0)
        return defaultColor;

    if (width == 1 && height == 1) {
        return src.getPixel(serchRect.left, serchRect.top);
    }/*www  . ja  va2 s.  co m*/
    if (width == 1 && height > 1) {
        int colorMerge = 0xffffffff;
        for (int start = 0; start < height; ++start) {
            colorMerge = colorMerge & src.getPixel(serchRect.left, serchRect.top + start);
        }
        return colorMerge;
    }
    if (width > 1 && height == 1) {
        int colorMerge = 0xffffffff;
        for (int start = 0; start < width; ++start) {
            colorMerge = colorMerge & src.getPixel(serchRect.left + start, serchRect.top);
        }
        return colorMerge;
    }
    if (width > 1 && height > 1) {
        int colorMerge = 0xffffffff;
        for (int x = 0; x < width; ++x) {
            for (int y = 0; y < height; ++y) {
                colorMerge = colorMerge & src.getPixel(serchRect.left + x, serchRect.top + y);
            }
        }
        return colorMerge;
    }

    return defaultColor;
}

From source file:com.hellofyc.base.net.http.HttpUtils.java

private static byte[] bitmapToBytes(@NonNull Bitmap bitmap) {
    byte[] bytes = new byte[bitmap.getWidth() * bitmap.getHeight()];
    for (int i = 0; i < bitmap.getWidth(); i++) {
        for (int j = 0; j < bitmap.getHeight(); j++) {
            bytes[i + j] = (byte) (bitmap.getPixel(i, j) & 0x80 >> 7);
        }/*from  w w  w  .ja va2  s .c  o m*/
    }
    return bytes;
}

From source file:jahirfiquitiva.iconshowcase.utilities.color.ColorUtils.java

/**
 * Determines if a given bitmap is dark. This extracts a palette inline so should not be called
 * with a large image!! If palette fails then check the color of the specified pixel
 *//*from w w w . jav  a2s  .  c om*/
public static boolean isDark(@NonNull Bitmap bitmap, int backupPixelX, int backupPixelY) {
    // first try palette with a small color quant size
    Palette palette = Palette.from(bitmap).generate();
    if (palette.getSwatches().size() > 0) {
        return isDark(palette) == IS_DARK;
    } else {
        // if palette failed, then check the color of the specified pixel
        return isDark(bitmap.getPixel(backupPixelX, backupPixelY));
    }
}