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:Main.java

public static Bitmap reliefImage(Bitmap bm) {

    int width = bm.getWidth();
    int height = bm.getHeight();
    int color;/* w w w.j a va 2 s  .  c o m*/
    int r, g, b, a;

    Bitmap bitmap = Bitmap.createBitmap(width, height, 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 = 1; i < oldPx.length; i++) {
        color = oldPx[i - 1];
        r = Color.red(color);
        g = Color.green(color);
        b = Color.blue(color);
        a = Color.alpha(color);

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

        r = r1 - r + 127;
        g = g1 - g + 127;
        b = b1 - b + 127;

        if (r > 255) {
            r = 255;
        } else if (r < 0) {
            r = 0;
        }

        if (g > 255) {
            g = 255;
        } else if (g < 0) {
            g = 0;
        }

        if (b > 255) {
            b = 255;
        } else if (b < 0) {
            b = 0;
        }

        newPx[i] = Color.argb(a, r, g, b);
    }

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

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;//  ww  w. ja v a2s  . 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);
        }
    }

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

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 va 2s . 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:com.linroid.pushapp.ui.home.DeviceTokenDialog.java

private Bitmap generateQrcode(String content, int size) {
    QRCodeWriter writer = new QRCodeWriter();
    try {/*from  w w w  .j a  v  a  2 s.c  o m*/
        EnumMap<EncodeHintType, Object> hint = new EnumMap<>(EncodeHintType.class);
        hint.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, size, size, hint);
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        int[] pixels = new int[width * height];
        for (int y = 0; y < height; y++) {
            int offset = y * width;
            for (int x = 0; x < width; x++) {
                // pixels[offset + x] = bitMatrix.get(x, y) ? 0xFF000000
                // : 0xFFFFFFFF;
                pixels[offset + x] = bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF;
            }
        }

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static Bitmap effectChangeContrast(Bitmap bitmap, double effectLevel) {
    if (bitmap == null) {
        return bitmap;
    }//w w w .j av  a  2 s  .c o  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] > 200 && color[1] > 200 && color[3] > 200) {
                color[0] = 255;
                color[1] = 255;
                color[2] = 255;
            }

            for (int i = 0; i < color.length; i++) {
                int tmpColor = color[i];
                tmpColor = (int) ((tmpColor - 128) * effectLevel + 128);
                if (tmpColor < 0) {
                    tmpColor = 0;
                } else if (tmpColor > 255) {
                    tmpColor = 255;
                }
                color[i] = tmpColor;
            }

            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:com.aegiswallet.utils.BasicUtils.java

public static Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int dimension) {

    Bitmap bitmap = null;

    try {/* w  ww  .ja v  a2  s  . co m*/
        final Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
        //hints.put(EncodeHintType.MARGIN, 0);
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        final BitMatrix result = qrCodeWriter.encode(contents, BarcodeFormat.QR_CODE, dimension, dimension,
                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.WHITE;
            }
        }

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

    } catch (WriterException e) {
        Log.e("Basic Utils", "cannot write to bitmap " + e.getMessage());
    }
    return bitmap;
}

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

@SuppressWarnings("ConstantConditions")
public static Bitmap getBitmapWithReplacedColor(@NonNull Bitmap bitmap, @ColorInt int colorToReplace,
        @ColorInt int replaceWith) {
    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 av a 2 s .c  om*/

    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] = replaceWith;
            }
            if (pixels[index] != replaceWith) {
                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:com.bonsai.btcreceive.ReceiveFragment.java

private Bitmap createBitmap(String content, final int size) {
    final Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
    hints.put(EncodeHintType.MARGIN, 0);
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    BitMatrix result;//from www.j  av  a2s  . c  o  m
    try {
        result = sQRCodeWriter.encode(content, BarcodeFormat.QR_CODE, size, size, hints);
    } catch (WriterException ex) {
        mLogger.warn("qr encoder failed: " + ex.toString());
        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) ? 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;
}

From source file:it.mb.whatshare.PairOutboundActivity.java

private Bitmap generateQRCode(String dataToEncode, int imageViewSize) throws WriterException {
    MultiFormatWriter writer = new MultiFormatWriter();
    BitMatrix matrix = writer.encode(dataToEncode, BarcodeFormat.QR_CODE, imageViewSize, imageViewSize);
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    int[] pixels = new int[width * height];
    for (int y = 0; y < height; y++) {
        int offset = y * width;
        for (int x = 0; x < width; x++) {
            pixels[offset + x] = matrix.get(x, y) ? 0xff000000 : 0xffffffff;
        }/*from w w  w. j  av a2 s.co m*/
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}

From source file:com.cettco.buycar.activity.OrderDetailActivity.java

public Bitmap Create2DCode(String str) throws WriterException {
    // ?,??,?????,
    BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, 500, 500);
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    // ?,?/*  w w w.java  2s  . co  m*/
    int[] pixels = new int[width * height];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            if (matrix.get(x, y)) {
                pixels[y * width + x] = 0xff000000;
            }

        }
    }

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    // ??bitmap,?api
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}