AffineTransform会截断图像,我该怎么办?

AffineTransform会截断图像,我该怎么办?,第1张

AffineTransform会截断图像,我该怎么办?

我不是专业人士,但是为什么不创建一个正确大小的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
您询问:

您能向我解释为什么它必须为[w / 2,w / 2]或[h / 2,h / 2]吗?

为了更好地说明这一点,最好可视化并实际 *** 作矩形

切出矩形纸并将其放在纸上,使其左上角位于纸的左上角,这就是屏幕上的图像。现在,检查您需要旋转矩形1或3象限的位置,以使其新的左上角覆盖纸张的左上角,并且您将看到为什么需要使用[w
/ 2,w / 2]或[h / 2,h / 2]。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5506105.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存