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 formatBitmap(Bitmap bitmap, int size) {

    float width = bitmap.getWidth();
    float height = bitmap.getHeight();
    float rota = size / height;

    int widthnew = (int) ((size * width) / height);
    // int wh = Math.max(bitmap.getWidth(), bitmap.getHeight());
    Bitmap mBitmap = Bitmap.createBitmap(widthnew, size, Bitmap.Config.ARGB_8888);
    Matrix matrix = new Matrix();
    matrix.postScale(rota, rota);//from  w ww  .ja  va 2  s .co  m
    Canvas canvas = new Canvas(mBitmap);
    canvas.drawBitmap(bitmap, matrix, null);

    // bitmap = Bitmap.createScaledBitmap(mBitmap, width, height, true);
    return mBitmap;
}

From source file:Main.java

public static Bitmap zoomBitmap(Bitmap bitmap, float res) {
    if (bitmap == null) {
        return null;
    }/*from  w  w w.  j a  v a2s  .  c o  m*/
    int width, height;
    width = (int) (bitmap.getWidth() * res);
    height = (int) (bitmap.getHeight() * res);
    Bitmap newbmp = Bitmap.createScaledBitmap(bitmap, width, height, true);
    return newbmp;
}

From source file:Main.java

public static Bitmap createScaledToFillBitmap(Bitmap bitmap, int reqWidth, int reqHeight) {
    final int height = bitmap.getHeight();
    final int width = bitmap.getWidth();

    float widthScaleF = (float) width / (float) reqWidth;
    float heightScaleF = (float) height / (float) reqHeight;
    if (widthScaleF < heightScaleF) {
        return Bitmap.createScaledBitmap(bitmap, reqWidth, (int) (height / widthScaleF), true);
    } else {/*from  w  w w.j a v a 2 s .c  o m*/
        return Bitmap.createScaledBitmap(bitmap, (int) (width / heightScaleF), reqHeight, true);
    }
}

From source file:Main.java

public static Bitmap rotateWithCanvas(Bitmap bitmap, int degrees) {

    int destWidth, destHeight;

    float centerX = bitmap.getWidth() / 2;
    float centerY = bitmap.getHeight() / 2;

    // We want to do the rotation at origin, but since the bounding
    // rectangle will be changed after rotation, so the delta values
    // are based on old & new width/height respectively.
    Matrix matrix = new Matrix();
    matrix.preTranslate(-centerX, -centerY);
    matrix.postRotate(degrees);//from  w  ww  .j ava  2 s  .  c om
    if (degrees / 90 % 2 == 0) {
        destWidth = bitmap.getWidth();
        destHeight = bitmap.getHeight();
        matrix.postTranslate(centerX, centerY);
    } else {
        destWidth = bitmap.getHeight();
        destHeight = bitmap.getWidth();
        matrix.postTranslate(centerY, centerX);
    }
    Bitmap cropped = Bitmap.createBitmap(destWidth, destHeight, Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(cropped);
    canvas.drawBitmap(bitmap, matrix, null);
    return cropped;
}

From source file:Main.java

public static Bitmap createReflectionBitmap(Bitmap bitmap) {
    final int reflectionGap = 4;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);// w w  w  .j av a2s.com

    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint deafalutPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);

    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, Shader.TileMode.CLAMP);
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}

From source file:Main.java

static public void setImageColorPixels(ImageView view, Bitmap myBitmap, int rgbcolor)// ,Bitmap sourceBitmap)
{

    int intArray[];

    intArray = new int[myBitmap.getWidth() * myBitmap.getHeight()];

    // copy pixel data from the Bitmap into the 'intArray' array
    myBitmap.getPixels(intArray, 0, myBitmap.getHeight(), 0, 0, myBitmap.getHeight(), myBitmap.getWidth());

    // replace the red pixels with yellow ones
    for (int i = 0; i < intArray.length; i++) {
        // System.out.println("color is--" + i + " " + intArray[i]);
        if (intArray[i] != Color.TRANSPARENT) {

            intArray[i] = rgbcolor;/*w w w. ja  v a  2 s.  c  om*/
        }
    }
    myBitmap.setPixels(intArray, 0, myBitmap.getHeight(), 0, 0, myBitmap.getHeight(), myBitmap.getWidth());

    view.setImageBitmap(myBitmap);
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int backgroundColor, int borderColor) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth() + 12, bitmap.getHeight() + 12,
            Bitmap.Config.ARGB_8888);//from   ww w. j  a v  a  2s .c o m

    Canvas canvas = new Canvas(output);
    //canvas.drawARGB(Color.alpha(backgroundColor), Color.red(backgroundColor), Color.green(backgroundColor), Color.blue(backgroundColor));

    Paint borderPaint = new Paint();
    borderPaint.setAntiAlias(true);
    borderPaint.setColor(borderColor);
    borderPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
    borderPaint.setShadowLayer(2.0f, 0.0f, 2.0f, Color.BLACK);

    int centerWidth = output.getWidth() / 2;
    int centerHeight = output.getHeight() / 2;
    canvas.drawCircle(centerWidth, centerHeight, ((centerWidth + centerHeight) / 2) - 4, borderPaint);

    Paint paint = new Paint();
    paint.setAntiAlias(true);

    Rect rectS = new Rect(0, 0, output.getWidth() - 12, output.getHeight() - 12);
    Rect rectD = new Rect(0, 0, output.getWidth(), output.getHeight());

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
    canvas.drawBitmap(bitmap, rectS, rectD, paint);

    return output;
}

From source file:Main.java

public static Bitmap imageRotate(Bitmap bitmap, int angle) {

    Matrix mat = new Matrix();
    mat.postRotate(angle);//  www .j  a  va  2  s . c  o m

    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mat, true);

}

From source file:Main.java

public static Bitmap cropJpgFile(String inFl, String outFl) throws IOException {
    Bitmap bmpIn = BitmapFactory.decodeFile(inFl);
    Bitmap bmOverlay = Bitmap.createBitmap(bmpIn.getWidth(), bmpIn.getHeight(), Bitmap.Config.ARGB_8888);
    Paint p = new Paint();
    p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    Canvas c = new Canvas(bmOverlay);
    c.drawBitmap(bmpIn, 0, 0, null);//from  w ww  .  j  av  a2s  . c o m
    //c.drawRect(30, 30, 100, 100, p);
    File fileOut = new File(outFl);
    FileOutputStream out = new FileOutputStream(fileOut);
    bmOverlay = drawTextToBitmap(bmOverlay, "Image Viewer");
    bmOverlay.compress(Bitmap.CompressFormat.JPEG, 100, out);
    return bmOverlay;
}

From source file:Main.java

public static Bitmap RotateBitmap(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);/*from ww  w .  j  a  v a  2  s .  c om*/
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}