0

I'm capturing an image with the camera, then I'm saving it into a Bitmap. I'm looking forward to center-crop that image, to 600x600px.

Something like this I've found:

https://stackoverflow.com/a/6909144/1943607

However, I can't find out how can I set a fixed with & height.

I'm soo bad at drawing images and canvas. It seems to be so abstract for me.

Thank you.

Community
  • 1
  • 1
Reinherd
  • 5,476
  • 7
  • 51
  • 88

2 Answers2

1

This is exactly what you need.

// Returns an immutable bitmap from the specified subset of the source bitmap.
static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height)



I don't really know if (0,0) is top-left or center, but I believe it's top-left.

* (0,0)

*~~~~~~+===========+
'      '           |
'      ' 200       |
'      '           |
+~~~~~~+           | 400
|  100             |
|                  |
|                  |
+==================+
        300


If it's indeed the center then:

x should be width /2
y    >>     height/2

Otherwise if it's top left:

x should be width /2 - cropWidth/2
y    >>     height/2 - cropHeight/2

On both occasions would look like this.

* (150,200)

+==================+
|                  |
|     +~~~~~~+     |
|     '      '     |
|     '  *   '200  | 400
|     '      '     |
|     +~~~~~~+     |
|       100        |
+==================+
        300
gifnoc-gkp
  • 1,506
  • 1
  • 8
  • 17
  • got something like `newBitmap = Bitmap.createBitmap(oldBitmap, x, y, 500, 500);`. As you can see, x and y are missing. I'm not sure about how to calculate it. – Reinherd Jul 09 '13 at 22:08
1

600x600px

if(srcBmp.getWidth()>600 && srcBmp.getHeight()>600) 
    dstBmp = Bitmap.createBitmap(srcBmp, 
         srcBmp.getWidth()/2 - 600/2,
         srcBmp.getHeight()/2 - 600/2,
         600,
         600);

I change the code from the link you gave, hope this works.

Gina
  • 902
  • 8
  • 15