Example usage for android.graphics Bitmap createBitmap

List of usage examples for android.graphics Bitmap createBitmap

Introduction

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

Prototype

public static Bitmap createBitmap(@NonNull DisplayMetrics display, @NonNull @ColorInt int[] colors, int offset,
        int stride, int width, int height, @NonNull Config config) 

Source Link

Document

Returns a immutable bitmap with the specified width and height, with each pixel value set to the corresponding value in the colors array.

Usage

From source file:Main.java

/**
 * @param bitmap/* www .j a  va 2s . co  m*/
 * @param matrix
 * @return
 * Function to get a square image once cropped from a rectangular image..
 */
public static Bitmap getSquareImage(Bitmap bitmap, Matrix matrix) {
    if (bitmap.getWidth() >= bitmap.getHeight()) {
        return Bitmap.createBitmap(bitmap, bitmap.getWidth() / 2 - bitmap.getHeight() / 2, 0,
                bitmap.getHeight(), bitmap.getHeight(), matrix, false);
    } else {
        return Bitmap.createBitmap(bitmap, 0, bitmap.getHeight() / 2 - bitmap.getWidth() / 2, bitmap.getWidth(),
                bitmap.getWidth(), matrix, false);
    }
}

From source file:Main.java

public final static Bitmap rotate(Bitmap b, float degrees) {
    if (degrees != 0 && b != null) {
        Matrix m = new Matrix();
        m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);

        Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
        if (b != b2) {
            b.recycle();/* w  w w  .ja v a 2 s  . c  o m*/
            b = b2;
        }

    }
    return b;
}

From source file:Main.java

/**
 * @param b//from w w w  .  j a  v  a 2  s  . c  om
 * @param rotateDegree
 * @return
 */
public static Bitmap getRotateBitmap(Bitmap b, float rotateDegree) {
    Matrix matrix = new Matrix();
    matrix.postRotate(rotateDegree);
    int width = b.getWidth();
    int height = b.getHeight();
    return Bitmap.createBitmap(b, 0, 0, width, height, matrix, false);
}

From source file:Main.java

/**
 * Method to flip vertically a Bitmap./*from   w ww.  j a va 2s  . c o m*/
 *
 * @param source The original Bitmap.
 * @return The flipped Bitmap.
 */
public static Bitmap flipVertically(Bitmap source) {
    Matrix m = new Matrix();
    m.preScale(1, -1);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), m, false);
}

From source file:Main.java

private static Bitmap resizeBitmap(Bitmap bmp, float width, float height) {
    float xscale = width / bmp.getWidth();
    float yscale = height / bmp.getHeight();
    Matrix matrix = new Matrix();
    // resize original image
    matrix.postScale(xscale, yscale);//  w ww  .  j  ava 2  s. c o  m
    Bitmap dstbmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
    return dstbmp;
}

From source file:Main.java

/**
 * Method using rotate photo.//  ww  w .j a va2 s  .c  o  m
 *
 * @param bitmap Value bitmap current.
 * @param angle  Value angle.
 * @return Bitmap.
 */
private static Bitmap rotateImage(Bitmap bitmap, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);

    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}

From source file:Main.java

/**
 * Method to flip horizontally a Bitmap.
 *
 * @param source The original Bitmap.//from  w  ww  .  ja  v a 2 s.  com
 * @return The flipped Bitmap.
 */
public static Bitmap flipHorizonally(Bitmap source) {
    Matrix m = new Matrix();
    m.setScale(-1, 1);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), m, false);
}

From source file:Main.java

public static Bitmap ScaleAndRotateBitmap(Bitmap bitmap, int rotation, int flipHorizontal) {
    Matrix m = new Matrix();
    if (flipHorizontal != 0) {
        m.postScale(-1, 1);/* w w w  . j  a  va2  s. c  o m*/
    }
    if (rotation != 0) {
        m.postRotate(rotation);
    }
    Bitmap finalBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
    if (finalBitmap != bitmap) {
        bitmap.recycle();
    }
    return finalBitmap;
}

From source file:Main.java

public static WeakReference<Bitmap> rotateBitmap(WeakReference<Bitmap> bitmap, int degress) {
    if (bitmap == null || bitmap.get() == null)
        return null;
    Matrix m = new Matrix();
    m.postRotate(degress);/*w  w w . ja  v  a 2  s . c  o m*/
    return new WeakReference<Bitmap>(Bitmap.createBitmap(bitmap.get(), 0, 0, bitmap.get().getWidth(),
            bitmap.get().getHeight(), m, true));
}

From source file:Main.java

public static Bitmap getTotateBitmap(Bitmap bitmap, int orientation) {
    Matrix matrix = new Matrix();

    switch (orientation) {
    case ExifInterface.ORIENTATION_ROTATE_90: {
        matrix.setRotate(90);/* w  w  w.  j  a va2  s.co m*/
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }
        break;
    case ExifInterface.ORIENTATION_ROTATE_180: {
        matrix.setRotate(180);
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }
        break;
    case ExifInterface.ORIENTATION_ROTATE_270: {
        matrix.setRotate(270);
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }
        break;
    default:
        break;
    }
    return bitmap;
}