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

private static Bitmap rotate(Bitmap result, int rotation) {
    Matrix matrix = new Matrix();
    matrix.setRotate(rotation, (float) result.getWidth() / 2, (float) result.getHeight() / 2);

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

From source file:Main.java

public static Bitmap scale(Bitmap b, int reqWidth, int reqHeight) {
    Matrix m = new Matrix();
    if (b.getWidth() > b.getHeight()) {
        reqWidth = (int) (reqHeight * (1.0 * b.getWidth() / b.getHeight()));
    } else {//  w w  w  .j a  v a 2 s  . co  m
        reqHeight = (int) (reqWidth * (1.0 * b.getHeight() / b.getWidth()));
    }
    m.setRectToRect(new RectF(0, 0, b.getWidth(), b.getHeight()), new RectF(0, 0, reqWidth, reqHeight),
            Matrix.ScaleToFit.CENTER);
    return Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
}

From source file:Main.java

/**
 * scale image/*  w  w w  .ja  v a  2 s.  c om*/
 * @param org
 * @param newWidth
 * @param newHeight
 * @return
 */
public static Bitmap scaleImageTo(Bitmap org, int newWidth, int newHeight) {
    return scaleImage(org, (float) newWidth / org.getWidth(), (float) newHeight / org.getHeight());
}

From source file:Main.java

public static Bitmap bitmapZoomByHeight(Bitmap srcBitmap, float newHeight) {
    int srcWidth = srcBitmap.getWidth();
    int srcHeight = srcBitmap.getHeight();

    float scaleHeight = ((float) newHeight) / srcHeight;
    float scaleWidth = scaleHeight;

    return bitmapZoomByScale(srcBitmap, scaleWidth, scaleHeight);

}

From source file:Main.java

public static Bitmap getPaintedBitmap(Bitmap bitmap, Paint paint, boolean deleteOld) {
    final Bitmap colorBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    final Canvas c = new Canvas(colorBitmap);
    c.drawBitmap(bitmap, 0, 0, paint);//w  w w.  j a va2  s . co  m
    if (deleteOld) {
        bitmap.recycle();
    }
    return colorBitmap;
}

From source file:Main.java

/**
 * Adds the white border to the specifed bitmap image using specified border size.
 * //from   w w  w.  j a v  a  2  s  .co  m
 * @param bmp to add border
 * @param borderSize
 * @return bitmap image with border.
 */
public static Bitmap addWhiteBorder(Bitmap bmp, int borderSize, int imgSize) {

    Bitmap bmpWithBorder = Bitmap.createBitmap(bmp.getWidth() + borderSize * 2,
            bmp.getHeight() + borderSize * 2, bmp.getConfig());
    Canvas canvas = new Canvas(bmpWithBorder);
    canvas.drawColor(Color.WHITE);
    canvas.drawBitmap(bmp, borderSize, borderSize, null);

    return bmpWithBorder;
}

From source file:Main.java

public static Bitmap createWatermark(Bitmap src, Bitmap watermark, int direction, int spacing) {
    final int w = src.getWidth();
    final int h = src.getHeight();
    Bitmap bitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawBitmap(src, 0, 0, null);//  w w  w  .  j a  va 2  s .com
    if (direction == LEFT_TOP) {
        canvas.drawBitmap(watermark, spacing, spacing, null);
    } else if (direction == LEFT_BOTTOM) {
        canvas.drawBitmap(watermark, spacing, h - watermark.getHeight() - spacing, null);
    } else if (direction == RIGHT_TOP) {
        canvas.drawBitmap(watermark, w - watermark.getWidth() - spacing, spacing, null);
    } else if (direction == RIGHT_BOTTOM) {
        canvas.drawBitmap(watermark, w - watermark.getWidth() - spacing, h - watermark.getHeight() - spacing,
                null);
    }
    return bitmap;
}

From source file:Main.java

/**
 * This method of image pixelization utilizes the bitmap scaling operations built
 * into the framework. By downscaling the bitmap and upscaling it back to its
 * original size (while setting the filter flag to false), the same effect can be
 * achieved with much better performance.
 *///from   w  w  w. j av a 2s .  c  o  m
public static BitmapDrawable builtInPixelization(Context context, float pixelizationFactor, Bitmap bitmap) {

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    int downScaleFactorWidth = (int) (pixelizationFactor * width);
    downScaleFactorWidth = downScaleFactorWidth > 0 ? downScaleFactorWidth : 1;
    int downScaleFactorHeight = (int) (pixelizationFactor * height);
    downScaleFactorHeight = downScaleFactorHeight > 0 ? downScaleFactorHeight : 1;

    int downScaledWidth = width / downScaleFactorWidth;
    int downScaledHeight = height / downScaleFactorHeight;

    Bitmap pixelatedBitmap = Bitmap.createScaledBitmap(bitmap, downScaledWidth, downScaledHeight, false);

    /* Bitmap's createScaledBitmap method has a filter parameter that can be set to either
     * true or false in order to specify either bilinear filtering or point sampling
     * respectively when the bitmap is scaled up or now.
     *
     * Similarly, a BitmapDrawable also has a flag to specify the same thing. When the
     * BitmapDrawable is applied to an ImageView that has some scaleType, the filtering
     * flag is taken into consideration. However, for optimization purposes, this flag was
     * ignored in BitmapDrawables before Jelly Bean MR1.
     *
     * Here, it is important to note that prior to JBMR1, two bitmap scaling operations
     * are required to achieve the pixelization effect. Otherwise, a BitmapDrawable
     * can be created corresponding to the downscaled bitmap such that when it is
     * upscaled to fit the ImageView, the upscaling operation is a lot faster since
     * it uses internal optimizations to fit the ImageView.
     * */
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        BitmapDrawable bitmapDrawable = new BitmapDrawable(context.getResources(), pixelatedBitmap);
        bitmapDrawable.setFilterBitmap(false);
        return bitmapDrawable;
    } else {
        Bitmap upscaled = Bitmap.createScaledBitmap(pixelatedBitmap, width, height, false);
        return new BitmapDrawable(context.getResources(), upscaled);
    }
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {

    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);//  ww w. j a v a 2 s. c  o  m
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

From source file:Main.java

public static int drawIn(Canvas canvas, Bitmap bitmap, Rect display, int x) {
    if (bitmap != null) {
        Rect source = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        display = new Rect(display.left + x, display.top + x, display.right - x, display.bottom - x);
        canvas.drawBitmap(bitmap, source, display, null);
        return x + display.width();
    }/*from  w  ww .  j a  v  a  2s . c  o  m*/
    return 0;
}