问题
I have an image of some arbitrary size, and I need to crop it with negative boundary values.
So, basically I have image (1) and I want to crop it to the dimensions of (2).
(1) +---------------------------+ (2) | | +-----------------------------+ | | | | | | | | | | | | | | | | | +-----------------------------+ | | | +---------------------------+
Any ideas on how to solve this in Java?
I've tried the Scalr library, but it doesn't support negative crop boundaries.
回答1:
You don't need a library for this task.
You can create the new image that is resulted this way:
BufferedImage newImage = new BufferedImage(width, height, imageType);
Then you can crop the piece you need from the old image this way:
BufferedImage tempImage = oldImage.getSubimage(0, y, otherWidth, height);
Then, you place the tempImage
in the newImage
:
Graphics2D g2 = newImage.createGraphics();
g2.drawImage(tempImage, x, 0, otherWidth, height, null);
g2.dispose();
width
is the image1 widthheight
is the image1 widthx
is the top intersection point between the 2 images on the x axisy
is the top intersection point between the 2 images on the y axisotherWidth
iswidth - x
回答2:
Cant you just put the image inside a div set to display as block with a set height and width then just position the image to the right of the containing div?
来源:https://stackoverflow.com/questions/12458907/how-can-i-crop-an-image-with-negative-crop-boundaries-in-java