java 如何从缓冲图像中获取子图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5813900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:54:28  来源:igfitidea点击:

How to get sub image from buffered image

javaimage

提问by Jagan

We can get a sub image from BufferedImage using getSubimage(int,int,int,int), but my problem is I want to get exact subimage(rectangle image) by passing double values as widthand height. Is there any alternative for that ?

我们可以使用 BufferedImage 获取子图像getSubimage(int,int,int,int),但我的问题是我想通过将 double 值传递为widthand来获得精确的子图像(矩形图像)height。有什么替代方法吗?

回答by camickr

cast the double values to int.

将双精度值转换为 int。

getSubImage((int)x, (int)y, (int)width, (int)height);

and as @camickr mentioned The internal cells will still be represented by integerpixel values. If each cell is 5 pixels square. The first cell starts at (0, 0), the second cell starts at (5, 0) etc...

正如@camickr 所提到的,内部单元格仍将由整数像素值表示。如果每个单元格是 5 个像素的正方形。第一个单元格从 (0, 0) 开始,第二个单元格从 (5, 0) 开始,等等......