【发布时间】:2016-07-21 11:01:02
【问题描述】:
有没有办法在 Java 中弯曲 BufferedImage?
我认为如果我将图像裁剪成更小的部分并旋转它们,那么我实际上会弯曲图像,但它似乎不起作用。
这是我创建的方法:
/**
* This is a recursive method that will accept an image the point where the bending will start and the point where the bending will end, as well as the angle of bending
*
* @param original:the original image
* @param startingPoint: the point where the bending should start
* @param endingPoint: the point where the bending should end
* @param radiands: the angle
* @return the bent image
*/
public static BufferedImage getBentImage(BufferedImage original, int startingPoint, int endingPoint, double radians) {
if (startingPoint >= endingPoint)
return original;
int type = BufferedImage.TYPE_INT_ARGB;
int width = original.getWidth();
int height = original.getHeight();
BufferedImage crop = original.getSubimage(0, 0, startingPoint, height);
BufferedImage crop0 = original.getSubimage(startingPoint, 0, width - startingPoint, height);
BufferedImage bendCrop = new BufferedImage(width, height, type);
BufferedImage image = new BufferedImage(width, height, type);
AffineTransform rotation = new AffineTransform();
rotation.translate(0, 0);
rotation.rotate(radians);
Graphics2D g = bendCrop.createGraphics();
g.drawImage(crop0, rotation, null);
g.dispose();
g = image.createGraphics();
g.drawImage(crop, 0, 0, null);
g.drawImage(bendCrop, startingPoint, 0, null);
g.dispose();
return getBentImage(image, startingPoint + 1, endingPoint, radians);
}
这是原图:
这就是getBentImage(image, 200, 220, Math.toRadians(1))的结果:
我期待更接近:
关于如何实际实现getBentImage() 方法的任何想法?
【问题讨论】:
-
不清楚您所说的“弯曲”究竟是什么意思。请提供您尝试应用的转换的准确定义。
-
@AndyTurner 下面的图片没有解释吗?
-
不,这就是我问的原因。变形参数有哪些?起点、终点和弧度参数是什么意思?例如,您传入 1 弧度作为参数,但没有旋转 57 度。如果你“弯曲”一个矩形网格会是什么样子?
-
@AndyTurner 起点是图像开始弯曲的 x 坐标,终点是弯曲停止的位置。 radiands 是曲线的弧度
-
将方法的参数添加到您预期的图像中,这样我们就可以看到什么是什么。
标签: java swing graphics bufferedimage affinetransform