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

intgetTrimmedRight(Bitmap img, int border)
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;
...
intgetTrimmedTop(Bitmap img)
returns blank area of the image to the up direction of the image
int width = img.getWidth();
int height = img.getHeight();
int data = height;
for (int i = 0; i < width; ++i) {
    for (int j = 0; j < height; ++j) {
        if (img.getPixel(i, j) != Color.TRANSPARENT && j < data) {
            data = j;
            break;
...
intgetTrimmedTop(Bitmap img, int border)
returns blank area of the image to the up direction of the image
int width = img.getWidth();
int height = img.getHeight();
int data = height;
for (int i = 0; i < width; ++i) {
    for (int j = 0; j < height; ++j) {
        if (img.getPixel(i, j) != Color.TRANSPARENT && j < data) {
            data = j;
            break;
...
BitmapcutImg(File file, int newWidth, int newHeight)
cut Img
Bitmap resizeBmp = null;
if (newWidth <= 0 || newHeight <= 0) {
    throw new IllegalArgumentException("0");
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file.getPath(), opts);
int srcWidth = opts.outWidth;
...
BitmapcutStretchImage(Bitmap image, int xsize, int ysize)
cuts the Bitmap into four corners and stretches the space between them.
Bitmap data = Bitmap.createBitmap((int) (xsize), (int) (ysize),
        image.getConfig());
Canvas canvas = new Canvas(data);
canvas.drawBitmap(
        image,
        new Rect(0, 0, image.getWidth() / 2, image.getHeight() / 2),
        new Rect(0, 0, (int) (image.getWidth() / 2), (int) (image
                .getHeight() / 2)), null);
...
BitmapcutImg(Bitmap bitmap, int newWidth, int newHeight)
cut Img
if (bitmap == null) {
    return null;
if (newWidth <= 0 || newHeight <= 0) {
    throw new IllegalArgumentException("????????????????0");
Bitmap resizeBmp = null;
try {
...
BitmapcropCenter(Bitmap bitmap, boolean recycle)
crop Center
int width = bitmap.getWidth();
int height = bitmap.getHeight();
if (width == height)
    return bitmap;
int size = Math.min(width, height);
Bitmap target = Bitmap.createBitmap(size, size, getConfig(bitmap));
Canvas canvas = new Canvas(target);
canvas.translate((size - width) / 2, (size - height) / 2);
...
BitmapcropCenterBitmap(Bitmap bitmap, int newWidth, int newHeight)
crop Center Bitmap
int width = bitmap.getWidth();
int height = bitmap.getHeight();
if (newWidth >= width || newHeight >= height) {
    return bitmap;
int startX = (width - newWidth) / 2;
int startY = (height - newHeight) / 2;
Bitmap result = Bitmap.createBitmap(bitmap, startX, startY,
...
BitmapresizeDownAndCropCenter(Bitmap bitmap, int size, boolean recycle)
resize Down And Crop Center
int w = bitmap.getWidth();
int h = bitmap.getHeight();
int minSide = Math.min(w, h);
if (w == h && minSide <= size)
    return Bitmap.createBitmap(bitmap);
size = Math.min(size, minSide);
float scale = Math.max((float) size / bitmap.getWidth(),
        (float) size / bitmap.getHeight());
...
BitmapresizeDownIfTooBig(Bitmap bitmap, int targetSize, boolean recycle)
resize Down If Too Big
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);