的AffineTransform截断的形象,做我自己做错了什么? [英] AffineTransform truncates image, what do I wrong?
问题描述
我这里,我想用旋转90的AffineTransform度尺寸2156x1728的黑/白PNG文件。由此产生的图像不具有正确的比例。这里是一些例子code(因为我已成功加载PNG文件到的BufferedImage):
I have here an black/white png file of the dimensions 2156x1728 which I want to rotate 90 degrees using AffineTransform. The resulting image doesn't have the right proportions. Here some example code (given I have successfully loaded the png file into the 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;
}
的输出是相应
输入宽度:2156
输入高度:1728
显示宽度:1942年
致使高度1942
怎么来的旋转回报等完全无关的尺寸是多少?
How comes that the rotation returns such completely unrelated dimensions?
推荐答案
我不是在这一个职业球员,但为什么不创建正确大小的BufferedImage?还要注意的是你的革命的中心是不正确。你将需要旋转过的中央[W / 2,W / 2]或[H / 2,H / 2](W为宽度和h是高度),这取决于象限你旋转到,1或3和相对高度和图像的宽度。例如:
I'm not a pro at this, but why not just create a BufferedImage of the correct size? Also note that your center of revolution is incorrect. You will need to rotate over a center of [w/2, w/2] or [h/2, h/2] (w being width and h being height) depending on which quadrant you're rotating to, 1 or 3, and the relative height and width of the image. For instance:
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 结果
你问:
Edit 1
You asked:
能不能请你解释为什么它必须是[W / 2,W / 2]或[H / 2,H / 2]?
can you explain to me why it must be [w/2, w/2] or [h/2, h/2] ?
要解释这个最好的,这是最好的可视化和物理操纵矩形:
To explain this best, it's best to visualize and physically manipulate a rectangle:
切出一块长方形的纸,将其放置在一张纸上,这样它的左上角是在一张纸的左上角 - 这就是你的屏幕上的图像。现在,检查一下,你会需要旋转该矩形1或3象限以便其新的左上角上覆的纸,你就会明白为什么你需要使用[W / 2,W / 2]或[H / 2,H / 2]。
Cut out rectangular piece of paper and place it on a piece of paper such that its upper left corner is on the upper left corner of the piece of paper -- that's your image on the screen. Now check to see where you would need to rotate that rectangle 1 or 3 quadrants so that its new upper left corner is overlying that of the paper, and you'll see why you need to use [w/2, w/2] or [h/2, h/2].
这篇关于的AffineTransform截断的形象,做我自己做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!