How can I crop an image with negative crop-boundaries in Java?

微笑、不失礼 提交于 2019-12-24 11:18:03

问题


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 width
  • height is the image1 width
  • x is the top intersection point between the 2 images on the x axis
  • y is the top intersection point between the 2 images on the y axis
  • otherWidth is width - 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!