gpt4 book ai didi

java - 如何在java中弯曲图像

转载 作者:行者123 更新时间:2023-11-30 08:00:28 24 4
gpt4 key购买 nike

有什么方法可以在 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);
}

这是原始图像:

original Image

这是 getBentImage(image, 200, 220, Math.toRadians(1)) 的结果:

result

我期待更接近于:

expected result

关于如何实际实现 getBentImage() 方法有什么想法吗?

最佳答案

正如评论中所建议的,一种简单的方法是将图像分为 3 部分:

  1. 与原件相同。
  2. 根据弯曲变换进行弯曲。
  3. 恒定对角线连续。

这是一个快速但有点困惑的示例,它显示了原始形状及其下方的结果形状。我只是为图像使用了标签图标,而不是进行自定义绘画。 (此外,我没有遵守使用 final 变量的 Java 命名约定,因为它是数学而不是典型的编码。)

由于计算代码中的变量比较多,所以我在最后加了一个草图,说明变量代表什么。

enter image description here

public class Main extends JFrame {

static BufferedImage image;

public static void main(String[] args) {

try {
image = ImageIO.read(ClassLoader.getSystemResource("img.png"));
} catch (IOException e) {
e.printStackTrace();
}
new Main();
}

public Main() {

getContentPane().setLayout(new BorderLayout(5, 10));
BufferedImage img2 = transform(15, 100, 300);

JLabel label1 = new JLabel(new ImageIcon(image));
label1.setHorizontalAlignment(JLabel.LEFT);
label1.setOpaque(true);
label1.setBackground(Color.YELLOW);
add(label1, BorderLayout.NORTH);

JLabel label2 = new JLabel(new ImageIcon(img2));
label2.setHorizontalAlignment(JLabel.LEFT);
label2.setOpaque(true);
label2.setBackground(Color.CYAN);
add(label2);

pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}

static BufferedImage transform(int t, int x1, int x2) {

final double TH = Math.toRadians(t);
final int D = x2 - x1;
final int W = image.getWidth();
final int H = image.getHeight();

final int dD = (int) (D / (2 * TH) * Math.sin(2 * TH));
final int dH = (int) (D / TH * Math.pow(Math.sin(TH), 2));
final int pH = (int) ((W - x2) * Math.tan(2 * TH));

final int width = W - (D - dD);
final int height = (int) (H + dH + pH);

System.out.println(W + " " + H + " -> " + width + " " + height);

BufferedImage img2 = new BufferedImage(width, height, image.getType());

for (int x = 0; x < x1; x++) {
for (int y = 0; y < H; y++) {
int rgb = image.getRGB(x, y);
img2.setRGB(x, y, rgb);
}
}

for (int x = x1; x < x2; x++) {
for (int y = 0; y < H; y++) {
int rgb = image.getRGB(x, y);
int dx = (int) (D / (2 * TH) * Math.sin(2 * (x-x1) * TH / D));
int dy = (int) (D / TH * Math.pow(Math.sin((x-x1) * TH / D), 2));
img2.setRGB(x1 + dx, y + dy, rgb);
}
}

for (int x = x2; x < W; x++) {
for (int y = 0; y < H; y++) {
int rgb = image.getRGB(x, y);
int dp = (int) ((x - x2) * Math.tan(2 * TH));
img2.setRGB(x - (D - dD), y + dH + dp, rgb);
}
}

return img2;
}
}

enter image description here

关于计算,我留给你做作业;它只是属于 Math.SE 而不是 SO 的几何/三角学。如果你想不通,我会给你一个方向。

请注意,此方法可能一点也不快,当然可以优化,我也会把它留给你。哦,粗心地将 double 舍入为 int,所以结果不是像素完美的。

关于java - 如何在java中弯曲图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38502480/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com