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:com.ze.client.projecto.utils.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 . ja  va2s . co m
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).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 cropMaxVisibleBitmap(Drawable drawable, int iconSize) {
    Bitmap tmp = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            Bitmap.Config.ARGB_8888);//from  ww w .j  ava2  s  .  c om
    Canvas canvas = new Canvas(tmp);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    Rect crop = new Rect(tmp.getWidth(), tmp.getHeight(), -1, -1);
    for (int y = 0; y < tmp.getHeight(); y++) {
        for (int x = 0; x < tmp.getWidth(); x++) {
            int alpha = (tmp.getPixel(x, y) >> 24) & 255;
            if (alpha > 0) { // pixel is not 100% transparent
                if (x < crop.left)
                    crop.left = x;
                if (x > crop.right)
                    crop.right = x;
                if (y < crop.top)
                    crop.top = y;
                if (y > crop.bottom)
                    crop.bottom = y;
            }
        }
    }

    if (crop.width() <= 0 || crop.height() <= 0) {
        return Bitmap.createScaledBitmap(tmp, iconSize, iconSize, true);
    }

    // We want to crop a square region.
    float size = Math.max(crop.width(), crop.height());
    float xShift = (size - crop.width()) * 0.5f;
    crop.left -= Math.floor(xShift);
    crop.right += Math.ceil(xShift);

    float yShift = (size - crop.height()) * 0.5f;
    crop.top -= Math.floor(yShift);
    crop.bottom += Math.ceil(yShift);

    Bitmap finalImage = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888);
    canvas.setBitmap(finalImage);
    float scale = iconSize / size;

    canvas.scale(scale, scale);
    canvas.drawBitmap(tmp, -crop.left, -crop.top, new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
    canvas.setBitmap(null);
    return finalImage;
}

From source file:io.romain.passport.utils.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  ww w.  j ava2s .  co  m
private static boolean isDark(@NonNull Bitmap bitmap, int backupPixelX, int backupPixelY) {
    // first try palette with a small color quant size
    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:app.witness.com.myapplication.plaint.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 . j  a v a2 s. c  o m
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).maximumColorCount(3).generate();
    if (palette != null && 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

/**
 * @param bitmap Bitmap that will be processed
 * @return The average color of the input bitmap
 */// w  ww . ja v  a 2 s .co  m
public static int getAverageColor(Bitmap bitmap) {
    if (bitmap == null) {
        Log.e("getAverageColor()", "ERROR: No bitmap generated to get average colour from.");
        return Color.BLACK;
    }

    int redBucket = 0;
    int greenBucket = 0;
    int blueBucket = 0;

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int pixelCount = 0;
    int c = 0;

    for (int y = 0; y < height; y += STEP_SIZE_PIXEL) {
        for (int x = 0; x < width; x += STEP_SIZE_PIXEL) {
            c = bitmap.getPixel(x, y);

            redBucket += Color.red(c);
            greenBucket += Color.green(c);
            blueBucket += Color.blue(c);
            pixelCount++;
        }
        redBucket += Color.red(c);
    }

    return Color.rgb(redBucket / pixelCount, greenBucket / pixelCount, blueBucket / pixelCount);
}

From source file:Main.java

/**
 * Converts a bitmap image to LCD screen data and returns the screen data as bytes.
 * @param buffer The screen's data buffer.
 * @param offset The byte offset to start writing screen bitmap data at.
 * @param bmp The bitmap image that you want to convert to screen data.
 * @param drawWhite Set to true to draw white pixels, false to draw pixels based on gradient.
 * @return A byte array with pixel data for the SSD1306.
 *///from   w  w w . jav  a  2 s .  c om
public static void bmpToBytes(byte[] buffer, int offset, Bitmap bmp, boolean drawWhite) {
    int width = bmp.getWidth();
    int height = bmp.getHeight();

    // Each byte stored in memory represents 8 vertical pixels.  As such, you must fill the
    // memory with pixel data moving vertically top-down through the image and scrolling
    // across, while appending the vertical pixel data by series of 8.
    for (int y = 0; y < height; y += 8) {
        for (int x = 0; x < width; x++) {
            int bytePos = (offset + x) + ((y / 8) * width);

            for (int k = 0; k < 8; k++) {
                if ((k + y < height) && (bytePos < buffer.length)) {
                    int pixel = bmp.getPixel(x, y + k);
                    if (!drawWhite) { // Look at Alpha channel instead
                        if ((pixel & 0xFF) > GRADIENT_CUTOFF) {
                            buffer[bytePos] |= 1 << k;
                        }
                    } else {
                        if (pixel == -1) { // Only draw white pixels
                            buffer[bytePos] |= 1 << k;
                        }
                    }
                }
            }
        }
    }
}

From source file:io.jawg.osmcontributor.ui.utils.BitmapHandler.java

public static Bitmap hue(Bitmap bitmap, float hue) {
    Bitmap newBitmap = bitmap.copy(bitmap.getConfig(), true);
    final int width = newBitmap.getWidth();
    final int height = newBitmap.getHeight();
    float[] hsv = new float[3];

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int pixel = newBitmap.getPixel(x, y);
            Color.colorToHSV(pixel, hsv);
            hsv[0] = hue;/*from w w w .  ja  v a  2s . c o m*/
            newBitmap.setPixel(x, y, Color.HSVToColor(Color.alpha(pixel), hsv));
        }
    }

    return newBitmap;
}

From source file:Main.java

/**
 * This picks a dominant color, looking for high-saturation, high-value, repeated hues.
 * @param bitmap The bitmap to scan/*  w ww.j a  v  a2  s . c  o  m*/
 * @param samples The approximate max number of samples to use.
 */
static int findDominantColorByHue(Bitmap bitmap, int samples) {
    final int height = bitmap.getHeight();
    final int width = bitmap.getWidth();
    int sampleStride = (int) Math.sqrt((height * width) / samples);
    if (sampleStride < 1) {
        sampleStride = 1;
    }

    // This is an out-param, for getting the hsv values for an rgb
    float[] hsv = new float[3];

    // First get the best hue, by creating a histogram over 360 hue buckets,
    // where each pixel contributes a score weighted by saturation, value, and alpha.
    float[] hueScoreHistogram = new float[360];
    float highScore = -1;
    int bestHue = -1;

    for (int y = 0; y < height; y += sampleStride) {
        for (int x = 0; x < width; x += sampleStride) {
            int argb = bitmap.getPixel(x, y);
            int alpha = 0xFF & (argb >> 24);
            if (alpha < 0x80) {
                // Drop mostly-transparent pixels.
                continue;
            }
            // Remove the alpha channel.
            int rgb = argb | 0xFF000000;
            Color.colorToHSV(rgb, hsv);
            // Bucket colors by the 360 integer hues.
            int hue = (int) hsv[0];
            if (hue < 0 || hue >= hueScoreHistogram.length) {
                // Defensively avoid array bounds violations.
                continue;
            }
            float score = hsv[1] * hsv[2];
            hueScoreHistogram[hue] += score;
            if (hueScoreHistogram[hue] > highScore) {
                highScore = hueScoreHistogram[hue];
                bestHue = hue;
            }
        }
    }

    SparseArray<Float> rgbScores = new SparseArray<Float>();
    int bestColor = 0xff000000;
    highScore = -1;
    // Go back over the RGB colors that match the winning hue,
    // creating a histogram of weighted s*v scores, for up to 100*100 [s,v] buckets.
    // The highest-scoring RGB color wins.
    for (int y = 0; y < height; y += sampleStride) {
        for (int x = 0; x < width; x += sampleStride) {
            int rgb = bitmap.getPixel(x, y) | 0xff000000;
            Color.colorToHSV(rgb, hsv);
            int hue = (int) hsv[0];
            if (hue == bestHue) {
                float s = hsv[1];
                float v = hsv[2];
                int bucket = (int) (s * 100) + (int) (v * 10000);
                // Score by cumulative saturation * value.
                float score = s * v;
                Float oldTotal = rgbScores.get(bucket);
                float newTotal = oldTotal == null ? score : oldTotal + score;
                rgbScores.put(bucket, newTotal);
                if (newTotal > highScore) {
                    highScore = newTotal;
                    // All the colors in the winning bucket are very similar. Last in wins.
                    bestColor = rgb;
                }
            }
        }
    }
    return bestColor;
}

From source file:Main.java

public static Bitmap createPOT(Bitmap bmp) {
    Bitmap potBmp = null;//from  w  ww  .java  2  s  . c o m
    int potWidth = (int) Math.ceil(Math.log(bmp.getWidth()) / Math.log(2));
    int potHeight = (int) Math.ceil(Math.log(bmp.getHeight()) / Math.log(2));
    potHeight = (int) Math.pow(2, potHeight);
    potWidth = (int) Math.pow(2, potWidth);

    if (potWidth == 1) {
        potWidth = 2;
    }
    if (potHeight == 1) {
        potHeight = 2;
    }

    if (potHeight != bmp.getHeight() || potWidth != bmp.getWidth()) {
        int[] colors = new int[potWidth * potHeight];
        int index = 0;

        int offset = potHeight - bmp.getHeight();
        for (int i = 0; i < potHeight; i++) {
            for (int j = 0; j < potWidth; j++) {
                if (i > offset - 1) {
                    if (j < bmp.getWidth() && i < bmp.getHeight()) {
                        colors[index] = bmp.getPixel(j, i);
                    } else {
                        colors[index] = 0;
                    }
                }
                index++;
            }
        }

        potBmp = Bitmap.createBitmap(colors, potWidth, potHeight, bmp.getConfig());
    } else {
        potBmp = bmp;
    }

    System.gc();

    return potBmp;
}

From source file:Main.java

public static Bitmap getMosaic(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int radius = 10;

    Bitmap mosaicBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(mosaicBitmap);

    int horCount = (int) Math.ceil(width / (float) radius);
    int verCount = (int) Math.ceil(height / (float) radius);

    Paint paint = new Paint();
    paint.setAntiAlias(true);// w  ww  .  jav  a2s.c  o m

    for (int horIndex = 0; horIndex < horCount; ++horIndex) {
        for (int verIndex = 0; verIndex < verCount; ++verIndex) {
            int l = radius * horIndex;
            int t = radius * verIndex;
            int r = l + radius;
            if (r > width) {
                r = width;
            }
            int b = t + radius;
            if (b > height) {
                b = height;
            }
            int color = bitmap.getPixel(l, t);
            Rect rect = new Rect(l, t, r, b);
            paint.setColor(color);
            canvas.drawRect(rect, paint);
        }
    }
    canvas.save();

    return mosaicBitmap;
}