Skip to content Skip to sidebar Skip to footer

Resize Image From File

I am uploading an image to a server and before that happens, I would like to resize the image dimensions. I get the image with a URI like this: Constants.currImageURI = data.getDat

Solution 1:

Use Bitmap.createScaledBitmap as others suggested.

However, this function is not very smart. If you're scaling to less than 50% size, you're likely to get this:

enter image description here

Instead of this:

enter image description here

Do you see bad antialiasing of 1st image? createScaledBitmap will get you this result.

Reason is pixel filtering, where some pixels are completely skipped from source if scaling to < 50%.

To get 2nd quality result, you need to halve the Bitmap's resolution if it's more than 2x larger than desired result, then finally you make call to createScaledBitmap.

And there're more approaches to halve (or quarter, or eighten) images. If you have Bitmap in memory, you recursively call Bitmap.createScaledBitmap to halve image.

If you load image from JPG file, implementation is even faster: you use BitmapFactory.decodeFile and setup Options parameter properly, mainly field inSampleSize, which controls subsambling of loaded images, utilizing JPEG's characteristics.

Many apps which provide image thumbnails use blindly Bitmap.createScaledBitmap, and the thumbnails are just ugly. Be smart and use proper image downsampling.

Solution 2:

This is taken from ThinkAndroid at this url: http://thinkandroid.wordpress.com/2009/12/25/resizing-a-bitmap/

I would look into the possibility of creating a Bitmap or Drawable from the resource and if you want to change it's size use the code below.

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    intwidth= bm.getWidth();
    intheight= bm.getHeight();
    floatscaleWidth= ((float) newWidth) / width;
    floatscaleHeight= ((float) newHeight) / height;

    // Create a matrix for the manipulationMatrixmatrix=newMatrix();

    // Resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);

    // Recreate the new BitmapBitmapresizedBitmap= Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
    return resizedBitmap;

}

EDIT: As suggested in other comment Bitmap.createScaledBitmap should be used for better quality when resizing.

Solution 3:

How to resize a bitmap:

Bitmap.createScaledBitmap(src, dstWidth, dstHeight, filter)

Solution 4:

See what Google recommends doing (as @Pointer Null advised):

publicintcalculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of imagefinalintheight= options.outHeight;
    finalintwidth= options.outWidth;
    intinSampleSize=1;

    if (height > reqHeight || width > reqWidth) {

        finalinthalfHeight= height / 2;
        finalinthalfWidth= width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both// height and width larger than the requested height and width.while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

I call the above to resize a large image:

// Check if source and destination exist// Check if we have read/write permissionsintdesired_width=200;
intdesired_height=200;

BitmapFactory.Optionsoptions=newBitmapFactory.Options();
options.inJustDecodeBounds = true;

BitmapFactory.decodeFile(SOME_PATH_TO_LARGE_IMAGE, options);

options.inSampleSize = calculateInSampleSize(options, desired_width, desired_height);
options.inJustDecodeBounds = false;

Bitmapsmaller_bm= BitmapFactory.decodeFile(src_path, options);

FileOutputStream fOut;
try {
    Filesmall_picture=newFile(SOME_PATH_STRING);
    fOut = newFileOutputStream(small_picture);
    // 0 = small/low quality, 100 = large/high quality
    smaller_bm.compress(Bitmap.CompressFormat.JPEG, 50, fOut);
    fOut.flush();
    fOut.close();
    smaller_bm.recycle();
} catch (Exception e) {
    Log.e(LOG_TAG, "Failed to save/resize image due to: " + e.toString());
}

Post a Comment for "Resize Image From File"