1
I want to crop  image like below.


 +------------------+
 |                  |
 |  +-----------+   |
 |  |           |   |
 |  |           |   |
 |  |           |   |
 |  |           |   |
 |  +-----------+   |
 |                  |
 +------------------+


style="width: 413px; height: 296px; margin-left: -134px; margin-top: -67px;"

             var height = ele.css('height');
     var width = ele.css('width');
     var margin_left = ele.css('margin-left');
     var margin_top = ele.css('margin-top');

我不知道该怎么做才能得到裁剪的图像?

 final int totalheight = image.getHeight();  //1900
 final int totalwidth = image.getWidth();  //2533
       image.getSubimage(totalheight+Integer.valueOf(margintop).intValue(), totalwidth+Integer.valueOf(marginleft).intValue(), Integer.valueOf(width).intValue(),Integer.valueOf(height).intValue());


java.awt.image.RasterFormatException: (y + height) is outside of Raster
at sun.awt.image.ByteInterleavedRaster.createWritableChild(Unknown Source)
at java.awt.image.BufferedImage.getSubimage(Unknown Source)
4

1 回答 1

0

看起来你混淆了 x 和 y 坐标。还有,totalWidth + marginLeft而且totalheight + margintop好像不太对。IE。x = 2533 + (-134) = 2399, width = 413, y = 1900 + (-67) = 1833, height = 296 将导致矩形部分超出您的栅格(以及您最初遇到的相同异常)。

尝试打印传递给该getSubImage方法的 x、y、宽度和高度值,您可能会发现问题所在。我不确定我是否完全理解你想要做什么,但我认为你想要这样的东西:

image.getSubimage(-Integer.valueOf(marginleft).intValue(),           // x
                  -Integer.valueOf(margintop).intValue(),            // y
                  Integer.valueOf(width).intValue(),                 // width
                  Integer.valueOf(height).intValue());               // height
于 2013-08-09T14:31:09.777 回答