我有一张图片,我想将其移动一些 x、y 值然后保存。我的问题是我想保留原始尺寸,以便在移动图像后留下 x 和 y“空白”空间。
还有,有什么方法可以将“空白”区域设置为黑色吗?
示例:我将一张 600x600 的图像向下移动 45 并向左移动 30,这样图像仍然是 600x600,但结果具有 45 英寸的高度和 30 英寸的“空白”空间。
到目前为止,我一直在使用 BufferedImage
的 getSubimage
方法来尝试解决这个问题,但我似乎无法恢复到原始尺寸。
关于如何解决这个问题有什么想法吗?
最佳答案
您可以通过创建一个新的缓冲图像并在其上绘图来做到这一点。
// Create new buffered image
BufferedImage shifted = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// Create the graphics
Graphics2D g = shifted.createGraphics();
// Draw original with shifted coordinates
g.drawImage(original, shiftx, shifty, null);
希望这能奏效。
关于java - 在保持尺寸的同时移动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16401779/