Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing a bitmap

The code below resizes a bitmap and keeps the aspect ratio. I was wondering if there is a more efficient way of resizing, because i got the idea that i'm writing code that is already available in the android API.

private Bitmap resizeImage(Bitmap bitmap, int newSize){
    int width = bitmap.getWidth();
    int height = bitmap.getHeight(); 

    int newWidth = 0;
    int newHeight = 0;

    if(width > height){
        newWidth = newSize;
        newHeight = (newSize * height)/width;
    } else if(width < height){
        newHeight = newSize;
        newWidth = (newSize * width)/height;
    } else if (width == height){
        newHeight = newSize;
        newWidth = newSize;
    }

    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); 

    return resizedBitmap;
}
like image 416
Oritm Avatar asked Mar 06 '12 12:03

Oritm


People also ask

How do I resize a bitmap image?

❓ How can I resize a BMP image? First, you need to add a BMP image file: drag & drop your BMP image file or click inside the white area to choose a file. Then adjust resize settings, and click the "Resize" button. After the process completes, you can download your result file.

How do you change the width and height of a bitmap image on android?

scaleToFitWidth(bitmap, 100); public static Bitmap scaleToFitWidth(Bitmap b, int width) { float factor = width / (float) b. getWidth(); return Bitmap. createScaledBitmap(b, width, (int) (b. getHeight() * factor), true); } // Scale and maintain aspect ratio given a desired height // BitmapScaler.

What is bitmap factory How will you use it to rescale the image?

Use BitmapFactory. Options. inSampleSize when you first load your image to get the size as close as possible to your target size, then use Bitmap. createScaledBitmap to scale to the exact size you want.

How do I resize a JPEG on Android?

Tap the image you want to adjust. You can adjust the size of an image or rotate it: Resize: Touch and drag the squares along the edges.


1 Answers

Use the method Bitmap.createScaledBitmap() :)

like image 78
Jave Avatar answered Oct 02 '22 10:10

Jave