(y +高度)在用Java裁剪图像时,在光栅外面。

时间:2021-06-14 18:28:45
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');

i am not sure what to do here to get the cropped image?

我不确定在这里做什么来得到裁剪的图像?

 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)

1 个解决方案

#1


0  

Looks like you are confusing the x and y coordinates. Also, totalWidth + marginLeft and totalheight + margintop does't seem right. Ie. x = 2533 + (-134) = 2399, width = 413, y = 1900 + (-67) = 1833, height = 296 will result in a rectangle partly outside your raster (and the same exception you got initially).

看起来你混淆了x和y坐标。同时,totalWidth +边缘化和totalheight + margintop似乎不正确。Ie。x = 2533 +(-134) = 2399,宽度= 413,y = 1900 +(-67) = 1833,高度= 296,将会出现在你的光栅外的矩形(和你最初得到的相同的例外)。

Try printing the x, y, width and height values passed to the getSubImage method, and you'll probably see what's wrong. I'm not sure if I fully understand what you want to do, but I think you want something like:

尝试打印传递给getSubImage方法的x、y、width和height值,您可能会看到错误。我不确定我是否完全理解你想要做什么,但我认为你想要的是:

image.getSubimage(-Integer.valueOf(marginleft).intValue(),           // x
                  -Integer.valueOf(margintop).intValue(),            // y
                  Integer.valueOf(width).intValue(),                 // width
                  Integer.valueOf(height).intValue());               // height

#1


0  

Looks like you are confusing the x and y coordinates. Also, totalWidth + marginLeft and totalheight + margintop does't seem right. Ie. x = 2533 + (-134) = 2399, width = 413, y = 1900 + (-67) = 1833, height = 296 will result in a rectangle partly outside your raster (and the same exception you got initially).

看起来你混淆了x和y坐标。同时,totalWidth +边缘化和totalheight + margintop似乎不正确。Ie。x = 2533 +(-134) = 2399,宽度= 413,y = 1900 +(-67) = 1833,高度= 296,将会出现在你的光栅外的矩形(和你最初得到的相同的例外)。

Try printing the x, y, width and height values passed to the getSubImage method, and you'll probably see what's wrong. I'm not sure if I fully understand what you want to do, but I think you want something like:

尝试打印传递给getSubImage方法的x、y、width和height值,您可能会看到错误。我不确定我是否完全理解你想要做什么,但我认为你想要的是:

image.getSubimage(-Integer.valueOf(marginleft).intValue(),           // x
                  -Integer.valueOf(margintop).intValue(),            // y
                  Integer.valueOf(width).intValue(),                 // width
                  Integer.valueOf(height).intValue());               // height