Example usage for android.graphics Bitmap getWidth

List of usage examples for android.graphics Bitmap getWidth

Introduction

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

Prototype

public final int getWidth() 

Source Link

Document

Returns the bitmap's width

Usage

From source file:Main.java

public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Matrix matrix = new Matrix();
    float scaleWidth = (float) (w / width);
    float scaleHeight = (float) (h / height);
    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    return result;
}

From source file:Main.java

public static Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) {
    int sourceWidth = source.getWidth();
    int sourceHeight = source.getHeight();

    // Compute the scaling factors to fit the new height and width, respectively.
    // To cover the final image, the final scaling will be the bigger
    // of these two.
    float xScale = (float) newWidth / sourceWidth;
    float yScale = (float) newHeight / sourceHeight;
    float scale = Math.max(xScale, yScale);

    // Now get the size of the source bitmap when scaled
    float scaledWidth = scale * sourceWidth;
    float scaledHeight = scale * sourceHeight;

    // Let's find out the upper left coordinates if the scaled bitmap
    // should be centered in the new size give by the parameters
    float left = (newWidth - scaledWidth) / 2;
    float top = (newHeight - scaledHeight) / 2;

    // The target rectangle for the new, scaled version of the source bitmap will now
    // be/*from  w w w  .  j  a  v a2 s.  c o m*/
    RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);

    // Finally, we create a new bitmap of the specified size and draw our new,
    // scaled bitmap onto it.
    Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(dest);
    canvas.drawBitmap(source, null, targetRect, null);

    return dest;
}

From source file:Main.java

public static Bitmap scaleToFitHeight(Bitmap bitmap, int i) {
    return Bitmap.createScaledBitmap(bitmap,
            (int) (((float) i / (float) bitmap.getHeight()) * (float) bitmap.getWidth()), i, false);
}

From source file:Main.java

/**
 * Method to combine two images side by side.
 *
 * @param leftBmp  The left Bitmap./* ww w  . ja  va 2s.com*/
 * @param rightBmp The right Bitmap.
 * @return A Bitmap with left and right bitmap are glued side by side.
 */
public static Bitmap combineTwoImagesSideBySide(Bitmap leftBmp, Bitmap rightBmp) {
    int width;
    int height = leftBmp.getHeight();

    if (leftBmp.getWidth() > rightBmp.getWidth()) {
        width = leftBmp.getWidth() + rightBmp.getWidth();
    } else {
        width = rightBmp.getWidth() + rightBmp.getWidth();
    }

    Bitmap cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas comboImage = new Canvas(cs);
    comboImage.drawBitmap(leftBmp, 0f, 0f, null);
    comboImage.drawBitmap(rightBmp, leftBmp.getWidth(), 0f, null);

    return cs;
}

From source file:Main.java

/**
 * Retrieves a copy of the specified drawable resource, rotated by a specified angle.
 *
 * @param resources The current resources.
 * @param resourceId The resource ID of the drawable to rotate.
 * @param angle The angle of rotation.//www.  ja va  2  s  .  c  o m
 * @return Rotated drawable.
 */
public static Drawable getRotatedDrawable(android.content.res.Resources resources, int resourceId,
        float angle) {

    // Get the original drawable and make a copy which will be rotated.
    Bitmap original = BitmapFactory.decodeResource(resources, resourceId);
    Bitmap rotated = Bitmap.createBitmap(original.getWidth(), original.getHeight(), Bitmap.Config.ARGB_8888);

    // Perform the rotation.
    Canvas tempCanvas = new Canvas(rotated);
    tempCanvas.rotate(angle, original.getWidth() / 2, original.getHeight() / 2);
    tempCanvas.drawBitmap(original, 0, 0, null);

    return new BitmapDrawable(resources, rotated);
}

From source file:Main.java

public static Bitmap scaleImage(Bitmap bitmap, int destW, int destH) {
    if (bitmap == null) {
        return null;
    }//from  w  ww  . j  a va  2s .  c om
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    float scaleWidth = ((float) destW) / width;
    float scaleHeight = ((float) destH) / height;

    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    return scaledBitmap;
}

From source file:Main.java

public static final Bitmap newBitmapSquareCenterCrop(Bitmap bitmap) {

    if (bitmap == null) {
        return null;
    }/*from   w  w w  .  j  a  va 2s  .c om*/

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int left, top, right, bottom;

    if (width < height) {
        int cropSize = width;
        left = 0;
        right = cropSize;
        top = height / 2 - cropSize / 2;
        bottom = top + cropSize;
    } else {
        int cropSize = height;
        top = 0;
        bottom = cropSize;
        left = width / 2 - cropSize / 2;
        right = left + cropSize;
    }

    return Bitmap.createBitmap(bitmap, left, top, right - left, bottom - top);
}

From source file:Main.java

public static Bitmap doScale(Bitmap b, float scale) {
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);/*from   ww w  . j  a v a 2s  .c  o  m*/
    b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);
    return b;
}

From source file:Main.java

/**
 * Scale down the bitmap in order to make color analysis faster. Taken from Palette.
 *///w  w  w .  java 2s  .  co  m
private static Bitmap scaleBitmapDown(@NonNull Bitmap bitmap) {
    final int CALCULATE_BITMAP_MIN_DIMENSION = 100;
    final int minDimension = Math.min(bitmap.getWidth(), bitmap.getHeight());

    if (minDimension <= CALCULATE_BITMAP_MIN_DIMENSION) {
        // If the bitmap is small enough already, just return it
        return bitmap;
    }

    final float scaleRatio = CALCULATE_BITMAP_MIN_DIMENSION / (float) minDimension;
    return Bitmap.createScaledBitmap(bitmap, Math.round(bitmap.getWidth() * scaleRatio),
            Math.round(bitmap.getHeight() * scaleRatio), false);
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static int PutImageScale(Canvas canvas, Drawable image, double Angle, int x, int y, double scale) {

    if (scale == 0.0)
        return 0;

    float newWidth = (int) Math.round((float) image.getIntrinsicWidth() * scale);
    float newHeight = (int) Math.round((float) image.getIntrinsicHeight() * scale);

    Bitmap bmp = ((BitmapDrawable) image).getBitmap();
    int width = bmp.getWidth();
    int height = bmp.getHeight();

    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // createa matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // rotate the Bitmap
    matrix.postRotate((float) Angle);
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, true);
    // make a Drawable from Bitmap to allow to set the BitMap
    // to the ImageView, ImageButton or what ever
    BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

    bmd.setBounds(x, y, x + bmd.getIntrinsicWidth(), y + bmd.getIntrinsicHeight());
    bmd.draw(canvas);//from   ww w  .  j ava 2 s.  c om

    return bmd.getIntrinsicWidth();

}