如何克隆图像?
我有一个图像。我需要精确复制它并将其保存到 BufferedImage,但没有 Image.clone()。该东西应该位于计算循环内,因此它应该非常快,没有逐像素复制。执行此操作的最佳性能方法是什么?
I have an Image. I need to make a exactly copy of it and save it to BufferedImage, but there is no Image.clone(). The thing should be inside a calculating loop and so it should be really fast, no pixel-by-pixel copying. What's the best in perfomance method to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以绘制缓冲图像,因此创建一个空白 bufferedImage,从中创建图形上下文,然后将原始图像绘制到其中。
You can draw to a buffered image, so make a blank bufferedImage, create a graphics context from it, and draw your original image to it.
还有另一种方法:
There is another way:
Image clone = origin.getScaledInstance(original.getWidth(), -1, Image.SCALE_DEFAULT);
这可能不是很漂亮,但是
getScaledInstance
返回,顾名思义,原始Image
对象的实例。通常仅用于调整大小。-1
告诉方法保持纵横比不变Image clone = original.getScaledInstance(original.getWidth(), -1, Image.SCALE_DEFAULT);
This might not be very pretty, but
getScaledInstance
returns, as the name suggests, an instance of your originalImage
object. Usually only used for resizing.-1
tells the method to keep the aspect ratio as it is您可以创建一个方法来返回要克隆的图像的子图像。
例如:
You can create a method that returns the subimage of the image you want to clone.
Such as: