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 overlay(Bitmap bmp1, Bitmap bmp2, float left, float top) {
    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bmp1, new Matrix(), null);
    canvas.drawBitmap(bmp2, left, top, null);
    return bmOverlay;
}

From source file:Main.java

public static Bitmap resizeBitmap(Bitmap input, int destWidth, int destHeight) {
    int srcWidth = input.getWidth();
    int srcHeight = input.getHeight();
    boolean needsResize = false;
    float p;//from w  w w  .ja  v  a  2s . co  m
    if (srcWidth > destWidth || srcHeight > destHeight) {
        needsResize = true;
        if (srcWidth > srcHeight && srcWidth > destWidth) {
            p = (float) destWidth / (float) srcWidth;
            destHeight = (int) (srcHeight * p);
        } else {
            p = (float) destHeight / (float) srcHeight;
            destWidth = (int) (srcWidth * p);
        }
    } else {
        destWidth = srcWidth;
        destHeight = srcHeight;
    }
    if (needsResize) {
        Bitmap output = Bitmap.createScaledBitmap(input, destWidth, destHeight, true);
        return output;
    } else {
        return input;
    }
}

From source file:Main.java

public static Bitmap combineImages(Bitmap bgd, Bitmap fg) {
    Bitmap bmp;/*  w  ww.  java  2  s  .com*/

    int width = bgd.getWidth() > fg.getWidth() ? bgd.getWidth() : fg.getWidth();
    int height = bgd.getHeight() > fg.getHeight() ? bgd.getHeight() : fg.getHeight();

    bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Paint paint = new Paint();
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));

    Canvas canvas = new Canvas(bmp);
    canvas.drawBitmap(bgd, 0, 0, null);
    canvas.drawBitmap(fg, 0, 0, paint);

    return bmp;
}

From source file:Main.java

public static Bitmap combineImages(Bitmap bgd, Bitmap fg) {
    Bitmap bmp;//ww w  .  java2  s .co  m

    int width = bgd.getWidth() > fg.getWidth() ? bgd.getWidth() : fg.getWidth();
    int height = bgd.getHeight() > fg.getHeight() ? bgd.getHeight() : fg.getHeight();

    bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Paint paint = new Paint();
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));

    Canvas canvas = new Canvas(bmp);
    canvas.drawBitmap(bgd, 0, 0, null);
    canvas.drawBitmap(fg, 0, 0, paint);

    return bmp;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), 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);
    final float roundPx = 12;

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

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

    if (null != bitmap) {
        bitmap.recycle();
        bitmap = null;
    }

    return output;
}

From source file:Main.java

public static Rect getBestFitRect(Bitmap bitmap, float ratio) {
    float bmpRatio = (float) bitmap.getWidth() / (float) bitmap.getHeight();

    if (bmpRatio > ratio) // bmp is wider
    {//from   w  ww.j  av  a 2s.  co m
        int height = bitmap.getHeight();
        int width = (int) (height * ratio);
        int offset = (bitmap.getWidth() - width) / 2;
        return new Rect(offset, 0, offset + width, height);
    } else if (bmpRatio < ratio) // bmp is taller
    {
        int width = bitmap.getWidth();
        int height = (int) (width / ratio);
        int offset = (bitmap.getHeight() - height) / 2;
        return new Rect(0, offset, width, offset + height);
    } else
        return new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
}

From source file:Main.java

private static Bitmap scaleBitmap(Bitmap bitmap, float ratio) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.postScale(1f / ratio, 1f / ratio);

    Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    return result;
}

From source file:Main.java

public static Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    if (scaleWidth < scaleHeight) {
        scaleHeight = scaleWidth;//w w  w .j  av a2s.c  o m
    } else {
        scaleWidth = scaleHeight;
    }

    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);

    // "recreate" the new bitmap
    return Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
}

From source file:Main.java

public static Bitmap emboss(Bitmap bmp) {
    int width = bmp.getWidth();
    int height = bmp.getHeight();
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

    int pixR = 0;
    int pixG = 0;
    int pixB = 0;

    int pixColor = 0;

    int newR = 0;
    int newG = 0;
    int newB = 0;

    int[] pixels = new int[width * height];
    bmp.getPixels(pixels, 0, width, 0, 0, width, height);
    int pos = 0;//w  w  w  .j a v a 2s .co  m
    for (int i = 1, length = height - 1; i < length; i++) {
        for (int k = 1, len = width - 1; k < len; k++) {
            pos = i * width + k;
            pixColor = pixels[pos];

            pixR = Color.red(pixColor);
            pixG = Color.green(pixColor);
            pixB = Color.blue(pixColor);

            pixColor = pixels[pos + 1];
            newR = Color.red(pixColor) - pixR + 127;
            newG = Color.green(pixColor) - pixG + 127;
            newB = Color.blue(pixColor) - pixB + 127;

            newR = Math.min(255, Math.max(0, newR));
            newG = Math.min(255, Math.max(0, newG));
            newB = Math.min(255, Math.max(0, newB));

            pixels[pos] = Color.argb(255, newR, newG, newB);
        }
    }

    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}

From source file:Main.java

public static byte[] bitmap2Byte(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }//from  w  ww .  j ava 2 s . com
    int size = bitmap.getWidth() * bitmap.getHeight();
    ByteArrayOutputStream baos = new ByteArrayOutputStream(size);
    bitmap.compress(CompressFormat.JPEG, 100, baos);
    return baos.toByteArray();
}