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:org.zoumbox.mh_dla_notifier.MhDlaNotifierUtils.java

protected static Bitmap loadAndCropBlason(String blasonUrl, File filesDir) {

    Bitmap result = null;/*w ww.  j av a2s .com*/
    if (!Strings.isNullOrEmpty(blasonUrl)) {
        String fileName = BLASON_FILES_PREFIX + MhDlaNotifierUtils.md5(blasonUrl) + "_cropped";
        File localFile = new File(filesDir, fileName);
        Log.i(TAG, "localFile: " + localFile);
        if (!localFile.exists()) {

            Bitmap rawBlason = loadRawBlason(blasonUrl, filesDir);

            if (rawBlason != null) {
                int minX = Integer.MAX_VALUE;
                int minY = Integer.MAX_VALUE;
                int maxX = Integer.MIN_VALUE;
                int maxY = Integer.MIN_VALUE;
                for (int x = 0; x < rawBlason.getWidth(); x++) {
                    for (int y = 0; y < rawBlason.getHeight(); y++) {
                        int pixel = rawBlason.getPixel(x, y);
                        int alpha = (pixel >> 24) & 0xff;
                        boolean transparent = alpha == 0;
                        if (!transparent) {
                            minX = Math.min(minX, x);
                            minY = Math.min(minY, y);
                            maxX = Math.max(maxX, x);
                            maxY = Math.max(maxY, y);
                        }
                    }
                }

                int cropMinX = Math.max(0, minX - CROP_PADDING);
                int cropMinY = Math.max(0, minY - CROP_PADDING);
                int cropMaxX = Math.min(rawBlason.getWidth() - 1, maxX + CROP_PADDING);
                int cropMaxY = Math.min(rawBlason.getHeight() - 1, maxY + CROP_PADDING);

                Log.i(TAG,
                        "Size info: " + rawBlason.getWidth() + "x" + rawBlason.getHeight() + " ; "
                                + "Alpha info: min=" + minX + "x" + minY + " ; max=" + maxX + "x" + maxY + " ; "
                                + "Crop info: min=" + cropMinX + "x" + cropMinY + " ; max=" + cropMaxX + "x"
                                + cropMaxY);

                if (cropMinX > 0 || cropMinY > 0 || cropMaxX < rawBlason.getWidth()
                        || cropMaxY < rawBlason.getHeight()) {

                    result = Bitmap.createBitmap(rawBlason, cropMinX, cropMinY, cropMaxX - cropMinX,
                            cropMaxY - cropMinY);

                    Log.i(TAG, "Save cropped result to " + localFile);
                    FileOutputStream fos = null;
                    try {
                        fos = new FileOutputStream(localFile);

                        result.compress(Bitmap.CompressFormat.PNG, 90, fos);

                    } catch (Exception eee) {
                        Log.e(TAG, "Exception", eee);
                    } finally {
                        closeQuitely(fos);
                    }
                }
            }
        } else {

            Log.i(TAG, "Existing, loading from cache");
            BufferedInputStream bis = null;
            try {
                bis = new BufferedInputStream(new FileInputStream(localFile));

                result = BitmapFactory.decodeStream(bis);

                bis.close();
            } catch (Exception eee) {
                Log.e(TAG, "Exception", eee);
            } finally {
                closeQuitely(bis);
            }
        }
    }
    return result;
}

From source file:org.y20k.transistor.helpers.ImageHelper.java

private int getDominantColor() {
    Bitmap onePixelBitmap = Bitmap.createScaledBitmap(mInputImage, 1, 1, false);
    int pixel = onePixelBitmap.getPixel(0, 0);

    int red = Color.red(pixel);
    int green = Color.green(pixel);
    int blue = Color.blue(pixel);

    return Color.argb(127, red, green, blue);
}

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

/**
 * remove bitmap white around white section by miao
 *//*from   w  w w.  ja  v a 2  s.co m*/
public static Bitmap removeAroundWhiteSection(Bitmap icon) {
    int xStart = 0;
    int xEnd = 0;
    int yStart = 0;
    int yEnd = 0;
    int width = icon.getWidth();
    int height = icon.getHeight();
    Bitmap copyIcon = icon.copy(Bitmap.Config.ARGB_8888, false);
    copyIcon = Bitmap.createScaledBitmap(copyIcon, width, 1, false);
    for (int i = 0; i < width; i++) {
        int rgb = copyIcon.getPixel(i, 0);
        if (rgb != 0 && xStart == 0) {
            xStart = i;
            continue;
        }
        if (rgb == 0 && xStart != 0) {
            xEnd = i;
        }
    }

    copyIcon = icon.copy(Bitmap.Config.ARGB_8888, false);
    copyIcon = Bitmap.createScaledBitmap(copyIcon, 1, height, false);
    for (int i = 0; i < height; i++) {
        int rgb = copyIcon.getPixel(0, i);
        if (rgb != 0 && yStart == 0) {
            yStart = i;
            continue;
        }
        if (rgb == 0 && yStart != 0) {
            yEnd = i;
        }
    }
    if (xStart < xEnd && yStart < yEnd) {
        icon = Bitmap.createBitmap(icon, xStart, yStart, xEnd - xStart, yEnd - yStart);
        Log.d(TAG, "success remove white section");
    } else {
        Log.d(TAG, "fail remove white section");
    }
    return icon;
}

From source file:io.bunnyblue.noticedog.app.apps.BaseApp.java

boolean shouldUseAsProfilePhoto(Bitmap bitmap) {
    if (bitmap == null) {
        return false;
    }/*from w  ww.j  a  va 2  s  .c  o  m*/
    boolean shouldUseAsProfilePhoto = false;
    try {
        if ((bitmap.getPixel(0, 0) & ViewCompat.MEASURED_STATE_MASK) == ViewCompat.MEASURED_STATE_MASK) {
            shouldUseAsProfilePhoto = true;
        } else {
            shouldUseAsProfilePhoto = false;
        }
    } catch (IllegalArgumentException e) {
    }
    return shouldUseAsProfilePhoto;
}

From source file:ru.jango.j0widget.imagebrowser.ImageBrowserView.java

public void setImageBitmap(Bitmap bmp) {
    bitmap = bmp;/* ww  w  . j a v  a2s . c o  m*/
    if (bitmap == null)
        return;

    viewport.set(0, 0, bitmap.getWidth(), bitmap.getHeight());
    bgPaint.setColor(bmp.getPixel(0, 0));

    ViewCompat.postInvalidateOnAnimation(this);
}

From source file:java_lang_programming.com.android_media_demo.article80.ImageFragment.java

/**
 * Bitmap??/* w  w w  . ja  v  a 2s.  c o m*/
 * ??
 * grayscale using Bitmap
 * SimpleMeanMethod
 */
private void grayScaleSimpleMeanMethod() {
    long start = System.currentTimeMillis();
    Bitmap mutableBitmap = getMutableBitmap();

    int width = mutableBitmap.getWidth();
    int height = mutableBitmap.getHeight();
    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {

            // Returns the Color at the specified location.
            int pixel = mutableBitmap.getPixel(i, j);

            int red = Color.red(pixel);
            int green = Color.green(pixel);
            int blue = Color.blue(pixel);

            int average = (red + green + blue) / 3;
            int gray_rgb = Color.rgb(average, average, average);

            mutableBitmap.setPixel(i, j, gray_rgb);
        }
    }
    imageView.setImageBitmap(mutableBitmap);
    long end = System.currentTimeMillis();
    Log.d(TAG, ((end - start) + "ms"));
}

From source file:java_lang_programming.com.android_media_demo.article80.ImageFragment.java

/**
 * Bitmap??/* w  w  w.  j ava2 s. c  o  m*/
 * NTSC ???
 * grayscale using Bitmap
 * NTSC Coef. method
 */
private void grayScaleNTSCCoefMethod() {
    long start = System.currentTimeMillis();
    Bitmap mutableBitmap = getMutableBitmap();

    int width = mutableBitmap.getWidth();
    int height = mutableBitmap.getHeight();
    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {

            // Returns the Color at the specified location.
            int pixel = mutableBitmap.getPixel(i, j);

            int red = Color.red(pixel);
            int green = Color.green(pixel);
            int blue = Color.blue(pixel);

            // RGB ??
            double gray_scale_value = 0.2989 * red + 0.5870 * green + 0.1140 * blue;
            int gray_scale = (int) gray_scale_value;

            int gray_rgb = Color.rgb(gray_scale, gray_scale, gray_scale);

            mutableBitmap.setPixel(i, j, gray_rgb);
        }
    }
    imageView.setImageBitmap(mutableBitmap);
    long end = System.currentTimeMillis();
    Log.d(TAG, ((end - start) + "ms"));
}

From source file:com.ifoer.util.NetPOSPrinter.java

public Bitmap bitmapFanzhuan(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    bitmap.getPixels(new int[(width * height)], 0, width, 0, 0, width, height);
    for (int i = 0; i < height; i += ERROR_PRINT_JAM) {
        for (int j = 0; j < width; j += ERROR_PRINT_JAM) {
            int grey = bitmap.getPixel(j, i);
            if (((((16711680 & grey) >> 16)
                    + ((MotionEventCompat.ACTION_POINTER_INDEX_MASK & grey) >> ERROR_PRINT_ACTUATOR_FAULT))
                    + (grey & KEYRecord.PROTOCOL_ANY)) / 3 < 100) {
                bitmap.setPixel(j, i, DefaultRenderer.BACKGROUND_COLOR);
            } else {
                bitmap.setPixel(j, i, -1);
            }/*  w ww . j  ava  2s . co m*/
        }
    }
    return bitmap;
}

From source file:kr.wdream.storyshop.AndroidUtilities.java

public static int[] calcDrawableColor(Drawable drawable) {
    int bitmapColor = 0xff000000;
    int result[] = new int[2];
    try {/*from  ww  w. j  a  v a2 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);
                    b.recycle();
                }
            }
        } else if (drawable instanceof ColorDrawable) {
            bitmapColor = ((ColorDrawable) drawable).getColor();
        }
    } catch (Exception e) {
        FileLog.e("tmessages", 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:ch.ethz.dcg.jukefox.manager.libraryimport.AndroidAlbumCoverFetcherThread.java

private int getColorFromBitmap(Bitmap bitmap) {
    int p1 = bitmap.getPixel(25, 25);
    int p2 = bitmap.getPixel(25, 75);
    int p3 = bitmap.getPixel(75, 25);
    int p4 = bitmap.getPixel(75, 75);
    int p5 = bitmap.getPixel(50, 50);

    int red = Color.red(p1) + Color.red(p2) + Color.red(p3) + Color.red(p4) + Color.red(p5);
    red /= 5;/*from   w  ww .j a v  a2 s.  c  o m*/

    int green = Color.green(p1) + Color.green(p2) + Color.green(p3) + Color.green(p4) + Color.green(p5);
    green /= 5;

    int blue = Color.blue(p1) + Color.blue(p2) + Color.blue(p3) + Color.blue(p4) + Color.blue(p5);
    blue /= 5;

    return Color.argb(255, red, green, blue);

}