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:app.axe.imooc.zxing.app.CaptureActivity.java

private Bitmap createQRCode() {
    int QR_WIDTH = 100;
    int QR_HEIGHT = 100;

    try {//w w w . j a  v  a  2  s  .c o  m
        // ?core
        QRCodeWriter writer = new QRCodeWriter();
        String text = Util.getIMEI(this);
        if (text == null || "".equals(text) || text.length() < 1) {
            return null;
        }
        // ?
        BitMatrix martix = writer.encode(text, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT);

        System.out.println("w:" + martix.getWidth() + "h:" + martix.getHeight());

        Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT,
                hints);
        int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
        for (int y = 0; y < QR_HEIGHT; y++) {
            for (int x = 0; x < QR_WIDTH; x++) {
                if (bitMatrix.get(x, y)) {
                    pixels[y * QR_WIDTH + x] = 0xff000000;
                } else {
                    pixels[y * QR_WIDTH + x] = 0xffffffff;
                }
            }
        }
        // cheng chen de er wei ma
        Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);

        return bitmap;
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.precisionag.waterplane.MainActivity.java

public void updateColors(Field field) {
    //get level from seekbar
    SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar);
    seekBar.setMax(255);//from ww  w  .  ja v  a  2  s  .c  o  m
    int waterLevel = seekBar.getProgress();

    //update text block
    waterLevelMeters = field.minElevation
            + ((double) waterLevel * (field.maxElevation - field.minElevation) / 255.0);
    TextView waterElevationTextView = (TextView) findViewById(R.id.text);
    String elevation = new DecimalFormat("#.#").format(waterLevelMeters);
    String waterElevationText = "Elevation: " + elevation + "m";
    waterElevationTextView.setText(waterElevationText);

    int width = field.elevationBitmap.getWidth();
    int height = field.elevationBitmap.getHeight();
    int[] pixels = new int[width * height];
    field.elevationBitmap.getPixels(pixels, 0, width, 0, 0, width, height);
    Bitmap bitmap = field.elevationBitmap.copy(field.elevationBitmap.getConfig(), true);

    //test each pixel, if below water level set blue, else set transparent
    for (int i = 0; i < (width * height); i++) {
        if ((pixels[i] & 0x000000FF) < waterLevel) {
            //water is visible, set pixel to blue
            pixels[i] = 0xFF0000FF;
        } else {
            //no water, set pixel transparent
            pixels[i] = 0x00000000;
        }
    }
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);

    //remove old map overlay and create new one
    prevoverlay.remove();

    prevoverlay = createOverlay(bitmap, field.fieldBounds);

}

From source file:com.yk.notification.util.BitmapUtil.java

/**
 * ?/*from  ww w.ja va2 s  . co m*/
 * 
 * @param bmp
 *            ?
 * @return ??
 */
public static Bitmap convertToBlackWhite(Bitmap bmp) {
    int width = bmp.getWidth();
    int height = bmp.getHeight();
    int[] pixels = new int[width * height];
    bmp.getPixels(pixels, 0, width, 0, 0, width, height);

    int alpha = 0xFF << 24; // bitmap?24
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            int grey = pixels[width * i + j];

            int red = ((grey & 0x00FF0000) >> 16);
            int green = ((grey & 0x0000FF00) >> 8);
            int blue = (grey & 0x000000FF);

            grey = (int) (red * 0.3 + green * 0.59 + blue * 0.11);
            grey = alpha | (grey << 16) | (grey << 8) | grey;
            pixels[width * i + j] = grey;
        }
    }
    Bitmap newBmp = Bitmap.createBitmap(width, height, Config.RGB_565);
    newBmp.setPixels(pixels, 0, width, 0, 0, width, height);
    return newBmp;
}

From source file:com.yk.notification.util.BitmapUtil.java

/**
 * ??/*from   w  ww  .ja va2s.  c  o m*/
 * 
 * @param img
 *            ?Bitmap
 * @return ??
 */
public static Bitmap convertGreyImg(Bitmap img) {
    int width = img.getWidth(); // ??
    int height = img.getHeight(); // ??

    int[] pixels = new int[width * height]; // ???

    img.getPixels(pixels, 0, width, 0, 0, width, height);
    int alpha = 0xFF << 24;
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            int grey = pixels[width * i + j];

            int red = ((grey & 0x00FF0000) >> 16);
            int green = ((grey & 0x0000FF00) >> 8);
            int blue = (grey & 0x000000FF);

            grey = (int) (red * 0.3 + green * 0.59 + blue * 0.11);
            grey = alpha | (grey << 16) | (grey << 8) | grey;
            pixels[width * i + j] = grey;
        }
    }
    Bitmap result = Bitmap.createBitmap(width, height, Config.RGB_565);
    result.setPixels(pixels, 0, width, 0, 0, width, height);
    return result;
}

From source file:Main.java

static Bitmap blurfast(Bitmap bmp, int radius) {
    try {//w  w  w . j av  a2s .  c  om
        Bitmap bitmap = bmp.copy(bmp.getConfig(), true);
        int w = bmp.getWidth();
        int h = bmp.getHeight();
        int[] pix = new int[w * h];
        bmp.getPixels(pix, 0, w, 0, 0, w, h);

        for (int r = radius; r >= 1; r /= 2) {
            for (int i = r; i < h - r; i++) {
                for (int j = r; j < w - r; j++) {
                    int tl = pix[(i - r) * w + j - r];
                    int tr = pix[(i - r) * w + j + r];
                    int tc = pix[(i - r) * w + j];
                    int bl = pix[(i + r) * w + j - r];
                    int br = pix[(i + r) * w + j + r];
                    int bc = pix[(i + r) * w + j];
                    int cl = pix[i * w + j - r];
                    int cr = pix[i * w + j + r];

                    pix[(i * w) + j] = 0xFF000000
                            | (((tl & 0xFF) + (tr & 0xFF) + (tc & 0xFF) + (bl & 0xFF) + (br & 0xFF)
                                    + (bc & 0xFF) + (cl & 0xFF) + (cr & 0xFF)) >> 3) & 0xFF
                            | (((tl & 0xFF00) + (tr & 0xFF00) + (tc & 0xFF00) + (bl & 0xFF00) + (br & 0xFF00)
                                    + (bc & 0xFF00) + (cl & 0xFF00) + (cr & 0xFF00)) >> 3) & 0xFF00
                            | (((tl & 0xFF0000) + (tr & 0xFF0000) + (tc & 0xFF0000) + (bl & 0xFF0000)
                                    + (br & 0xFF0000) + (bc & 0xFF0000) + (cl & 0xFF0000)
                                    + (cr & 0xFF0000)) >> 3) & 0xFF0000;
                }
            }
        }
        bitmap.setPixels(pix, 0, w, 0, 0, w, h);
        return bitmap;
    } catch (Exception e) {
        return bmp;
    }
}

From source file:at.wada811.gammacorrection.MainActivity.java

protected Bitmap gamma(double gamma) {
    int[] lut = new int[256];
    for (int i = 0; i < lut.length; i++) {
        lut[i] = Math.min((int) Math.round(255 * Math.pow(i / 255.0, 1.0 / gamma)), 255);
    }/*from ww  w. j  av  a  2s  . c  o  m*/
    Bitmap bitmap = BitmapUtils.copy(mBitmap);
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
    for (int w = 0; w < width; w++) {
        for (int h = 0; h < height; h++) {
            int r = 0;
            int g = 0;
            int b = 0;
            int color = pixels[w + h * width];
            r = Color.red(color);
            g = Color.green(color);
            b = Color.blue(color);
            // 
            r = lut[r];
            g = lut[g];
            b = lut[b];
            //
            color = Color.rgb(r, g, b);
            pixels[w + h * width] = color;
        }
    }
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}

From source file:onion.chat.MainActivity.java

void showQR() {
    String name = db.getName();//ww w. j a  va  2 s .c  o  m
    String txt = "Its Ur's " + tor.getID() + " " + name;

    QRCode qr;

    try {
        //qr = Encoder.encode(txt, ErrorCorrectionLevel.H);
        qr = Encoder.encode(txt, ErrorCorrectionLevel.M);
    } catch (Exception ex) {
        throw new Error(ex);
    }

    ByteMatrix mat = qr.getMatrix();
    int width = mat.getWidth();
    int height = mat.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] = mat.get(x, y) != 0 ? Color.BLACK : Color.WHITE;
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);

    bitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth() * 8, bitmap.getHeight() * 8, false);

    ImageView view = new ImageView(this);
    view.setImageBitmap(bitmap);

    int pad = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16,
            getResources().getDisplayMetrics());
    view.setPadding(pad, pad, pad, pad);

    Rect displayRectangle = new Rect();
    Window window = getWindow();
    window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
    int s = (int) (Math.min(displayRectangle.width(), displayRectangle.height()) * 0.9);
    view.setMinimumWidth(s);
    view.setMinimumHeight(s);
    new AlertDialog.Builder(this)
            //.setMessage(txt)
            .setView(view).show();
}

From source file:com.htc.dotdesign.DrawingView.java

public void setAsDotViewTheme() {
    if (mContext == null) {
        return;//from ww  w.j a  v a2s  .com
    }
    Bitmap bitmap = null;
    Bitmap photoARGB = null;
    try {
        bitmap = handlePicture();
        if (bitmap != null) {
            int[] photoData = new int[bitmap.getWidth() * bitmap.getHeight()];
            bitmap.getPixels(photoData, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
            photoARGB = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
            photoARGB.setPixels(photoData, 0, photoARGB.getWidth(), 0, 0, photoARGB.getWidth(),
                    photoARGB.getHeight());

            WallpaperMaker wallpaper = new WallpaperMaker();
            mScaledPhoto = wallpaper.convertDotViewWallpaper(photoARGB, 27, 48);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            mScaledPhoto.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();
            Intent intent = new Intent(DotDesignConstants.NOTIFY_DOTVIEW_TO_UPDATE_WALLPAPER);
            intent.putExtra(DotDesignConstants.EXTRA_BITMAP_ARRAY, byteArray);
            String filename = DotDesignUtil.getFileName() + "DotDrop";
            intent.putExtra(DotDesignConstants.EXTRA_FILE_NAME, filename);
            mContext.sendBroadcast(intent);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (bitmap != null && !bitmap.isRecycled()) {
            bitmap.recycle();
            bitmap = null;
        }
        if (mScaledPhoto != null && !mScaledPhoto.isRecycled()) {
            mScaledPhoto.recycle();
            mScaledPhoto = null;
        }
        if (photoARGB != null && !photoARGB.isRecycled()) {
            photoARGB.recycle();
            photoARGB = null;
        }
    }
    if (mActivity != null) {
        Toast.makeText(mActivity, getResources().getString(R.string.toast_theme_applied), Toast.LENGTH_SHORT)
                .show();
    }
}

From source file:io.jawg.osmcontributor.ui.utils.BitmapHandler.java

/**
 * Get the white icon corresponding to a poiType.
 *
 * @param poiType the PoiType or null for notes.
 * @return The white icon.//from   ww  w  . java2s .  c  o  m
 */
public Drawable getIconWhite(PoiType poiType) {
    Bitmap myBitmap = BitmapFactory.decodeResource(context.getResources(),
            poiType == null ? R.drawable.open_book : getIconDrawableId(poiType));
    myBitmap = myBitmap.copy(myBitmap.getConfig(), true);

    int[] allpixels = new int[myBitmap.getHeight() * myBitmap.getWidth()];

    myBitmap.getPixels(allpixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());

    for (int i = 0; i < myBitmap.getHeight() * myBitmap.getWidth(); i++) {
        if (allpixels[i] != 0) {
            int A = Color.alpha(allpixels[i]);
            // inverting byte for each R/G/B channel
            int R = 255 - Color.red(allpixels[i]);
            int G = 255 - Color.green(allpixels[i]);
            int B = 255 - Color.blue(allpixels[i]);
            // set newly-inverted pixel to output image
            allpixels[i] = Color.argb(A, R, G, B);
        }
    }

    myBitmap.setPixels(allpixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());

    return new BitmapDrawable(context.getResources(), myBitmap);
}

From source file:com.ibm.mil.readyapps.physio.fragments.PainLocationFragment.java

/**
 * Pulls the bitmap image of the body part out of its RelativeLayout, colors it appropriatly,
 * and then tells the zoomLayout where it needs to zoom to. Also swaps the FRONT/BACK buttons
 * for the ADD/DELETE buttons.//from  w  w  w .  jav a 2s. c om
 *
 * @param image the body part the touch occured in.
 */
private void zoomOnImage(RelativeLayout image) {

    Bitmap bitm = ((BitmapDrawable) image.getBackground()).getBitmap();

    Bitmap temp = bitm.copy(bitm.getConfig(), true);
    int[] pixels = new int[temp.getHeight() * temp.getWidth()];

    temp.getPixels(pixels, 0, temp.getWidth(), 0, 0, temp.getWidth(), temp.getHeight());

    for (int i = 0; i < temp.getHeight() * temp.getWidth(); i++) {

        if (pixels[i] != 0) {

            int r, g, b;

            if (!blue) {
                r = Color.red(pixels[i]) + 100;
                if (r > 255) {
                    r = 255;
                }
                g = Color.green(pixels[i]) - 50;
                b = Color.blue(pixels[i]) - 50;
            } else {
                r = Color.red(pixels[i]) - 30;
                g = Color.green(pixels[i]) + 15;
                b = Color.blue(pixels[i]) + 100;
                if (b > 255) {
                    b = 255;
                }

            }
            pixels[i] = Color.argb(Color.alpha(pixels[i]), r, g, b);
        }
    }

    temp.setPixels(pixels, 0, temp.getWidth(), 0, 0, temp.getWidth(), temp.getHeight());
    image.setBackground(new BitmapDrawable(getResources(), temp));

    Rect rect = new Rect();
    image.getGlobalVisibleRect(rect);

    zoomLayout.zoomToRect(rect, 0, getYOffset());
    current = image;
    zoomed = true;
    swapButtons();

    placePointer(rect.centerX(), rect.centerY());

}