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 getBlur(Bitmap bitmap) {
    int iterations = 1;
    int radius = 8;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int[] inPixels = new int[width * height];
    int[] outPixels = new int[width * height];
    Bitmap blured = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    bitmap.getPixels(inPixels, 0, width, 0, 0, width, height);
    for (int i = 0; i < iterations; i++) {
        blur(inPixels, outPixels, width, height, radius);
        blur(outPixels, inPixels, height, width, radius);
    }/*from   w w  w.  j  a v  a 2 s  . co  m*/
    blured.setPixels(inPixels, 0, width, 0, 0, width, height);
    return blured;
}

From source file:Main.java

public static Bitmap createBitmapWithAlphaMask(Bitmap bmpSource, Bitmap bmpMask) {
    int width = bmpSource.getWidth();
    int height = bmpSource.getHeight();
    int size = width * height;

    if (width != bmpMask.getWidth() || height != bmpMask.getHeight())
        bmpMask = resize(bmpMask, width, height);

    int[] result = new int[size];
    int[] mask = new int[size];
    bmpSource.getPixels(result, 0, width, 0, 0, width, height);
    bmpMask.getPixels(mask, 0, width, 0, 0, width, height);

    int alphaMask = 0xff000000;
    int colorMask = 0x00ffffff;
    for (int i = 0; i < size; i++) {
        result[i] = (mask[i] & alphaMask) | (result[i] & colorMask);
    }//www .  j av a 2s . c  om

    // ensuring the bitmap is mutable
    Bitmap bmpResult = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    bmpResult.setPixels(result, 0, width, 0, 0, width, height);

    return bmpResult;
}

From source file:Main.java

public static Bitmap handleImagePixelsRelief(Bitmap bm) {
    Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
    int width = bm.getWidth();
    int height = bm.getHeight();
    int color = 0, colorBefore = 0;
    int a, r, g, b;
    int r1, g1, b1;

    int[] oldPx = new int[width * height];
    int[] newPx = new int[width * height];

    bm.getPixels(oldPx, 0, bm.getWidth(), 0, 0, width, height);
    for (int i = 1; i < width * height; i++) {
        colorBefore = oldPx[i - 1];/*w w w  .j  av  a2 s.co m*/
        a = Color.alpha(colorBefore);
        r = Color.red(colorBefore);
        g = Color.green(colorBefore);
        b = Color.blue(colorBefore);

        color = oldPx[i];
        r1 = Color.red(color);
        g1 = Color.green(color);
        b1 = Color.blue(color);

        r = (r - r1 + 127);
        g = (g - g1 + 127);
        b = (b - b1 + 127);
        if (r > 255) {
            r = 255;
        }
        if (g > 255) {
            g = 255;
        }
        if (b > 255) {
            b = 255;
        }
        newPx[i] = Color.argb(a, r, g, b);
    }
    bmp.setPixels(newPx, 0, width, 0, 0, width, height);
    return bmp;
}

From source file:Main.java

public static Bitmap nostalgic(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Bitmap newBitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
    int pixColor = 0;
    int pixR = 0;
    int pixG = 0;
    int pixB = 0;
    int newR = 0;
    int newG = 0;
    int newB = 0;
    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
    for (int i = 0; i < height; i++) {
        for (int k = 0; k < width; k++) {
            pixColor = pixels[width * i + k];
            pixR = Color.red(pixColor);
            pixG = Color.green(pixColor);
            pixB = Color.blue(pixColor);
            newR = (int) (0.393 * pixR + 0.769 * pixG + 0.189 * pixB);
            newG = (int) (0.349 * pixR + 0.686 * pixG + 0.168 * pixB);
            newB = (int) (0.272 * pixR + 0.534 * pixG + 0.131 * pixB);
            int newColor = Color.argb(255, newR > 255 ? 255 : newR, newG > 255 ? 255 : newG,
                    newB > 255 ? 255 : newB);
            pixels[width * i + k] = newColor;
        }//from   ww w  .ja va  2 s . com
    }
    newBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return newBitmap;
}

From source file:Main.java

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

    int pixR = 0;
    int pixG = 0;
    int pixB = 0;

    int pixColor = 0;

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

    int[] pixels = new int[width * height];
    bmp.getPixels(pixels, 0, width, 0, 0, width, height);
    int pos = 0;//w w  w. j  a  v a 2 s.c  om
    for (int i = 1, length = height - 1; i < length; i++) {
        for (int k = 1, len = width - 1; k < len; k++) {
            pos = i * width + k;
            pixColor = pixels[pos];

            pixR = Color.red(pixColor);
            pixG = Color.green(pixColor);
            pixB = Color.blue(pixColor);

            pixColor = pixels[pos + 1];
            newR = Color.red(pixColor) - pixR + 127;
            newG = Color.green(pixColor) - pixG + 127;
            newB = Color.blue(pixColor) - pixB + 127;

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

            pixels[pos] = Color.argb(255, newR, newG, newB);
        }
    }

    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}

From source file:android.support.v7.testutils.TestUtils.java

/**
 * Checks whether all the pixels in the specified bitmap are of the same specified color.
 *
 * In case there is a color mismatch, the behavior of this method depends on the
 * <code>throwExceptionIfFails</code> parameter. If it is <code>true</code>, this method will
 * throw an <code>Exception</code> describing the mismatch. Otherwise this method will call
 * <code>Assert.fail</code> with detailed description of the mismatch.
 *///from  w w  w  .j ava 2  s .  c  o  m
public static void assertAllPixelsOfColor(String failMessagePrefix, @NonNull Bitmap bitmap, int bitmapWidth,
        int bitmapHeight, @ColorInt int color, int allowedComponentVariance, boolean throwExceptionIfFails) {
    int[] rowPixels = new int[bitmapWidth];
    for (int row = 0; row < bitmapHeight; row++) {
        bitmap.getPixels(rowPixels, 0, bitmapWidth, 0, row, bitmapWidth, 1);
        for (int column = 0; column < bitmapWidth; column++) {
            int sourceAlpha = Color.alpha(rowPixels[column]);
            int sourceRed = Color.red(rowPixels[column]);
            int sourceGreen = Color.green(rowPixels[column]);
            int sourceBlue = Color.blue(rowPixels[column]);

            int expectedAlpha = Color.alpha(color);
            int expectedRed = Color.red(color);
            int expectedGreen = Color.green(color);
            int expectedBlue = Color.blue(color);

            int varianceAlpha = Math.abs(sourceAlpha - expectedAlpha);
            int varianceRed = Math.abs(sourceRed - expectedRed);
            int varianceGreen = Math.abs(sourceGreen - expectedGreen);
            int varianceBlue = Math.abs(sourceBlue - expectedBlue);

            boolean isColorMatch = (varianceAlpha <= allowedComponentVariance)
                    && (varianceRed <= allowedComponentVariance) && (varianceGreen <= allowedComponentVariance)
                    && (varianceBlue <= allowedComponentVariance);

            if (!isColorMatch) {
                String mismatchDescription = failMessagePrefix + ": expected all drawable colors to be ["
                        + expectedAlpha + "," + expectedRed + "," + expectedGreen + "," + expectedBlue
                        + "] but at position (" + row + "," + column + ") out of (" + bitmapWidth + ","
                        + bitmapHeight + ") found [" + sourceAlpha + "," + sourceRed + "," + sourceGreen + ","
                        + sourceBlue + "]";
                if (throwExceptionIfFails) {
                    throw new RuntimeException(mismatchDescription);
                } else {
                    Assert.fail(mismatchDescription);
                }
            }
        }
    }
}

From source file:Main.java

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

    int pixR = 0;
    int pixG = 0;
    int pixB = 0;

    int pixColor = 0;

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

    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
    int pos = 0;/*from   ww w . j a  va2  s . c o  m*/
    for (int i = 1, length = height - 1; i < length; i++) {
        for (int k = 1, len = width - 1; k < len; k++) {
            pos = i * width + k;
            pixColor = pixels[pos];

            pixR = Color.red(pixColor);
            pixG = Color.green(pixColor);
            pixB = Color.blue(pixColor);

            pixColor = pixels[pos + 1];
            newR = Color.red(pixColor) - pixR + 127;
            newG = Color.green(pixColor) - pixG + 127;
            newB = Color.blue(pixColor) - pixB + 127;

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

            pixels[pos] = Color.argb(255, newR, newG, newB);
        }
    }

    newBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return newBitmap;
}

From source file:Main.java

public static Bitmap cropCenterInside(Bitmap src, Rect rect) {
    int width = Math.min(src.getWidth(), rect.width());
    int height = Math.min(src.getHeight(), rect.height());
    int[] rowData = new int[width];
    int stride = src.getWidth();
    int x = (src.getWidth() - width) / 2;
    int y = (src.getHeight() - height) / 2;

    Bitmap dest = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

    int delta = 0;
    while (delta < height) {
        src.getPixels(rowData, 0, stride, x, y + delta, width, 1);
        dest.setPixels(rowData, 0, stride, 0, delta, width, 1);
        delta++;//from  ww w  .ja va2 s.c o m
    }
    return dest;
}

From source file:jahirfiquitiva.iconshowcase.utilities.utils.Utils.java

@SuppressWarnings("ConstantConditions")
public static Bitmap getWidgetPreview(@NonNull Bitmap bitmap, @ColorInt int colorToReplace) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);

    int minX = width;
    int minY = height;
    int maxX = -1;
    int maxY = -1;

    Bitmap newBitmap = Bitmap.createBitmap(width, height, bitmap.getConfig());
    int pixel;//from  ww w .j a v  a2  s .  c  o m

    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            int index = y * width + x;
            pixel = pixels[index];
            if (pixel == colorToReplace) {
                pixels[index] = android.graphics.Color.TRANSPARENT;
            }
            if (pixels[index] != android.graphics.Color.TRANSPARENT) {
                if (x < minX)
                    minX = x;
                if (x > maxX)
                    maxX = x;
                if (y < minY)
                    minY = y;
                if (y > maxY)
                    maxY = y;
            }
        }
    }

    newBitmap.setPixels(pixels, 0, width, 0, 0, width, height);

    return Bitmap.createBitmap(newBitmap, minX, minY, (maxX - minX) + 1, (maxY - minY) + 1);
}

From source file:org.thezero.qrfi.MainActivity.java

public static void handleDecodeImage(Uri imageUri) {
    if (imageUri != null) {
        try {//from   ww w.ja  va2s.c o m
            InputStream inputStream = c.getContentResolver().openInputStream(imageUri);
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            if (bitmap == null) {
                Log.e(TAG, "uri is not a bitmap," + imageUri.toString());
                return;
            }
            int width = bitmap.getWidth(), height = bitmap.getHeight();
            int[] pixels = new int[width * height];
            bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
            bitmap.recycle();
            //noinspection UnusedAssignment
            bitmap = null;
            RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
            BinaryBitmap bBitmap = new BinaryBitmap(new HybridBinarizer(source));
            MultiFormatReader reader = new MultiFormatReader();
            try {
                Result result = reader.decode(bBitmap);
                //Toast.makeText(c, result.getText(), Toast.LENGTH_SHORT).show();
                SuperAwesomeCardFragment.connectAP(result);
            } catch (NotFoundException e) {
                Toast.makeText(c, c.getString(R.string.error_decode), Toast.LENGTH_SHORT).show();
            }
        } catch (FileNotFoundException e) {
            Log.e(TAG, "can not open file" + imageUri.toString(), e);
        }
    }
}