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.ferdi2005.secondgram.AndroidUtilities.java

public static int[] calcDrawableColor(Drawable drawable) {
    int bitmapColor = 0xff000000;
    int result[] = new int[2];
    try {//from  w w  w .  ja va  2 s. c o m
        if (drawable instanceof BitmapDrawable) {
            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
            if (bitmap != null) {
                Bitmap b = Bitmaps.createScaledBitmap(bitmap, 1, 1, true);
                if (b != null) {
                    bitmapColor = b.getPixel(0, 0);
                    if (bitmap != b) {
                        b.recycle();
                    }
                }
            }
        } else if (drawable instanceof ColorDrawable) {
            bitmapColor = ((ColorDrawable) drawable).getColor();
        }
    } catch (Exception e) {
        FileLog.e(e);
    }

    double[] hsv = rgbToHsv((bitmapColor >> 16) & 0xff, (bitmapColor >> 8) & 0xff, bitmapColor & 0xff);
    hsv[1] = Math.min(1.0, hsv[1] + 0.05 + 0.1 * (1.0 - hsv[1]));
    hsv[2] = Math.max(0, hsv[2] * 0.65);
    int rgb[] = hsvToRgb(hsv[0], hsv[1], hsv[2]);
    result[0] = Color.argb(0x66, rgb[0], rgb[1], rgb[2]);
    result[1] = Color.argb(0x88, rgb[0], rgb[1], rgb[2]);
    return result;
}

From source file:com.creativeongreen.imageeffects.MainActivity.java

public static Bitmap invert(Bitmap bmImage) {
    Bitmap bmTemp = Bitmap.createBitmap(bmImage.getWidth(), bmImage.getHeight(), bmImage.getConfig());

    for (int i = 0; i < bmImage.getWidth(); i++) {
        for (int j = 0; j < bmImage.getHeight(); j++) {
            int p = bmImage.getPixel(i, j);
            bmTemp.setPixel(i, j, (p & 0xff000000) | (~p & 0x00ffffff));
        }/*from  w ww .ja  v  a  2 s .  c om*/
    }

    return bmTemp;
}

From source file:com.creativeongreen.imageeffects.MainActivity.java

public static Bitmap bright(Bitmap bmImage, int brightness) {
    Bitmap bmTemp = Bitmap.createBitmap(bmImage.getWidth(), bmImage.getHeight(), bmImage.getConfig());

    for (int i = 0; i < bmImage.getWidth(); i++) {
        for (int j = 0; j < bmImage.getHeight(); j++) {
            int p = bmImage.getPixel(i, j);
            int r = Color.red(p);
            int g = Color.green(p);
            int b = Color.blue(p);
            int alpha = Color.alpha(p);

            r += brightness;//w  w w  .  j  av a2s  .  co  m
            g += brightness;
            b += brightness;
            alpha += brightness;
            r = r < 0 ? 0 : (r > 255 ? 255 : r);
            g = g < 0 ? 0 : (g > 255 ? 255 : g);
            b = b < 0 ? 0 : (b > 255 ? 255 : b);
            alpha = alpha < 0 ? 0 : (alpha > 255 ? 255 : alpha);

            bmTemp.setPixel(i, j, Color.argb(alpha, r, g, b));
        }
    }

    return bmTemp;
}

From source file:Main.java

public static Bitmap blur(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

    int pixColor = 0;

    int newR = 0;
    int newG = 0;
    int newB = 0;

    int newColor = 0;

    int[][] colors = new int[9][3];
    for (int i = 1, length = width - 1; i < length; i++) {
        for (int k = 1, len = height - 1; k < len; k++) {
            for (int m = 0; m < 9; m++) {
                int s = 0;
                int p = 0;
                switch (m) {
                case 0:
                    s = i - 1;/*from  www  . j a  v a  2 s. c  o  m*/
                    p = k - 1;
                    break;
                case 1:
                    s = i;
                    p = k - 1;
                    break;
                case 2:
                    s = i + 1;
                    p = k - 1;
                    break;
                case 3:
                    s = i + 1;
                    p = k;
                    break;
                case 4:
                    s = i + 1;
                    p = k + 1;
                    break;
                case 5:
                    s = i;
                    p = k + 1;
                    break;
                case 6:
                    s = i - 1;
                    p = k + 1;
                    break;
                case 7:
                    s = i - 1;
                    p = k;
                    break;
                case 8:
                    s = i;
                    p = k;
                }
                pixColor = bitmap.getPixel(s, p);
                colors[m][0] = Color.red(pixColor);
                colors[m][1] = Color.green(pixColor);
                colors[m][2] = Color.blue(pixColor);
            }

            for (int m = 0; m < 9; m++) {
                newR += colors[m][0];
                newG += colors[m][1];
                newB += colors[m][2];
            }

            newR = (int) (newR / 9F);
            newG = (int) (newG / 9F);
            newB = (int) (newB / 9F);

            newR = Math.min(255, Math.max(0, newR));
            newG = Math.min(255, Math.max(0, newG));
            newB = Math.min(255, Math.max(0, newB));

            newColor = Color.argb(255, newR, newG, newB);
            newBitmap.setPixel(i, k, newColor);

            newR = 0;
            newG = 0;
            newB = 0;
        }
    }
    return newBitmap;
}

From source file:com.atwal.wakeup.battery.util.Utilities.java

/**
 * This picks a dominant color, looking for high-saturation, high-value, repeated hues.
 *
 * @param bitmap  The bitmap to scan//from  www  .j av  a2  s  .c o m
 * @param samples The approximate max number of samples to use.
 */
public 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 theme_icon_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:com.creativeongreen.imageeffects.MainActivity.java

public static Bitmap gray(Bitmap bmImage) {
    Bitmap bmTemp = Bitmap.createBitmap(bmImage.getWidth(), bmImage.getHeight(), bmImage.getConfig());

    // The W3C Algorithm
    double intensityRed = 0.299;
    double intensityGreen = 0.587;
    double intensityBlue = 0.114;

    for (int i = 0; i < bmImage.getWidth(); i++) {
        for (int j = 0; j < bmImage.getHeight(); j++) {
            int p = bmImage.getPixel(i, j);
            int r = Color.red(p);
            int g = Color.green(p);
            int b = Color.blue(p);

            /*//w  w w  .  j  a  v  a  2  s.  c  o  m
             * r = g = b = (int) (r * intensityRed + g * intensityGreen + b intensityBlue);
             * 
             * bmTemp.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
             */
            // int rgb = (r * 77 + g * 151 + b * 28) >> 8; // NTSC luma
            int rgb = (int) Math.sqrt(r * r * 0.241 + g * g * 0.691 + b * b * 0.068); // HSP, where the P stands for Perceived brightness
            bmTemp.setPixel(i, j, (p & 0xff000000) | (rgb << 16) | (rgb << 8) | rgb);
        }
    }

    return bmTemp;
}

From source file:com.creativeongreen.imageeffects.MainActivity.java

public static Bitmap tint(Bitmap bmImage, int degree) {
    Bitmap bmTemp = Bitmap.createBitmap(bmImage.getWidth(), bmImage.getHeight(), bmImage.getConfig());

    double angle = (3.14159d * (double) degree) / 180.0d;
    int S = (int) (256.0d * Math.sin(angle));
    int C = (int) (256.0d * Math.cos(angle));

    for (int i = 0; i < bmImage.getWidth(); i++) {
        for (int j = 0; j < bmImage.getHeight(); j++) {
            int p = bmImage.getPixel(i, j);
            int r = Color.red(p);
            int g = Color.green(p);
            int b = Color.blue(p);
            // int alpha = Color.alpha(p);

            int RY = (70 * r - 59 * g - 11 * b) / 100;
            int BY = (-30 * r - 59 * g + 89 * b) / 100;
            int Y = (30 * r + 59 * g + 11 * b) / 100;
            int RYY = (S * BY + C * RY) / 256;
            int BYY = (C * BY - S * RY) / 256;
            int GYY = (-51 * RYY - 19 * BYY) / 100;
            r = Y + RYY;/*from w w  w .j a  v  a  2 s  .  c  o m*/
            r = (r < 0) ? 0 : ((r > 255) ? 255 : r);
            g = Y + GYY;
            g = (g < 0) ? 0 : ((g > 255) ? 255 : g);
            b = Y + BYY;
            b = (b < 0) ? 0 : ((b > 255) ? 255 : b);
            bmTemp.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
        }
    }

    return bmTemp;
}

From source file:com.sim2dial.dialer.ChatFragment.java

private long hashBitmap(Bitmap bmp) {
    long hash = 31; // Random prime number
    for (int x = 0; x < bmp.getWidth(); x++) {
        for (int y = 0; y < bmp.getHeight(); y++) {
            hash *= (bmp.getPixel(x, y) + 31);
        }// w w  w  . jav a  2 s.  com
    }
    return hash;
}

From source file:com.egloos.hyunyi.musicinfo.LinkPopUp.java

private void adjBottomColor(Drawable d) {
    Bitmap b = ((BitmapDrawable) d).getBitmap();
    int b_sum = 0;
    int g_sum = 0;
    int r_sum = 0;

    for (int i = 0; i < 10; i++) {
        int x = (int) ((double) b.getWidth() * Math.random());
        int y = (int) ((double) b.getHeight() * Math.random());
        int pixel = b.getPixel(x, y);

        int redValue = Color.red(pixel);
        int blueValue = Color.blue(pixel);
        int greenValue = Color.green(pixel);

        b_sum += blueValue;//from ww  w.  j a  va  2s  . co m
        g_sum += greenValue;
        r_sum += redValue;
    }

    int av_r = r_sum / 10;
    int av_g = g_sum / 10;
    int av_b = b_sum / 10;

    iBottomPanel.setBackgroundColor(Color.rgb(av_r, av_g, av_b));
    ArtistImage.setBackgroundColor(Color.rgb(av_r, av_g, av_b));

    int ave_sum = (av_b + av_g + av_r);
    Log.d("musicInfo", "ave_sum = " + ave_sum);

    if (ave_sum > 400) {
        tEchoNest.setTextColor(Color.parseColor("#8f363c6b"));
        for (int i = 0; i < lLinkList.getChildCount(); i++) {
            ((TextView) lLinkList.getChildAt(i)).setTextColor(Color.DKGRAY);
        }
    } else {
        tEchoNest.setTextColor(Color.parseColor("#8fc9cfff"));
        for (int i = 0; i < lLinkList.getChildCount(); i++) {
            ((TextView) lLinkList.getChildAt(i)).setTextColor(Color.LTGRAY);
        }
    }
}

From source file:com.microsoft.projectoxford.face.samples.ui.Camera2BasicFragment.java

boolean checkb(Bitmap bitm, Bitmap bitn) {
    double sum = 0;
    double mean = 0;
    double d[][] = new double[result[0].faceRectangle.width][result[0].faceRectangle.height];
    for (int i = 0; i < result[0].faceRectangle.width; i++) {
        for (int j = 0; j < result[0].faceRectangle.height; j++) {
            int cm = bitm.getPixel(result[0].faceRectangle.left + i, result[0].faceRectangle.top + j);
            int cn = bitn.getPixel(result[0].faceRectangle.left + i, result[0].faceRectangle.top + j);
            double gm = Color.red(cm) * 0.299 + Color.green(cm) * 0.587 + Color.blue(cm) * 0.114;
            double gn = Color.red(cn) * 0.299 + Color.green(cn) * 0.587 + Color.blue(cn) * 0.114;
            d[i][j] = gn - gm;//from   w w w  .j  av  a 2  s  .c  om
            mean += d[i][j];
        }
    }
    mean /= result[0].faceRectangle.width * result[0].faceRectangle.height;
    for (int i = 0; i < result[0].faceRectangle.width; i++) {
        for (int j = 0; j < result[0].faceRectangle.height; j++) {
            sum += (d[i][j] - mean) * (d[i][j] - mean);
        }
    }
    sum /= result[0].faceRectangle.width * result[0].faceRectangle.height;
    sum = Math.sqrt(sum);
    Log.i("Human", "std=" + sum);
    return sum < 45;
}