Example usage for android.graphics Bitmap setPixels

List of usage examples for android.graphics Bitmap setPixels

Introduction

In this page you can find the example usage for android.graphics Bitmap setPixels.

Prototype

public void setPixels(@ColorInt int[] pixels, int offset, int stride, int x, int y, int width, int height) 

Source Link

Document

Replace pixels in the bitmap with the colors in the array.

Usage

From source file:org.rm3l.ddwrt.tiles.status.wireless.WirelessIfaceQrCodeActivity.java

@Nullable
private static Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int imgWidth, int imgHeight)
        throws WriterException {
    if (contents == null) {
        return null;
    }//  w  ww  .  j  a  v a  2s.co m
    Map<EncodeHintType, Object> hints = null;
    final String encoding = guessAppropriateEncoding(contents);
    if (encoding != null) {
        hints = new EnumMap<>(EncodeHintType.class);
        hints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    final MultiFormatWriter writer = new MultiFormatWriter();
    final BitMatrix result;
    try {
        result = writer.encode(contents, format, imgWidth, imgHeight, hints);
    } catch (IllegalArgumentException iae) {
        // Unsupported format
        return null;
    }
    final int width = result.getWidth();
    final int height = result.getHeight();
    final int[] pixels = new int[width * height];
    for (int y = 0; y < height; y++) {
        final int offset = y * width;
        for (int x = 0; x < width; x++) {
            pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
        }
    }

    final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);

    return bitmap;
}

From source file:Main.java

/**
 * Fills the bitmap's pixels of the specified Color to transparency.
 * //from   ww w  .j  a v a 2s  .co m
 * @param aBitmap bitmap to process
 * @param aColor color to fill
 * @return bmp
 */
@SuppressLint("NewApi")
public static Bitmap eraseBG(Bitmap aBitmap, int aColor) {
    int width = aBitmap.getWidth();
    int height = aBitmap.getHeight();
    Bitmap b = aBitmap.copy(Config.ARGB_8888, true);
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB_MR1) {
        b.setHasAlpha(true);
    }

    int[] pixels = new int[width * height];
    aBitmap.getPixels(pixels, 0, width, 0, 0, width, height);

    for (int i = 0; i < width * height; i++) {
        if (pixels[i] == aColor) {
            pixels[i] = 0;
        }
    }

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

    return b;
}

From source file:Main.java

public static Bitmap toThreshold(Bitmap bmpOriginal) {
    int imageWidth = bmpOriginal.getWidth();
    int imageHeight = bmpOriginal.getHeight();
    Bitmap bmpThreshold = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.RGB_565);
    int[] buffer = new int[imageWidth];

    int grayVal;//from   w w  w.  ja v a 2 s  .  c  om
    for (int row = 0; row < imageHeight; row++) {
        for (int col = 0; col < imageWidth; col++) {
            grayVal = rgbToGray(bmpOriginal.getPixel(col, row));
            if (grayVal > 125)
                buffer[col] = Color.rgb(255, 255, 255);
            else
                buffer[col] = Color.rgb(0, 0, 0);
        }
        bmpThreshold.setPixels(buffer, 0, imageWidth, 0, row, imageWidth, 1);
    }
    return bmpThreshold;
}

From source file:Main.java

public static Bitmap createBitmapFromBytes(byte[] data, int width, int height) {

    int[] colors = new int[width * height];
    Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int index = (y * width + x) * 3;
            int r = (((byte) data[index + 0]) & 0x00FF) & getMask(8);
            int g = (((byte) data[index + 1]) & 0x00FF) & getMask(8);
            int b = (((byte) data[index + 2]) & 0x00FF) & getMask(8);
            colors[x + y * width] = Color.rgb(r, g, b);
        }/*  www . ja  v  a  2  s  .co  m*/
    }
    data = null;

    bitmap.setPixels(colors, 0, width, 0, 0, width, height);
    colors = null;

    return bitmap;
}

From source file:piuk.blockchain.android.util.WalletUtils.java

public static Bitmap getQRCodeBitmap(final String url, final int size) {
    try {// w ww  . j a va2 s  .  c  o  m
        final Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.MARGIN, 2);

        final BitMatrix result = QR_CODE_WRITER.encode(url, BarcodeFormat.QR_CODE, size, size, hints);

        final int width = result.getWidth();
        final int height = result.getHeight();
        final int[] pixels = new int[width * height];

        for (int y = 0; y < height; y++) {
            final int offset = y * width;
            for (int x = 0; x < width; x++) {
                pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.TRANSPARENT;
            }
        }

        final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    } catch (final WriterException x) {
        x.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Bitmap getSingleColorBitmap(int picWidth, int picHeight, int color) {
    Bitmap bm1 = Bitmap.createBitmap(picWidth, picHeight, Bitmap.Config.ARGB_8888);

    int[] pix = new int[picWidth * picHeight];

    for (int y = 0; y < picHeight; y++)
        for (int x = 0; x < picWidth; x++) {
            int index = y * picWidth + x;
            //int r = ((pix[index] >> 16) & 0xff)|0xff;
            //int g = ((pix[index] >> 8) & 0xff)|0xff;
            //int b =( pix[index] & 0xff)|0xff;
            // pix[index] = 0xff000000 | (r << 16) | (g << 8) | b;
            pix[index] = color;/*from   ww w.  j  a  v a 2  s.  co  m*/

        }
    bm1.setPixels(pix, 0, picWidth, 0, 0, picWidth, picHeight);
    return bm1;
}

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;//  w  w w.  java 2 s  .c  o  m
        }
    }
    myBitmap.setPixels(intArray, 0, myBitmap.getHeight(), 0, 0, myBitmap.getHeight(), myBitmap.getWidth());

    view.setImageBitmap(myBitmap);
}

From source file:Main.java

public static Bitmap effectWhiten(Bitmap bitmap, int threshold) {
    if (bitmap == null) {
        return bitmap;
    }/*w w  w. j a  v a 2 s . 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] > threshold && color[1] > threshold && color[2] > threshold) {
                color[0] = 255;
                color[1] = 255;
                color[2] = 255;
            }
            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

public static Bitmap handleImageOldPhoto(Bitmap bm) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    int color;/*from   w  w  w  . j  av  a2 s  .c  o  m*/
    int r, g, b, a;
    Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
    int[] oldPx = new int[width * height];
    int[] newPx = new int[width * height];
    bm.getPixels(oldPx, 0, width, 0, 0, width, height);
    for (int i = 0; i < width * height; i++) {
        color = oldPx[i];
        r = Color.red(color);
        g = Color.green(color);
        b = Color.blue(color);
        a = Color.alpha(color);

        r = (int) (0.393 * r + 0.769 * g + 0.189 * b);
        g = (int) (0.349 * r + 0.686 * g + 0.168 * b);
        b = (int) (0.272 * r + 0.534 * g + 0.131 * b);
        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:com.example.amapdemo.heatmap.HeatmapTileProvider.java

/**
 * Converts a grid of intensity values to a colored Bitmap, using a given color map
 *
 * @param grid     the input grid (assumed to be square)
 * @param colorMap color map (created by generateColorMap)
 * @param max      Maximum intensity value: maps to 100% on gradient
 * @return the colorized grid in Bitmap form, with same dimensions as grid
 *///  w ww  . j av  a 2  s  . com
static Bitmap colorize(double[][] grid, int[] colorMap, double max) {
    // Maximum color value
    int maxColor = colorMap[colorMap.length - 1];
    // Multiplier to "scale" intensity values with, to map to appropriate color
    double colorMapScaling = (colorMap.length - 1) / max;
    // Dimension of the input grid (and dimension of output bitmap)
    int dim = grid.length;

    int i, j, index, col;
    double val;
    // Array of colors
    int colors[] = new int[dim * dim];
    for (i = 0; i < dim; i++) {
        for (j = 0; j < dim; j++) {
            // [x][y]
            // need to enter each row of x coordinates sequentially (x first)
            // -> [j][i]
            val = grid[j][i];
            index = i * dim + j;
            col = (int) (val * colorMapScaling);

            if (val != 0) {
                // Make it more resilient: cant go outside colorMap
                if (col < colorMap.length)
                    colors[index] = colorMap[col];
                else
                    colors[index] = maxColor;
            } else {
                colors[index] = Color.TRANSPARENT;
            }
        }
    }

    // Now turn these colors into a bitmap
    Bitmap tile = Bitmap.createBitmap(dim, dim, Bitmap.Config.ARGB_8888);
    // (int[] pixels, int offset, int stride, int x, int y, int width, int height)
    tile.setPixels(colors, 0, dim, 0, 0, dim, dim);
    return tile;
}