Example usage for android.graphics Rect Rect

List of usage examples for android.graphics Rect Rect

Introduction

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

Prototype

public Rect(int left, int top, int right, int bottom) 

Source Link

Document

Create a new rectangle with the specified coordinates.

Usage

From source file:Main.java

/**
 * Save jpeg image with background color
 * @param strFileName Save file path//from  www  .  j  a va 2 s.c o m
 * @param bitmap Input bitmap
 * @param nQuality Jpeg quality for saving
 * @param nBackgroundColor background color
 * @return whether success or not
 */
public static boolean saveBitmapJPEGWithBackgroundColor(String strFileName, Bitmap bitmap, int nQuality,
        int nBackgroundColor) {
    boolean bSuccess1 = false;
    boolean bSuccess2 = false;
    boolean bSuccess3;
    File saveFile = new File(strFileName);

    if (saveFile.exists()) {
        if (!saveFile.delete())
            return false;
    }

    int nA = (nBackgroundColor >> 24) & 0xff;

    // If Background color alpha is 0, Background color substitutes as white
    if (nA == 0)
        nBackgroundColor = 0xFFFFFFFF;

    Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(newBitmap);
    canvas.drawColor(nBackgroundColor);
    canvas.drawBitmap(bitmap, rect, rect, new Paint());

    // Quality limitation min/max
    if (nQuality < 10)
        nQuality = 10;
    else if (nQuality > 100)
        nQuality = 100;

    OutputStream out = null;

    try {
        bSuccess1 = saveFile.createNewFile();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        out = new FileOutputStream(saveFile);
        bSuccess2 = newBitmap.compress(CompressFormat.JPEG, nQuality, out);
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        if (out != null) {
            out.flush();
            out.close();
            bSuccess3 = true;
        } else
            bSuccess3 = false;

    } catch (IOException e) {
        e.printStackTrace();
        bSuccess3 = false;
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    return (bSuccess1 && bSuccess2 && bSuccess3);
}

From source file:Main.java

/**
 * Save jpeg image with background color
 * @param strFileName Save file path//from   www . ja va 2  s . c o m
 * @param bitmap Input bitmap
 * @param nQuality Jpeg quality for saving
 * @param nBackgroundColor background color
 * @return whether success or not
 */
public static boolean saveBitmapJPEGWithBackgroundColor(String strFileName, Bitmap bitmap, int nQuality,
        int nBackgroundColor) {
    boolean bSuccess1 = false;
    boolean bSuccess2 = false;
    boolean bSuccess3;
    File saveFile = new File(strFileName);

    if (saveFile.exists()) {
        if (!saveFile.delete())
            return false;
    }

    int nA = (nBackgroundColor >> 24) & 0xff;

    // If Background color alpha is 0, Background color substitutes as white
    if (nA == 0)
        nBackgroundColor = 0xFFFFFFFF;

    Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(newBitmap);
    canvas.drawColor(nBackgroundColor);
    canvas.drawBitmap(newBitmap, rect, rect, new Paint());
    canvas.drawBitmap(bitmap, rect, rect, new Paint());

    // Quality limitation min/max
    if (nQuality < 10)
        nQuality = 10;
    else if (nQuality > 100)
        nQuality = 100;

    OutputStream out = null;

    try {
        bSuccess1 = saveFile.createNewFile();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        out = new FileOutputStream(saveFile);
        bSuccess2 = newBitmap.compress(CompressFormat.JPEG, nQuality, out);
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        if (out != null) {
            out.flush();
            out.close();
            bSuccess3 = true;
        } else
            bSuccess3 = false;

    } catch (IOException e) {
        e.printStackTrace();
        bSuccess3 = false;
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    return (bSuccess1 && bSuccess2 && bSuccess3);
}

From source file:Main.java

private static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, float offset,
        boolean clipShadow, Paint paint) {

    int scaledWidth = width;
    int scaledHeight = height;

    final Canvas canvas = new Canvas();
    canvas.translate(offset / 2.0f, offset / 2.0f);

    Bitmap bitmap;/*from  ww w  .j  a  v a  2s . co  m*/

    final Rect from = new Rect(x, y, x + width, y + height);
    final RectF to = new RectF(0, 0, width, height);

    if (m == null || m.isIdentity()) {
        bitmap = Bitmap.createBitmap(scaledWidth + (int) offset,
                scaledHeight + (int) (clipShadow ? (offset / 2.0f) : offset), Bitmap.Config.ARGB_8888);
        paint = null;
    } else {
        RectF mapped = new RectF();
        m.mapRect(mapped, to);

        scaledWidth = Math.round(mapped.width());
        scaledHeight = Math.round(mapped.height());

        bitmap = Bitmap.createBitmap(scaledWidth + (int) offset,
                scaledHeight + (int) (clipShadow ? (offset / 2.0f) : offset), Bitmap.Config.ARGB_8888);
        canvas.translate(-mapped.left, -mapped.top);
        canvas.concat(m);
    }

    canvas.setBitmap(bitmap);
    canvas.drawRect(0.0f, 0.0f, width, height, paint);
    canvas.drawBitmap(source, from, to, SCALE_PAINT);

    return bitmap;
}

From source file:Main.java

/**
 * Creates a bitmap with rounded corners
 *
 * @param bitmap    source bitmap/*  www .  j  ava 2s . c  o  m*/
 * @param maxHeight maximal height for result bitmap
 * @param pixels    corner radius
 * @return a new bitmap if succeed, nil instead
 */
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int maxHeight, int pixels) {
    if (bitmap == null || sApplicationContext == null) {
        return bitmap;
    }
    maxHeight = (int) (sApplicationContext.getResources().getDisplayMetrics().density * maxHeight);
    float scale = bitmap.getHeight() * 1.0f / maxHeight;
    int newWidth = (int) (bitmap.getWidth() / scale);

    Bitmap output = Bitmap.createBitmap(newWidth, maxHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

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

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, (float) pixels, (float) pixels, paint);

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

    return output;
}

From source file:Main.java

public static Bitmap getCircularBitmap(Bitmap bm) {
    int size = 192;

    Bitmap bitmap = ThumbnailUtils.extractThumbnail(bm, size, size);

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

    Canvas canvas = new Canvas(output);

    final int color = 0xffff0000;
    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);/*w w  w. j a va2 s .  c  om*/
    paint.setDither(true);
    paint.setFilterBitmap(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawOval(rectF, paint);

    paint.setColor(Color.BLUE);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth((float) 4);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

From source file:Main.java

/**
 * Method for finding the flag given its index.
 * @param index The index of the flag of one of the 32 countries.
 * @param width The width of the source image.
 * @return Rect//ww  w.j  a  v a  2 s .  c  om
 * 
 * <p>
 * Usage for BitmapRegiondDecoder.
 *  It decodes a region of a bitmap. 
 *  It can be very useful if you only need a region of a large image.
 *  <p>
 */
public static Rect getRectForIndex(int index, int width, int countAllImages, int countPerRow, int space) {
    // check if index is valid
    if (index < 0 && index >= countAllImages)
        throw new IllegalArgumentException("Index must be between 0 and 31.");

    // calculate one side of a single flag
    int oneSide = (width - ((countPerRow + 1) * space)) / countPerRow;

    // calculate the row and col of the given index
    int row = (index / countPerRow);
    int col = (index % countPerRow);

    // left and right sides of the rectangle
    int left = (oneSide * col) + (space * (col + 1));
    int right = left + oneSide;

    // top and bottom sides of the rectangle
    int top = (oneSide * row) + (space * (row + 1));
    int bottom = top + oneSide;

    // the resulting rectangle
    return new Rect(left, top, right, bottom);
}

From source file:Main.java

public static Bitmap get_circular_bitmap(Bitmap bitmap) {
    Bitmap output;//w w w. j ava 2 s  . com

    if (bitmap.getWidth() > bitmap.getHeight()) {
        output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    } else {
        output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getWidth(), 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());

    float r = 0;

    if (bitmap.getWidth() > bitmap.getHeight()) {
        r = bitmap.getHeight() / 2;
    } else {
        r = bitmap.getWidth() / 2;
    }

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawCircle(r, r, r, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

From source file:Main.java

/** Return a bitmap identical to src with rounded corners. */
public static final Bitmap roundCornersRGB888(Bitmap src, int roundPixles) {
    final Bitmap output = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    // TODO: document what is special about this color
    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, src.getWidth(), src.getHeight());
    final RectF rectF = new RectF(rect);

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

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(src, 0, 0, paint);

    return output;
}

From source file:Main.java

public static Bitmap toRoundBitmap(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    float roundPx;
    float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
    if (width <= height) {
        roundPx = width / 2;/*from w  w w . jav a2  s. c o  m*/
        left = 0;
        top = 0;
        right = width;
        bottom = width;
        height = width;
        dst_left = 0;
        dst_top = 0;
        dst_right = width;
        dst_bottom = width;
    } else {
        roundPx = height / 2;
        float clip = (width - height) / 2;
        left = clip;
        right = width - clip;
        top = 0;
        bottom = height;
        width = height;
        dst_left = 0;
        dst_top = 0;
        dst_right = height;
        dst_bottom = height;
    }
    Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final Paint paint = new Paint();
    final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);
    final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
    final RectF rectF = new RectF(dst);
    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, src, dst, paint);
    return output;
}

From source file:Main.java

/**
 * Convert touch position x:y to {@link Camera.Area} position -1000:-1000 to 1000:1000.
 *//*from  www  .  jav a2  s .com*/
private static Rect calculateTapArea(Camera camera, float x, float y, float coefficient) {
    float focusAreaSize = 300;
    int areaSize = Float.valueOf(focusAreaSize * coefficient).intValue();

    int centerX = (int) (x / getResolution(camera).width - 1000);
    int centerY = (int) (y / getResolution(camera).height - 1000);

    int left = clamp(centerX - areaSize / 2, -1000, 1000);
    int top = clamp(centerY - areaSize / 2, -1000, 1000);

    RectF rectF = new RectF(left, top, left + areaSize, top + areaSize);

    return new Rect(Math.round(rectF.left), Math.round(rectF.top), Math.round(rectF.right),
            Math.round(rectF.bottom));
}