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 Bitmap effectChangeContrast(Bitmap bitmap, double effectLevel) {
    if (bitmap == null) {
        return bitmap;
    }//  ww w .  j  a  va  2s. co  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] > 200 && color[1] > 200 && color[3] > 200) {
                color[0] = 255;
                color[1] = 255;
                color[2] = 255;
            }

            for (int i = 0; i < color.length; i++) {
                int tmpColor = color[i];
                tmpColor = (int) ((tmpColor - 128) * effectLevel + 128);
                if (tmpColor < 0) {
                    tmpColor = 0;
                } else if (tmpColor > 255) {
                    tmpColor = 255;
                }
                color[i] = tmpColor;
            }

            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

static public void setImageColorPixels(ImageView view, Bitmap myBitmap, int rgbcolor)// ,Bitmap sourceBitmap)
{

    int intArray[];

    intArray = new int[myBitmap.getWidth() * myBitmap.getHeight()];

    // copy pixel data from the Bitmap into the 'intArray' array
    myBitmap.getPixels(intArray, 0, myBitmap.getHeight(), 0, 0, myBitmap.getHeight(), myBitmap.getWidth());

    // replace the red pixels with yellow ones
    for (int i = 0; i < intArray.length; i++) {
        // System.out.println("color is--" + i + " " + intArray[i]);
        if (intArray[i] != Color.TRANSPARENT) {

            intArray[i] = rgbcolor;//from w w w  . j  a v  a 2s .  c om
        }
    }
    myBitmap.setPixels(intArray, 0, myBitmap.getHeight(), 0, 0, myBitmap.getHeight(), myBitmap.getWidth());

    view.setImageBitmap(myBitmap);
}

From source file:Main.java

public static Bitmap binarize(Bitmap bmp) {
    int threshold = Math.round(getOtsuThreshold(bmp));
    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 p = 0;
        if (Color.red(pixels[i]) > threshold)
            p = 255;//from www.  j a v  a 2s.  c o m
        pixels[i] = Color.rgb(p, p, p);
    }

    return Bitmap.createBitmap(pixels, width, height, bmp.getConfig());
}

From source file:org.odk.collect.android.utilities.QRCodeUtils.java

@NonNull
private static BinaryBitmap getBinaryBitmap(Bitmap bitmap) {
    int[] intArray = new int[bitmap.getWidth() * bitmap.getHeight()];

    //copy pixel data from bitmap into the array
    bitmap.getPixels(intArray, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());

    LuminanceSource source = new RGBLuminanceSource(bitmap.getWidth(), bitmap.getHeight(), intArray);
    return new BinaryBitmap(new HybridBinarizer(source));
}

From source file:Main.java

public static Bitmap changeColor(Bitmap bitmap, int oldColor, int newColor) {
    if (bitmap == null) {
        return bitmap;
    }//from   w  ww .  j a  va2  s .  com

    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

/**
 * Returns true if 20% of the image's top right corner is white, or 20% of the bottom of the
 * image is white.//from ww w  .  j  av  a  2  s. c  om
 */
public static boolean isBitmapWhiteAtTopOrBottom(Bitmap largeBitmap) {
    final Bitmap smallBitmap = scaleBitmapDown(largeBitmap);

    final int[] rgbPixels = new int[smallBitmap.getWidth() * smallBitmap.getHeight()];
    smallBitmap.getPixels(rgbPixels, 0, smallBitmap.getWidth(), 0, 0, smallBitmap.getWidth(),
            smallBitmap.getHeight());

    // look at top right corner of the bitmap
    int whiteCount = 0;
    for (int y = 0; y < smallBitmap.getHeight() * HEIGHT_PERCENT_ANALYZED; y++) {
        for (int x = (int) (smallBitmap.getWidth() * (1 - THIRD)); x < smallBitmap.getWidth(); x++) {
            final int rgb = rgbPixels[y * smallBitmap.getWidth() + x];
            if (isWhite(rgb)) {
                whiteCount++;
            }
        }
    }
    int totalPixels = (int) (smallBitmap.getHeight() * smallBitmap.getWidth() * THIRD
            * HEIGHT_PERCENT_ANALYZED);
    if (whiteCount / (float) totalPixels > PROPORTION_WHITE_CUTOFF) {
        return true;
    }

    // look at bottom portion of bitmap
    whiteCount = 0;
    for (int y = (int) (smallBitmap.getHeight() * (1 - HEIGHT_PERCENT_ANALYZED)); y < smallBitmap
            .getHeight(); y++) {
        for (int x = 0; x < smallBitmap.getWidth(); x++) {
            final int rgb = rgbPixels[y * smallBitmap.getWidth() + x];
            if (isWhite(rgb)) {
                whiteCount++;
            }
        }
    }

    totalPixels = (int) (smallBitmap.getHeight() * smallBitmap.getWidth() * HEIGHT_PERCENT_ANALYZED);

    return whiteCount / (float) totalPixels > PROPORTION_WHITE_CUTOFF;
}

From source file:Main.java

public static Bitmap onlyColors(Bitmap bitmap, int[] colors) {
    if (bitmap == null) {
        return bitmap;
    }//from w  ww  . j av a 2  s . co m
    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

private static boolean calculateIsDark(Bitmap bitmap) {
    boolean dark = false;

    float darkThreshold = bitmap.getWidth() * (bitmap.getHeight() / 5) * 0.55f;
    int darkPixels = 0;

    int[] pixels = new int[bitmap.getWidth() * (bitmap.getHeight() / 5)];
    bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, bitmap.getHeight() / 5 * 4, bitmap.getWidth(),
            bitmap.getHeight() / 5);// w  ww .  ja  v  a 2s  .  co  m

    for (int pixel : pixels) {
        int r = Color.red(pixel);
        int g = Color.green(pixel);
        int b = Color.blue(pixel);
        double luminance = (0.299 * r + 0.0f + 0.587 * g + 0.0f + 0.114 * b + 0.0f);
        if (luminance < 150) {
            darkPixels++;
        }
    }

    if (darkPixels >= darkThreshold) {
        dark = true;
    }

    return dark;
}

From source file:Main.java

/**
 * Returns true if 20% of the image's top right corner is white, or 20% of the bottom
 * of the image is white.//  ww  w  .ja va2 s. c o m
 */
public static boolean isBitmapWhiteAtTopOrBottom(Bitmap largeBitmap) {
    if (Build.VERSION.SDK_INT >= 18) {
        Trace.beginSection("isBitmapWhiteAtTopOrBottom");
    }
    try {
        final Bitmap smallBitmap = scaleBitmapDown(largeBitmap);

        final int[] rgbPixels = new int[smallBitmap.getWidth() * smallBitmap.getHeight()];
        smallBitmap.getPixels(rgbPixels, 0, smallBitmap.getWidth(), 0, 0, smallBitmap.getWidth(),
                smallBitmap.getHeight());

        // look at top right corner of the bitmap
        int whiteCount = 0;
        for (int y = 0; y < smallBitmap.getHeight() * HEIGHT_PERCENT_ANALYZED; y++) {
            for (int x = (int) (smallBitmap.getWidth() * (1 - THIRD)); x < smallBitmap.getWidth(); x++) {
                final int rgb = rgbPixels[y * smallBitmap.getWidth() + x];
                if (isWhite(rgb)) {
                    whiteCount++;
                }
            }
        }
        int totalPixels = (int) (smallBitmap.getHeight() * smallBitmap.getWidth() * THIRD
                * HEIGHT_PERCENT_ANALYZED);
        if (whiteCount / (float) totalPixels > PROPORTION_WHITE_CUTOFF) {
            return true;
        }

        // look at bottom portion of bitmap
        whiteCount = 0;
        for (int y = (int) (smallBitmap.getHeight() * (1 - HEIGHT_PERCENT_ANALYZED)); y < smallBitmap
                .getHeight(); y++) {
            for (int x = 0; x < smallBitmap.getWidth(); x++) {
                final int rgb = rgbPixels[y * smallBitmap.getWidth() + x];
                if (isWhite(rgb)) {
                    whiteCount++;
                }
            }
        }

        totalPixels = (int) (smallBitmap.getHeight() * smallBitmap.getWidth() * HEIGHT_PERCENT_ANALYZED);

        return whiteCount / (float) totalPixels > PROPORTION_WHITE_CUTOFF;
    } finally {
        if (Build.VERSION.SDK_INT >= 18) {
            Trace.endSection();
        }
    }
}

From source file:Main.java

/**
 * Returns true if 20% of the image's top right corner is white, or 20% of the bottom
 * of the image is white.//  w w  w . j av  a  2 s  . c om
 */
public static boolean isBitmapWhiteAtTopOrBottom(@NonNull Bitmap largeBitmap) {
    if (Build.VERSION.SDK_INT >= 18) {
        Trace.beginSection("isBitmapWhiteAtTopOrBottom");
    }
    try {
        final Bitmap smallBitmap = scaleBitmapDown(largeBitmap);

        final int[] rgbPixels = new int[smallBitmap.getWidth() * smallBitmap.getHeight()];
        smallBitmap.getPixels(rgbPixels, 0, smallBitmap.getWidth(), 0, 0, smallBitmap.getWidth(),
                smallBitmap.getHeight());

        // look at top right corner of the bitmap
        int whiteCount = 0;
        for (int y = 0; y < smallBitmap.getHeight() * HEIGHT_PERCENT_ANALYZED; y++) {
            for (int x = (int) (smallBitmap.getWidth() * (1 - THIRD)); x < smallBitmap.getWidth(); x++) {
                final int rgb = rgbPixels[y * smallBitmap.getWidth() + x];
                if (isWhite(rgb)) {
                    whiteCount++;
                }
            }
        }
        int totalPixels = (int) (smallBitmap.getHeight() * smallBitmap.getWidth() * THIRD
                * HEIGHT_PERCENT_ANALYZED);
        if (whiteCount / (float) totalPixels > PROPORTION_WHITE_CUTOFF) {
            return true;
        }

        // look at bottom portion of bitmap
        whiteCount = 0;
        for (int y = (int) (smallBitmap.getHeight() * (1 - HEIGHT_PERCENT_ANALYZED)); y < smallBitmap
                .getHeight(); y++) {
            for (int x = 0; x < smallBitmap.getWidth(); x++) {
                final int rgb = rgbPixels[y * smallBitmap.getWidth() + x];
                if (isWhite(rgb)) {
                    whiteCount++;
                }
            }
        }

        totalPixels = (int) (smallBitmap.getHeight() * smallBitmap.getWidth() * HEIGHT_PERCENT_ANALYZED);

        return whiteCount / (float) totalPixels > PROPORTION_WHITE_CUTOFF;
    } finally {
        if (Build.VERSION.SDK_INT >= 18) {
            Trace.endSection();
        }
    }
}