Android Utililty Methods Bitmap Crop

List of utility methods to do Bitmap Crop

Description

The list of methods to do Bitmap Crop are organized into topic(s).

Method

BitmapcreateCenterCropBitmap(Bitmap srcBitmap, int dstWidth, int dstHeight, boolean tryRecycleSource)
create Center Crop Bitmap
if (srcBitmap == null || dstWidth == 0 || dstHeight == 0) {
    return srcBitmap;
int srcWidth = srcBitmap.getWidth();
int srcHeight = srcBitmap.getHeight();
Bitmap dstBitmap = srcBitmap;
try {
    if ((dstHeight / dstWidth) - (srcHeight / srcWidth) > 0) {
...
Bitmapcrop(Bitmap bitmap, float newWidth, float newHeight)
Crop.
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width,
        height, matrix, true);
...
Bitmapcrop(Bitmap bitmap, int x, int y, float newWidth, float newHeight)
Crop from 4 sides.
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, x, y,
        (int) newWidth, (int) newHeight, null, true);
return resizedBitmap;
BitmapcropBitmapToSquare(Bitmap bitmap, int squareLength)
crop Bitmap To Square
if (bitmap != null) {
    int imageSquareLength = Math.min(bitmap.getWidth(),
            bitmap.getHeight());
    int croppedLength = Math.min(imageSquareLength, squareLength);
    float scale = (float) croppedLength / imageSquareLength;
    Matrix matrix = new Matrix();
    matrix.setScale(scale, scale);
    try {
...
BitmapcropBitmapToSquare(String bitmapPath, int squareLength)
crop Bitmap To Square
Bitmap bitmap = decodeSampledBitmapFromFile(bitmapPath,
        squareLength, squareLength);
return cropBitmapToSquare(bitmap, squareLength);
intgetTrimmedBottom(Bitmap img)
returns blank area of the image to the downward direction of the image
int width = img.getWidth();
int height = img.getHeight();
int data = 0;
for (int i = 0; i < width; ++i) {
    for (int j = height - 1; j >= 0; --j) {
        if (img.getPixel(i, j) != Color.TRANSPARENT && j > data) {
            data = j;
            break;
...
intgetTrimmedBottom(Bitmap img, int border)
returns blank area of the image to the downward direction of the image
int width = img.getWidth();
int height = img.getHeight();
int data = 0;
for (int i = 0; i < width; ++i) {
    for (int j = height - 1; j >= 0; --j) {
        if (img.getPixel(i, j) != Color.TRANSPARENT && j > data) {
            data = j;
            break;
...
intgetTrimmedLeft(Bitmap img)
returns blank area of the image to the left direction of the image
int width = img.getWidth();
int height = img.getHeight();
int data = width;
for (int i = 0; i < height; ++i) {
    for (int j = 0; j < width; ++j) {
        if (img.getPixel(j, i) != Color.TRANSPARENT && j < data) {
            data = j;
            break;
...
intgetTrimmedLeft(Bitmap img, int border)
returns blank area of the image to the left direction of the image
int width = img.getWidth();
int height = img.getHeight();
int data = width;
for (int i = 0; i < height; ++i) {
    for (int j = 0; j < width; ++j) {
        if (img.getPixel(j, i) != Color.TRANSPARENT && j < data) {
            data = j;
            break;
...
intgetTrimmedRight(Bitmap img)
returns blank area of the image to the right direction of the image
int width = img.getWidth();
int height = img.getHeight();
int data = 0;
for (int i = 0; i < height; ++i) {
    for (int j = width - 1; j >= 0; --j) {
        if (img.getPixel(j, i) != Color.TRANSPARENT && j > data) {
            data = j;
            break;
...