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 void saveBitmap(Bitmap bitmap, File bitmapFile) {
    try {//from   w w w .  j a  va2  s  . co  m
        if (bitmap != null && bitmap.getWidth() > 0 && bitmap.getHeight() > 0) {
            FileOutputStream out = new FileOutputStream(bitmapFile);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static Bitmap getRoundedImage(Bitmap bitmap) {
    Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);

    BitmapShader shader = new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP);
    Paint paint = new Paint();
    paint.setShader(shader);//from   w w  w  . j a v  a  2s.  c o m

    Canvas c = new Canvas(circleBitmap);
    c.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);

    return circleBitmap;
}

From source file:Main.java

public static Bitmap scaleByWidth(Bitmap bitmap, int newWidth) {
    return scale(bitmap, (float) newWidth / bitmap.getWidth());
}

From source file:Main.java

public static Bitmap createResizedBitmap(Bitmap srcBit, int newWidth, int newHeight) {
    int width = srcBit.getWidth();
    int height = srcBit.getHeight();

    // calculate the scale - in this case = 0.4f
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap resizedBitmap = Bitmap.createBitmap(srcBit, 0, 0, width, height, matrix, true);

    width = resizedBitmap.getWidth();/*from  w  w  w.j av a 2 s. c o m*/
    height = resizedBitmap.getHeight();

    Log.i("ImageResize",
            "Image Resize Result : " + Boolean.toString((newHeight == height) && (newWidth == width)));
    return resizedBitmap;
}

From source file:Main.java

public static byte[] bmp2bytesStream(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    if (width % 8 != 0) {
        int adjustWidth = width + (8 - width % 8);
        final Bitmap.Config config = Bitmap.Config.ARGB_8888;
        Bitmap whiteBgBitmap = Bitmap.createBitmap(adjustWidth, height, config);
        Canvas canvas = new Canvas(whiteBgBitmap);
        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(bitmap, 0, 0, null);

        bitmap = whiteBgBitmap;/* w ww  .  j ava 2s. c  o m*/
        width = bitmap.getWidth();
    }
    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
    byte[] image = new byte[width * height];
    final byte WHITE = 0, BLACK = 1;
    for (int i = 0; i < pixels.length; i++) {
        image[i] = (pixels[i] != 0xFFFFFFFF) ? BLACK : WHITE;
    }
    final int COL = width + width % 2;
    byte[] row = new byte[COL];
    byte[] res = new byte[COL / 8 * height];
    for (int i = 0, dex = 0, num = 0; i < height; ++i) {
        System.arraycopy(image, i * width, row, 0, width);
        for (byte e : row) {
            res[dex] += e << (7 - num++);
            num = 8 == num ? 0 : num;
            dex = 0 == num ? dex + 1 : dex;
        }
    }
    return res;
}

From source file:Main.java

public static int hashBitmap(Bitmap bitmap) {
    int hash_result = 0;
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    hash_result = (hash_result << 7) ^ h;
    hash_result = (hash_result << 7) ^ w;
    for (int pixel = 0; pixel < 20; ++pixel) {
        int x = (pixel * 50) % w;
        int y = (pixel * 100) % h;
        hash_result = (hash_result << 7) ^ bitmap.getPixel(x, y);
    }/*from w ww . j  ava  2  s.  c o m*/
    return hash_result;
}

From source file:Main.java

public static Bitmap scaleBitmap(Bitmap originalBitmap, int toWidth, int toHeight) {

    int image_width = originalBitmap.getWidth();
    int image_height = originalBitmap.getHeight();

    float scale = (float) toWidth / (float) image_width;

    if (image_width < image_height) {
        scale = toHeight / image_height;
    }//  w w  w . j a  v a  2s .c o  m

    return Bitmap.createScaledBitmap(originalBitmap, (int) (image_width * scale), (int) (image_height * scale),
            true);

}

From source file:Main.java

/**
 * Since max texture size is 4Kx4K, we have to size it down. Also, no new phones have screens wider than 1440px, but there will be no difference for 1080p width, so we resize to that
 * @param toResize - bitmap to resize//  www  .ja  v  a 2s . c om
 * @return resized bitmap
 */
public static Bitmap resizeForPreview(Bitmap toResize) {
    final int maxSize = 1024;
    int width = toResize.getWidth();
    int height = toResize.getHeight();
    int newWidth = width, newHeight = height;

    if (width > height && width > maxSize) {
        newWidth = maxSize;
        newHeight = (height * maxSize) / width;
    } else if (height > maxSize) {
        newHeight = maxSize;
        newWidth = (width * maxSize) / height;
    }

    return Bitmap.createScaledBitmap(toResize, newWidth, newHeight, false);
}

From source file:Main.java

public static Drawable resizeImage(Bitmap bitmap, int w, int h) {

    // load the origial Bitmap
    Bitmap BitmapOrg = bitmap;

    int width = BitmapOrg.getWidth();
    int height = BitmapOrg.getHeight();
    int newWidth = w;
    int newHeight = h;

    // calculate the scale
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the Bitmap
    matrix.postScale(scaleWidth, scaleHeight);
    // if you want to rotate the Bitmap
    // matrix.postRotate(45);

    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width, height, matrix, true);

    // make a Drawable from Bitmap to allow to set the Bitmap
    // to the ImageView, ImageButton or what ever
    return new BitmapDrawable(resizedBitmap);

}

From source file:Main.java

public static Bitmap ResizeBitmap(Bitmap bitmap, int newWidth) {

    int width = bitmap.getWidth();

    int height = bitmap.getHeight();

    float temp = ((float) height) / ((float) width);

    int newHeight = (int) ((newWidth) * temp);

    float scaleWidth = ((float) newWidth) / width;

    float scaleHeight = ((float) newHeight) / height;

    Matrix matrix = new Matrix();

    // resize the bit map

    matrix.postScale(scaleWidth, scaleHeight);

    //         matrix.postRotate(90);

    Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

    bitmap.recycle();/*from  w w  w. j ava 2  s .c o  m*/

    return resizedBitmap;

}