0

I have faced trouble how to find center of an image and write it to different image. Situation is that I have image (for e.g. 800X500) and I need to crop exactly (50X50 or 70X70) center of it with img.getSubimage(x, y, w, h).

Sizes will be different every time for new image.

How could I calculate X and Y to get 50X50 subImage from the image center?

Thanks in advance.

img.getSubimage(x, y, w, h) - X and Y are not coordinates of center. * @param x the X coordinate of the upper-left corner of the * specified rectangular region * @param y the Y coordinate of the upper-left corner of the * specified rectangular region

mibru
  • 3
  • 3

1 Answers1

5
int bigwidth = 800;
int bigheight = 500;
int cropwidth = 70;
int cropheight = 70;
img.getSubimage((bigwidth - cropwidth) / 2, (bigheight - cropheight) / 2, cropwidth, cropheight);
C. K. Young
  • 219,335
  • 46
  • 382
  • 435