Example usage for android.graphics Bitmap getHeight

List of usage examples for android.graphics Bitmap getHeight

Introduction

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

Prototype

public final int getHeight() 

Source Link

Document

Returns the bitmap's height

Usage

From source file:Main.java

public static Bitmap createBitmapWithAlphaMask(Bitmap bmpSource, Bitmap bmpMask) {
    int width = bmpSource.getWidth();
    int height = bmpSource.getHeight();
    int size = width * height;

    if (width != bmpMask.getWidth() || height != bmpMask.getHeight())
        bmpMask = resize(bmpMask, width, height);

    int[] result = new int[size];
    int[] mask = new int[size];
    bmpSource.getPixels(result, 0, width, 0, 0, width, height);
    bmpMask.getPixels(mask, 0, width, 0, 0, width, height);

    int alphaMask = 0xff000000;
    int colorMask = 0x00ffffff;
    for (int i = 0; i < size; i++) {
        result[i] = (mask[i] & alphaMask) | (result[i] & colorMask);
    }/*from w ww. ja v a2s .com*/

    // ensuring the bitmap is mutable
    Bitmap bmpResult = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    bmpResult.setPixels(result, 0, width, 0, 0, width, height);

    return bmpResult;
}

From source file:Main.java

/**
 * Converts a bitmap image to LCD screen data and returns the screen data as bytes.
 * @param buffer The screen's data buffer.
 * @param offset The byte offset to start writing screen bitmap data at.
 * @param bmp The bitmap image that you want to convert to screen data.
 * @param drawWhite Set to true to draw white pixels, false to draw pixels based on gradient.
 * @return A byte array with pixel data for the SSD1306.
 *//*from  ww  w.ja v a  2s. c  om*/
public static void bmpToBytes(byte[] buffer, int offset, Bitmap bmp, boolean drawWhite) {
    int width = bmp.getWidth();
    int height = bmp.getHeight();

    // Each byte stored in memory represents 8 vertical pixels.  As such, you must fill the
    // memory with pixel data moving vertically top-down through the image and scrolling
    // across, while appending the vertical pixel data by series of 8.
    for (int y = 0; y < height; y += 8) {
        for (int x = 0; x < width; x++) {
            int bytePos = (offset + x) + ((y / 8) * width);

            for (int k = 0; k < 8; k++) {
                if ((k + y < height) && (bytePos < buffer.length)) {
                    int pixel = bmp.getPixel(x, y + k);
                    if (!drawWhite) { // Look at Alpha channel instead
                        if ((pixel & 0xFF) > GRADIENT_CUTOFF) {
                            buffer[bytePos] |= 1 << k;
                        }
                    } else {
                        if (pixel == -1) { // Only draw white pixels
                            buffer[bytePos] |= 1 << k;
                        }
                    }
                }
            }
        }
    }
}

From source file:Main.java

public static Bitmap backsheetImage(Bitmap bm) {

    int width = bm.getWidth();
    int height = bm.getHeight();
    int color;/*w ww .  j a  va 2s  .c o  m*/
    int r, g, b, a;

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

    int[] oldPx = new int[width * height];
    int[] newPx = new int[width * height];

    bm.getPixels(oldPx, 0, width, 0, 0, width, height);

    for (int i = 0; i < oldPx.length; i++) {
        color = oldPx[i];
        r = Color.red(color);
        g = Color.green(color);
        b = Color.blue(color);
        a = Color.alpha(color);

        r = 255 - r;
        g = 255 - g;
        b = 255 - b;

        if (r > 255) {
            r = 255;
        } else if (r < 0) {
            r = 0;
        }

        if (g > 255) {
            g = 255;
        } else if (g < 0) {
            g = 0;
        }

        if (b > 255) {
            b = 255;
        } else if (b < 0) {
            b = 0;
        }

        newPx[i] = Color.argb(a, r, g, b);
    }

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

From source file:Main.java

public static Bitmap olderImage(Bitmap bm) {

    int width = bm.getWidth();
    int height = bm.getHeight();
    int color;/*from   ww  w .  ja va  2s.  c  om*/
    int r, g, b, a;

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

    int[] oldPx = new int[width * height];
    int[] newPx = new int[width * height];

    bm.getPixels(oldPx, 0, width, 0, 0, width, height);

    for (int i = 0; i < oldPx.length; i++) {
        color = oldPx[i];
        r = Color.red(color);
        g = Color.green(color);
        b = Color.blue(color);
        a = Color.alpha(color);

        r = (int) (0.393 * r + 0.769 * g + 0.189 * b);
        g = (int) (0.349 * r + 0.686 * g + 0.168 * b);
        b = (int) (0.272 * r + 0.534 * g + 0.131 * b);

        if (r > 255) {
            r = 255;
        } else if (r < 0) {
            r = 0;
        }

        if (g > 255) {
            g = 255;
        } else if (g < 0) {
            g = 0;
        }

        if (b > 255) {
            b = 255;
        } else if (b < 0) {
            b = 0;
        }

        newPx[i] = Color.argb(a, r, g, b);
    }

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

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 showPicture(Bitmap myBitmap, int rot) {
    Matrix matrix = new Matrix();
    matrix.postRotate(rot);/*from w  w w. j  a  v  a  2 s.co  m*/

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(myBitmap, 320, 320, true);
    return Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix,
            true);
}

From source file:Main.java

public static Bitmap resizeDownIfTooBig(Bitmap bitmap, int targetSize, boolean recycle) {
    int srcWidth = bitmap.getWidth();
    int srcHeight = bitmap.getHeight();
    float scale = Math.max((float) targetSize / srcWidth, (float) targetSize / srcHeight);
    if (scale > 0.5f)
        return bitmap;
    return resizeBitmapByScale(bitmap, scale, recycle);
}

From source file:Main.java

/**
 * Scales an image maintaining aspect ratio
 *
 * @param original  original image/*w  w  w  . j  ava  2s  .  co m*/
 * @param maxWidth  maximum width
 * @param maxHeight maximum height
 * @return a Bitmap representing the thumbnail
 */
public static Bitmap createThumbnailForImage(Bitmap original, int maxWidth, int maxHeight) {
    int width = original.getWidth();
    int height = original.getHeight();

    Log.v("Pictures", "Width and height are " + width + "--" + height);

    if (width > height) {
        // landscape
        float ratio = (float) width / maxWidth;
        width = maxWidth;
        height = (int) (height / ratio);
    } else if (height > width) {
        // portrait
        float ratio = (float) height / maxHeight;
        height = maxHeight;
        width = (int) (width / ratio);
    } else {
        // square
        height = maxHeight;
        width = maxWidth;
    }

    Log.v("Pictures", "after scaling Width and height are " + width + "--" + height);

    return Bitmap.createScaledBitmap(original, width, height, true);
}

From source file:Main.java

public static Bitmap resizeDownBySideLength(Bitmap bitmap, int maxLength, boolean recycle) {
    int srcWidth = bitmap.getWidth();
    int srcHeight = bitmap.getHeight();
    float scale = Math.min((float) maxLength / srcWidth, (float) maxLength / srcHeight);
    if (scale >= 1.0f)
        return bitmap;
    return resizeBitmapByScale(bitmap, scale, recycle);
}

From source file:Main.java

public static Bitmap rotateDrawable(Context context, @DrawableRes int resId, int angle) {
    Bitmap bmpOriginal = BitmapFactory.decodeResource(context.getResources(), resId);
    Bitmap bmResult = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(),
            Bitmap.Config.ARGB_8888);/*from w  w w  .ja  v a2  s . c  o  m*/
    Canvas tempCanvas = new Canvas(bmResult);
    tempCanvas.rotate(angle - 90, bmpOriginal.getWidth() / 2, bmpOriginal.getHeight() / 2);
    tempCanvas.drawBitmap(bmpOriginal, 0, 0, null);
    return bmResult;
}