在java中旋转图像
我正在寻找旋转图像。我有一个在java中旋转图像,java,swing,image-processing,bufferedimage,affinetransform,Java,Swing,Image Processing,Bufferedimage,Affinetransform,我正在寻找旋转图像。我有一个JInternalFrame,其中包含一个JLabel。标签包含图像。旋转图像后,我需要调整内部框架的大小。我当前使用的代码旋转图像,但图像边缘有黑色,并且偏离中心。有没有关于如何解决这个问题的建议 public void rotateIcon(int angle) { int w = theLabel.getIcon().getIconWidth(); int h = theLabel.getIcon().getIconHeight(
JInternalFrame
,其中包含一个JLabel
。标签包含图像。旋转图像后,我需要调整内部框架的大小。我当前使用的代码旋转图像,但图像边缘有黑色,并且偏离中心。有没有关于如何解决这个问题的建议
public void rotateIcon(int angle)
{
int w = theLabel.getIcon().getIconWidth();
int h = theLabel.getIcon().getIconHeight();
int type = BufferedImage.TYPE_INT_RGB; // other options, see api
BufferedImage DaImage = new BufferedImage(h, w, type);
Graphics2D g2 = DaImage.createGraphics();
double x = (h - w)/2.0;
double y = (w - h)/2.0;
AffineTransform at = AffineTransform.getTranslateInstance(x, y);
at.rotate(Math.toRadians(angle), w/2.0, h/2.0);
g2.drawImage(new ImageIcon(getData()).getImage(), at, theLabel);
g2.dispose();
theLabel.setIcon(new ImageIcon(DaImage));
this.setSize(DaImage.getWidth(),DaImage.getHeight()); //resize the frame
}
您可以尝试使用。如果您更改:
BufferedImage DaImage=新的BufferedImage(高度、宽度、类型)代码>
致:
BufferedImage DaImage=新的BufferedImage(**宽度,高度**,类型)代码>?您需要使用三角学来确定正确的宽度/高度,使用透明度来防止黑色区域,我认为变换是错误的,这使它偏离中心
试试这个:
public static BufferedImage rotate(BufferedImage image, double angle) {
double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
int w = image.getWidth(), h = image.getHeight();
int neww = (int)Math.floor(w*cos+h*sin), newh = (int) Math.floor(h * cos + w * sin);
GraphicsConfiguration gc = getDefaultConfiguration();
BufferedImage result = gc.createCompatibleImage(neww, newh, Transparency.TRANSLUCENT);
Graphics2D g = result.createGraphics();
g.translate((neww - w) / 2, (newh - h) / 2);
g.rotate(angle, w / 2, h / 2);
g.drawRenderedImage(image, null);
g.dispose();
return result;
}
private static GraphicsConfiguration getDefaultConfiguration() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
return gd.getDefaultConfiguration();
}
从基于前面的示例,但实际使用最新的JDK并在无头模式下:
public static BufferedImage rotate(BufferedImage image, double angle) {
double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
int w = image.getWidth(), h = image.getHeight();
int neww = (int)Math.floor(w*cos+h*sin), newh = (int) Math.floor(h * cos + w * sin);
BufferedImage result = deepCopy(image, false);
Graphics2D g = result.createGraphics();
g.translate((neww - w) / 2, (newh - h) / 2);
g.rotate(angle, w / 2, h / 2);
g.drawRenderedImage(image, null);
g.dispose();
return result;
}
public static BufferedImage deepCopy(BufferedImage bi, boolean copyPixels) {
ColorModel cm = bi.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
WritableRaster raster = bi.getRaster().createCompatibleWritableRaster();
if (copyPixels) {
bi.copyData(raster);
}
return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
通常,旋转图像会更改宽度和高度(相对于X轴和Y轴)。我猜这是导致它“偏离中心”的原因。我必须通过计算新的尺寸并考虑它来解决这个问题。至于黑色边缘,这是一种非常常见的情况,旋转函数不使用alpha通道。也许这将有助于定位:请看,这真的很酷。不过我有一个问题。我的图像只需要旋转90度。一次。如果我通过90度的角度,它实际上会旋转90度以上这是使用弧度,所以先用数学。toRadians(度)。我们能摆脱对屏幕设备的依赖吗?是的,我前后切换了这些,仍然没有成功。谢谢,我将不得不研究这个问题。