2013-07-26 116 views
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'); 

我不确定在这里要做什么来获得裁剪后的图像?(y +高度)在裁剪过程中在栅格图像之外由Java

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) 

回答

0

看起来像你在混淆x和y坐标。另外,totalWidth + marginLefttotalheight + 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