如何在Java中裁剪图像的某些区域?

发布于 2024-10-20 02:24:26 字数 711 浏览 2 评论 0 原文

我正在尝试执行以下代码:

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)

我的错误在哪里?

I'm trying do the following code:

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));

}

But keeps giving me the same error:

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)

Where is my mistake ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盗梦空间 2024-10-27 02:24:26

我最初的猜测是你的 (x + w) > image.getWidth()。

如果打印出image.getWidth(),是3264吗? :O

你目前正在做的是这样的:

<-- 3264 ------>
+--------------+
|    orig      | +-- Causing the problem
|              | V
|   +--------------+
|100| overlap  |   |
|   |          |   |
|   |          |   |
+---|----------+   |
    |              |
    |    out       |
    +--------------+

如果你想修剪掉原稿的顶角,并且只是得到“重叠”那么你需要做

BufferedImage out = image.getSubimage(x, y, w-x, h-y);

如果你想这样做:

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

那么你需要做这:

BufferedImage out = image.getSubimage(x, y, w-2*x, h-2*y);

My initial guess is that your (x + w) > image.getWidth().

If you print out image.getWidth(), is it 3264? :O

What you're currently doing is this:

<-- 3264 ------>
+--------------+
|    orig      | +-- Causing the problem
|              | V
|   +--------------+
|100| overlap  |   |
|   |          |   |
|   |          |   |
+---|----------+   |
    |              |
    |    out       |
    +--------------+

If you're trying to trim off the top corner of orig, and just get "overlap" then you need to do

BufferedImage out = image.getSubimage(x, y, w-x, h-y);

If you're trying to do this:

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

Then you need to do this:

BufferedImage out = image.getSubimage(x, y, w-2*x, h-2*y);
西瑶 2024-10-27 02:24:26

对于那些只想在软件上进行裁剪和其他基本图像处理功能的人,我建议使用图像处理库。通常,实现是优化且稳定的。

一些Java图像处理库:ImageJ马文JMagickJIU JH 实验室imgscalr

另一个优点是让事情变得简单。只需几行代码就可以完成很多事情。在下面的示例中,我使用 Marvin Framework 进行裁剪。

原文:
输入图像描述此处

裁剪:
输入图片描述此处

来源:

MarvinImage image = MarvinImageIO.loadImage("./res/famousFace.jpg");
crop(image.clone(), image, 60, 32, 182, 62);
MarvinImageIO.saveImage(image, "./res/famousFace_cropped.jpg");

For those who just want cropping and other basic image manipulation features on your software I recommend to use an image processing library. Usually the implementations are optimized and stable.

Some Java image processing libraries: ImageJ, Marvin, JMagick, JIU, JH Labs, imgscalr.

Another advantage is to keep things simple on your side. You can do a lot of things with just a few lines of code. In the example below, I used Marvin Framework for cropping.

Original:
enter image description here

Cropped:
enter image description here

Source:

MarvinImage image = MarvinImageIO.loadImage("./res/famousFace.jpg");
crop(image.clone(), image, 60, 32, 182, 62);
MarvinImageIO.saveImage(image, "./res/famousFace_cropped.jpg");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文