如何在Java中裁剪图像的某些区域?
2022-09-02 03:20:14
我正在尝试执行以下代码:
private void crop(HttpServletRequest request, HttpServletResponse response){
int x = 100;
int y = 100;
int w = 3264;
int h = 2448;
String path = "D:images\\upload_final\\030311175258.jpg";
BufferedImage image = ImageIO.read(new File(path));
BufferedImage out = image.getSubimage(x, y, w, h);
ImageIO.write(out, "jpg", new File(path));
}
但一直给我同样的错误:
java.awt.image.RasterFormatException: (x + width) is outside of Raster
sun.awt.image.ByteInterleavedRaster.createWritableChild(ByteInterleavedRaster.java:1230)
java.awt.image.BufferedImage.getSubimage(BufferedImage.java:1156)
我的错误在哪里?