3

I have very large bitmap image. My source

BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), null, o);

            // The new size we want to scale to
            final int REQUIRED_WIDTH = 1000;
            final int REQUIRED_HIGHT = 500;
            // Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_WIDTH
                    && o.outHeight / scale / 2 >= REQUIRED_HIGHT)
                scale *= 2;

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);

i want to resize image correctly, i need resize image to maximum available size

for example

i downloaded image size 4000x4000 px and my phone supported 2000x1500 px size i need anderstend how size suported my phone? then i resize image to 2000x1500 (for example)

TN888
  • 7,659
  • 9
  • 48
  • 84
Max Usanin
  • 2,479
  • 6
  • 40
  • 70

2 Answers2

0

Here you have good to resize bitmap to maximum avaliabe size :

public void onClick() //for example 
{
/*
Getting screen diemesions
*/
WindowManager w = getWindowManager();
        Display d = w.getDefaultDisplay();
        int width = d.getWidth();
        int height = d.getHeight(); 
// "bigging" bitmap
Bitmap nowa = getResizedBitmap(yourbitmap, width, height);
}

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {

int width = bm.getWidth();

int height = bm.getHeight();

float scaleWidth = ((float) newWidth) / width;

float scaleHeight = ((float) newHeight) / height;

// create a matrix for the manipulation

Matrix matrix = new Matrix();

// resize the bit map

matrix.postScale(scaleWidth, scaleHeight);

// recreate the new Bitmap

Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

return resizedBitmap;

}

I hope I helped

TN888
  • 7,659
  • 9
  • 48
  • 84
  • metrics.widthPixels - this is display size ... display size is much less than those that I want – Max Usanin Feb 05 '13 at 15:12
  • @MaxUsanin sorry, I changed this. Look at it. – TN888 Feb 06 '13 at 17:17
  • This answer is still based on maximum screen size, not maximum bitmap size to be displayed. What needs to be found is the maximum bitmap size, which on a lot of phones is 2048x2048, but this needs to be found programatically. – Pork 'n' Bunny Jun 17 '13 at 06:36
0

Not So Perfect But here is My solution to Do That,

fun setScaleImageWithConstraint(bitmap: Bitmap): Bitmap {

        val maxWidth = 500 //max Height Constraint
        val maxHeight = 500 //max Width Constraint

        var width = bitmap.width
        var height = bitmap.height

        if (width > maxWidth || height > maxHeight) {
            //If Image is still Large than allowed W/H Half the Size
            val quarterWidth = ((width / 2).toFloat() + (width / 3).toFloat()).toInt()
            val quarterHeight = ((height / 2).toFloat() + (height / 3).toFloat()).toInt()
            val scaledBitmap = Bitmap.createScaledBitmap(bitmap, quarterWidth, quarterHeight, false)
            //Recursive Call to Resize and return Resized One
            return setScaleImageWithConstraint(scaledBitmap)
        } else {
            //This will be executed when Image is not violating the Constraints
            return bitmap
        }
    }

Reduce Bitmap 1 Quarter Size Recursively Until the Allowed With And Height is reached.

Jawad Zeb
  • 523
  • 4
  • 18