我有一个图像,我想移动一些 x、y 值然后保存。我的问题是我想保留我的原始尺寸,以便在移动图像后留下 x 和 y “空白”空间。
另外,有什么办法可以将“空白”空间设置为黑色?
示例:我将 600x600 的图像向下移动 45 并向左移动 30,这样图像仍然是 600x600,但结果是“空白”空间的高度为 45,宽度为 30。
到目前为止,我一直在使用getSubimage方法BufferedImage来尝试解决这个问题,但我似乎无法恢复到原始尺寸。
关于如何解决这个问题的任何想法?
我有一个图像,我想移动一些 x、y 值然后保存。我的问题是我想保留我的原始尺寸,以便在移动图像后留下 x 和 y “空白”空间。
另外,有什么办法可以将“空白”空间设置为黑色?
示例:我将 600x600 的图像向下移动 45 并向左移动 30,这样图像仍然是 600x600,但结果是“空白”空间的高度为 45,宽度为 30。
到目前为止,我一直在使用getSubimage方法BufferedImage来尝试解决这个问题,但我似乎无法恢复到原始尺寸。
关于如何解决这个问题的任何想法?
您可以通过创建一个新的缓冲图像并在其上绘图来做到这一点。
// 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);
希望这有效。
public BufferedImage shiftImage(BufferedImage original, int x, int y) {
BufferedImage result = new BufferedImage(original.getWidth() + x,
original.getHeight() + y, original.getType());
Graphics2D g2d = result.createGraphics();
g2d.drawImage(original, x, y, null);
return result;
}
应该管用。
保存
public void SaveImage(BufferedImage image, String filename) {
File outputfile = new File(filename + ".png");
try {
ImageIO.write(image, "png", outputfile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}