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:cmusv.mr.carbon.utils.QRCodeGenerator.java

public static Bitmap encodeAsBitmap(String contentsToEncode, int dimension, boolean isEncoded)
        throws WriterException {
    if (contentsToEncode == null) {
        return null;
    }//from w w w .j av a 2s  .c om
    if (isEncoded) {
        Base64 base64 = new Base64();
        contentsToEncode = new String(base64.encode(contentsToEncode.getBytes()));
    }

    Map<EncodeHintType, Object> hints = null;
    String encoding = guessAppropriateEncoding(contentsToEncode);
    if (encoding != null) {
        hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
        hints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    MultiFormatWriter writer = new MultiFormatWriter();
    BitMatrix result = writer.encode(contentsToEncode, format, dimension, dimension, hints);
    int width = result.getWidth();
    int height = result.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] = result.get(x, y) ? BLACK : WHITE;
        }
    }

    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

/**
 * /*  w ww  .ja v  a  2s.co  m*/
 * TODO
 * ImageFormat.NV21 || format == ImageFormat.YUY2.
 * @param data
 * @param width
 * @param height
 * @param rect
 * @return
 */
public static Bitmap cropYuv2Bitmap(byte[] data, int width, int height, Rect rect) {
    int w = rect.width();
    int h = rect.height();
    int frameSize = width * height;
    int[] pixels = new int[w * h];
    byte[] yuv = data;
    int yOffset = rect.top * width + rect.left;
    int uvOffset = (rect.top / 2) * width + (rect.left / 2) * 2 + frameSize;
    int y, u, v, k;

    for (int i = 0; i < h; ++i) {
        int outputOffset = i * w;
        for (int j = 0; j < w; ++j) {
            y = (0xff & yuv[yOffset + j]) - 16;

            k = ((j >> 1) << 1);
            v = (0xff & yuv[uvOffset + k]) - 128;
            u = (0xff & yuv[uvOffset + k + 1]) - 128;

            int y1192 = 1192 * y;
            int r = (y1192 + 1634 * v);
            int g = (y1192 - 833 * v - 400 * u);
            int b = (y1192 + 2066 * u);

            if (r < 0)
                r = 0;
            else if (r > 262143)
                r = 262143;
            if (g < 0)
                g = 0;
            else if (g > 262143)
                g = 262143;
            if (b < 0)
                b = 0;
            else if (b > 262143)
                b = 262143;

            pixels[outputOffset + j] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00)
                    | ((b >> 10) & 0xff);
        }
        yOffset += width;
        if (((rect.top + i) & 1) == 1) {
            uvOffset += width;
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, w, 0, 0, w, h);
    return bitmap;
}

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++;/*  w  ww  .  ja  v a  2  s  .  c  o  m*/
    }
    return dest;
}

From source file:Main.java

/**
 * Convert bitmap format to <code>Bitmap.Config.ARGB_8888</code>.
 *
 * @param img/*  w ww .  j  ava 2s .  co  m*/
 *            JPEG bitmap to convert
 * @return {@link Bitmap} in ARGB8888 format
 */
public static Bitmap convertBitmapFormatToARGB888(Bitmap img) {
    int numPixels = img.getWidth() * img.getHeight();
    int[] pixels = new int[numPixels];

    // Get JPEG pixels. Each int is the color values for one pixel.
    img.getPixels(pixels, 0, img.getWidth(), 0, 0, img.getWidth(), img.getHeight());

    // Create a Bitmap of the appropriate format.
    Bitmap result = Bitmap.createBitmap(img.getWidth(), img.getHeight(), Bitmap.Config.ARGB_8888);

    // Set RGB pixels.
    result.setPixels(pixels, 0, result.getWidth(), 0, 0, result.getWidth(), result.getHeight());
    return result;
}

From source file:Main.java

/** Sets pixels in the given bitmap to a grayscale image from the byte array from a camera preview,
 * assumed to be in YUV (NV21) format with brightness pixels first.
 *//*from   www.  j  a  v  a  2 s . c o  m*/
public static Bitmap fillGrayscaleBitmapFromCameraData(Bitmap bitmap, byte[] cdata, int width, int height) {
    int[] pixels = new int[cdata.length];
    for (int i = 0; i < cdata.length; i++) {
        int g = 0xff & cdata[i];
        pixels[i] = (255 << 24) + (g << 16) + (g << 8) + g;
    }
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}

From source file:Main.java

/**
 * Replaces colors in a bitmap that are not farther away from a specific color than a given
 * threshold.//from w  ww  .j av  a2s  .  com
 *
 * @param srcBitmap    The source bitmap to scan.
 * @param mutateSrc    Indicates whether to mutate srcBitmap or to produce a new one.
 * @param keepCr       The red color to keep
 * @param keepCg       The green color to keep
 * @param keepCb       The blue color to keep
 * @param replaceColor The color to replace mismatched colors with
 * @param distance     The distance threshold.
 * @param simpleBG     Whether the bitmap has a simple background
 * @return Bitmap with replaced colors
 */
private static Bitmap replaceColors(Bitmap srcBitmap, boolean mutateSrc, int keepCr, int keepCg, int keepCb,
        int replaceColor, int distance, boolean simpleBG) {
    int[] allpixels = new int[srcBitmap.getHeight() * srcBitmap.getWidth()];
    srcBitmap.getPixels(allpixels, 0, srcBitmap.getWidth(), 0, 0, srcBitmap.getWidth(), srcBitmap.getHeight());
    int bgColor = replaceColor;
    int distanceSq = distance * distance;

    if (simpleBG) {
        bgColor = allpixels[0];
    }

    for (int i = 0; i < allpixels.length; i++) {
        /* Avoid unnecessary math for obviously background color. This removes most of the math
         * for candy, HP and name bitmaps. */
        if (allpixels[i] == bgColor) {
            allpixels[i] = replaceColor;
            continue;
        }
        int rDiff = keepCr - Color.red(allpixels[i]);
        int gDiff = keepCg - Color.green(allpixels[i]);
        int bDiff = keepCb - Color.blue(allpixels[i]);
        int dSq = rDiff * rDiff + gDiff * gDiff + bDiff * bDiff;
        if (dSq > distanceSq) {
            allpixels[i] = replaceColor;
        }
    }

    Bitmap dstBitmap;
    if (mutateSrc) {
        dstBitmap = srcBitmap;
    } else {
        dstBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), srcBitmap.getConfig());
    }
    dstBitmap.setPixels(allpixels, 0, srcBitmap.getWidth(), 0, 0, srcBitmap.getWidth(), srcBitmap.getHeight());
    return dstBitmap;
}

From source file:io.digibyte.tools.qrcode.QRUtils.java

public static Bitmap encodeAsBitmap(String content, int dimension) {

    if (content == null) {
        return null;
    }//  www  . j  a  va  2s. c  o m
    Map<EncodeHintType, Object> hints = null;
    String encoding = guessAppropriateEncoding(content);
    hints = new EnumMap<>(EncodeHintType.class);
    if (encoding != null) {
        hints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    hints.put(EncodeHintType.MARGIN, 1);
    BitMatrix result = null;
    try {
        result = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, dimension, dimension, hints);
    } catch (IllegalArgumentException iae) {
        // Unsupported format
        return null;
    } catch (WriterException e) {
        e.printStackTrace();
    }
    if (result == null)
        return null;
    int width = result.getWidth();
    int height = result.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] = result.get(x, y) ? BLACK : WHITE;
        }
    }

    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

/** Sets pixels in the given bitmap to a grayscale image from the byte array from a camera preview,
 * assumed to be in YUV (NV21) format with brightness pixels first.
 *//*from w  w w.  j a v a2s.c  o m*/
public static Bitmap fillGrayscaleBitmapFromData(Bitmap bitmap, byte[] cdata, int width, int height,
        int[] pixelBuffer) {
    int npixels = width * height;
    for (int i = 0; i < npixels; i++) {
        int g = 0xff & cdata[i];
        pixelBuffer[i] = (255 << 24) + (g << 16) + (g << 8) + g;
    }
    bitmap.setPixels(pixelBuffer, 0, width, 0, 0, width, height);
    return bitmap;
}

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, Bitmap.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  ww  w  .ja  v a  2 s  . c o m*/
    blured.setPixels(inPixels, 0, width, 0, 0, width, height);
    return blured;
}

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);
    }// w ww  . j av a2 s  . c om
    blured.setPixels(inPixels, 0, width, 0, 0, width, height);
    return blured;
}