Android Bitmap Crop crop(Bitmap bitmap, int x, int y, float newWidth, float newHeight)

Here you can find the source of crop(Bitmap bitmap, int x, int y, float newWidth, float newHeight)

Description

Crop from 4 sides.

Parameter

Parameter Description
bitmap the bitmap
x the x
y the y
newWidth the new width
newHeight the new height

Return

the bitmap

Declaration

public static Bitmap crop(Bitmap bitmap, int x, int y, float newWidth,
        float newHeight) 

Method Source Code

//package com.java2s;

import android.graphics.Bitmap;

import android.graphics.Matrix;

public class Main {
    /**/* ww w.j a v  a2  s . c  o  m*/
     * Crop.
     * 
     * @param bitmap
     *            the bitmap
     * @param newWidth
     *            the new width
     * @param newHeight
     *            the new height
     * @return the bitmap
     */
    public static Bitmap crop(Bitmap bitmap, float newWidth, float newHeight) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

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

        // recreate the new Bitmap
        // The following line is the place where you can crop the image.
        // If you do not want to resize image then in above line keep
        // scaledWidth and
        // scaledHeight to 1.
        // Then in te following line, you can change following four parameters
        // 0,0,width,height
        // Thease four parameters mark the four axis readings of bounding box.
        // Change it and
        // see how it works

        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 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 resizedBitmap;
    }

    /**
     * Crop from 4 sides.
     *
     * @param bitmap the bitmap
     * @param x the x
     * @param y the y
     * @param newWidth the new width
     * @param newHeight the new height
     * @return the bitmap
     */
    public static Bitmap crop(Bitmap bitmap, int x, int y, float newWidth,
            float newHeight) {
        //      int width = bitmap.getWidth();
        //      int height = bitmap.getHeight();

        //      Logger.debug(BitmapUtils.class, "WIDTH : " + width + " HEIGHT : "
        //            + height);
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, x, y,
                (int) newWidth, (int) newHeight, null, true);
        return resizedBitmap;
    }
}

Related

  1. createCenterCropBitmap(Bitmap srcBitmap, int dstWidth, int dstHeight, boolean tryRecycleSource)
  2. crop(Bitmap bitmap, float newWidth, float newHeight)
  3. cropBitmapToSquare(Bitmap bitmap, int squareLength)
  4. cropBitmapToSquare(String bitmapPath, int squareLength)
  5. getTrimmedBottom(Bitmap img)
  6. getTrimmedBottom(Bitmap img, int border)