gpt4 book ai didi

java - AffineTransform 截断图像,我错了什么?

转载 作者:行者123 更新时间:2023-12-02 07:47:37 26 4
gpt4 key购买 nike

我这里有一个尺寸为 2156x1728 的黑白 png 文件,我想使用 AffineTransform 将其旋转 90 度。生成的图像比例不正确。这里是一些示例代码(假设我已成功将 png 文件加载到 BufferedImage 中):

public BufferedImage transform(BufferedImage image){

System.out.println("Input width: "+ image.getWidth());
System.out.println("Input height: "+ image.getHeight());

AffineTransform affineTransform = new AffineTransform();
affineTransform.setToQuadrantRotation(1, image.getWidth() / 2, image.getHeight() / 2);

AffineTransformOp opRotated = new AffineTransformOp(affineTransform, AffineTransformOp.TYPE_BILINEAR);
BufferedImage transformedImage = opRotated.createCompatibleDestImage(image, image.getColorModel());
System.out.println("Resulting width: "+ transformedImage.getWidth());
System.out.println("Resulting height: "+ transformedImage.getHeight());

transformedImage = opRotated.filter(image, transformedImage);
return transformedImage;
}

相应的输出是:

Input width: 2156

Input height: 1728

Resulting width: 1942

Resulting height: 1942

为什么旋转会返回如此完全不相关的维度?

最佳答案

我不是这方面的专家,但为什么不创建一个正确大小的 BufferedImage 呢?另请注意,您的旋转中心不正确。您需要以 [w/2, w/2] 或 [h/2, h/2] 为中心旋转(w 是宽度,h 是高度),具体取决于您要旋转到的象限,1 或 3 ,以及图像的相对高度和宽度。例如:

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class RotateImage {
public static final String IMAGE_PATH = "http://duke.kenai.com/"
+ "models/Duke3DprogressionSmall.jpg";

public static void main(String[] args) {
try {
URL imageUrl = new URL(IMAGE_PATH);
BufferedImage img0 = ImageIO.read(imageUrl);
ImageIcon icon0 = new ImageIcon(img0);

int numquadrants = 1;
BufferedImage img1 = transform(img0, numquadrants );
ImageIcon icon1 = new ImageIcon(img1);

JOptionPane.showMessageDialog(null, new JLabel(icon0));
JOptionPane.showMessageDialog(null, new JLabel(icon1));

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static BufferedImage transform(BufferedImage image, int numquadrants) {
int w0 = image.getWidth();
int h0 = image.getHeight();
int w1 = w0;
int h1 = h0;

int centerX = w0 / 2;
int centerY = h0 / 2;

if (numquadrants % 2 == 1) {
w1 = h0;
h1 = w0;
}

if (numquadrants % 4 == 1) {
if (w0 > h0) {
centerX = h0 / 2;
centerY = h0 / 2;
} else if (h0 > w0) {
centerX = w0 / 2;
centerY = w0 / 2;
}
// if h0 == w0, then use default
} else if (numquadrants % 4 == 3) {
if (w0 > h0) {
centerX = w0 / 2;
centerY = w0 / 2;
} else if (h0 > w0) {
centerX = h0 / 2;
centerY = h0 / 2;
}
// if h0 == w0, then use default
}

AffineTransform affineTransform = new AffineTransform();
affineTransform.setToQuadrantRotation(numquadrants, centerX, centerY);

AffineTransformOp opRotated = new AffineTransformOp(affineTransform,
AffineTransformOp.TYPE_BILINEAR);

BufferedImage transformedImage = new BufferedImage(w1, h1,
image.getType());

transformedImage = opRotated.filter(image, transformedImage);
return transformedImage;
}
}

编辑 1
您问:

can you explain to me why it must be [w/2, w/2] or [h/2, h/2] ?

为了最好地解释这一点,最好可视化并物理操作一个矩形:

剪出一张矩形纸并将其放在一张纸上,使其左上角位于该纸的左上角上 - 这就是您在屏幕上的图像。现在检查一下需要将矩形旋转 1 或 3 个象限的位置,以便其新的左上角覆盖纸张的左上角,您就会明白为什么需要使用 [w/2, w/2] 或[h/2,h/2]。

关于java - AffineTransform 截断图像,我错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8719473/

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