0

I'm trying to crop and scale given bitmap,But only scaling works.
What am I doing wrong?

private Bitmap CropAndShrinkBitmap(Bitmap io_BitmapFromFile, int i_NewWidth, int i_NewHeight) 
{
    int cuttingOffset = 0;
    int currentWidth = i_BitmapFromFile.getWidth();
    int currentHeight = i_BitmapFromFile.getHeight();

    if(currentWidth > currentHeight)
    {
        cuttingOffset = currentWidth - currentHeight;
        Bitmap.createBitmap(i_BitmapFromFile, cuttingOffset/2, 0, currentWidth - cuttingOffset, currentHeight);
    }
    else
    {
        cuttingOffset = i_NewHeight - currentWidth;
        Bitmap.createBitmap(i_BitmapFromFile, 0, cuttingOffset/2, currentWidth, currentHeight - cuttingOffset);
    }
    Bitmap fixedBitmap = Bitmap.createScaledBitmap(i_BitmapFromFile, i_NewWidth, i_NewHeight, false)  ;

    return i_BitmapFromFile;
}

The description say that: "createBitmap Returns an immutable bitmap ".
What is that mean? and is it the cause to my problem?

Rami
  • 2,098
  • 5
  • 25
  • 37

2 Answers2

1

Bitmaps are by default "immutable", meaning you can't alter them. You need to create a "mutable" bitmap, which is editable.

Check out these links on how to do so:

BitmapFactory.decodeResource returns a mutable Bitmap in Android 2.2 and an immutable Bitmap in Android 1.6

http://sudarnimalan.blogspot.com/2011/09/android-convert-immutable-bitmap-into.html

Community
  • 1
  • 1
jmhend
  • 537
  • 1
  • 6
  • 16
1

The cropping likely works fine, but the resulting Bitmap object that is cropped is returned from createBitmap(), the original object is not modified (as was mentioned, because the Bitmap instance is immutable). If you want the cropped result, you have to grab the return value.

Bitmap cropped = Bitmap.createBitmap(i_BitmapFromFile, cuttingOffset/2, 0, currentWidth - cuttingOffset, currentHeight);

Then you can do any further work you would like with that result.

HTH

devunwired
  • 62,780
  • 12
  • 127
  • 139