Commit d85cf16b by Juho Juopperi

fix UserBean.forceCrop()

1 parent e267ae4b
......@@ -327,38 +327,41 @@ public class UserBean implements UserBeanLocal {
}
private BufferedImage forceCrop(BufferedImage source) {
int x, y, xl, yl, xh, yh, xc, yc, x0, y0, x1, y1;
double ar = CardPrintBean.ASPECT_RATIO; // x/y
x = source.getWidth();
y = source.getHeight();
xc = x / 2;
yc = y / 2;
if (y >= x) {
xl = x;
yl = (int) (y * ((double) x / (double) y));
int targetWidth, targetHeight;
double targetRatio = CardPrintBean.ASPECT_RATIO; // x/y
int sourceWidth = source.getWidth();
int sourceHeight = source.getHeight();
int sourceCenterX = sourceWidth / 2;
int sourceCenterY = sourceHeight / 2;
int sourceTop, sourceLeft, sourceRight, sourceBottom;
double sourceRatio = (double)sourceWidth / (double)sourceHeight;
if (sourceRatio > targetRatio) {
// the pic is too wide - reduce width
targetWidth = (int)((double)sourceHeight * targetRatio);
targetHeight = sourceHeight;
// crop box coords - calculate left and right
sourceLeft = sourceCenterX - (targetWidth / 2);
sourceTop = 0;
sourceRight = sourceCenterX + (targetWidth / 2);
sourceBottom = sourceHeight;
} else {
xl = (int) (x * ((double) y / (double) x));
yl = y;
// the pic is too tall - reduce height
targetWidth = sourceWidth;
targetHeight = (int)((double)sourceWidth / targetRatio);
// crop box coords - calculate top and bottom
sourceLeft = 0;
sourceTop = sourceCenterY - (targetHeight / 2);
sourceRight = sourceWidth;
sourceBottom = sourceCenterY + (targetHeight / 2);
}
xh = (int) ((xl / 2) * ar);
yh = yl / 2;
x0 = xc - xh;
x1 = xc + xh;
y0 = yc - yh;
y1 = yc + yh;
int cix = (int) (((double) xl) * ar);
int ciy = yl;
BufferedImage cropped = new BufferedImage(cix, ciy, source.getType());
BufferedImage cropped = new BufferedImage(targetWidth, targetHeight, source.getType());
Graphics2D g = cropped.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(source, 0, 0, cix, ciy, x0, y0, x1, y1, null);
g.drawImage(source, 0, 0, targetWidth, targetHeight, sourceLeft, sourceTop, sourceRight, sourceBottom, null);
g.dispose();
return cropped;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!