gpt4 book ai didi

java - 旋转 BufferedImage 显示黑色边界

转载 作者:行者123 更新时间:2023-12-02 05:34:46 30 4
gpt4 key购买 nike


我想旋转 bufferedImage。我使用的代码可以使图像旋转。但它将图像切割成正方形。屏幕的左侧和右侧会显示一个黑色“边框”。
如果我使用调试工具,图像宽度大约是包括黑色“边框”的整个宽度。但黑色“边框”不会旋转,它始终位于左侧和右侧。并且图像缺少左右部分的图片。如果我再次旋转方形图像,则不会再次剪切它。如果我更改 src.getWidth() 部分,则每次旋转图像都会变小。

  private static BufferedImage rotateImage(BufferedImage src, double degrees) {
AffineTransform affineTransform = AffineTransform.getRotateInstance(
Math.toRadians(degrees), (src.getWidth() / 2), (src.getHeight() / 2));

BufferedImage rotatedImage = new BufferedImage(src.getWidth(), src.getHeight(), src.getType());
Graphics2D g = (Graphics2D) rotatedImage.getGraphics();
g.setTransform(affineTransform);
g.drawImage(src, 0, 0, null);
return rotatedImage;
}

public void rotateImage(int degree) {
if (this.image != null) {
this.setImage(myJComponent.rotateImage(this.image, degree));
}
}

最佳答案

图像的大小会随着旋转而改变。

这里是一些可以使用的代码:

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;

public class Rotation
{
BufferedImage image;
JLabel label;

public Rotation(BufferedImage image)
{
this.image = image;
}

private BufferedImage getImage(double theta)
{
// Determine the size of the rotated image

double cos = Math.abs(Math.cos(theta));
double sin = Math.abs(Math.sin(theta));
double width = image.getWidth();
double height = image.getHeight();
int w = (int)(width * cos + height * sin);
int h = (int)(width * sin + height * cos);

// Rotate and paint the original image onto a BufferedImage

BufferedImage out = new BufferedImage(w, h, image.getType());
Graphics2D g2 = out.createGraphics();
g2.setPaint(UIManager.getColor("Panel.background"));
g2.fillRect(0,0,w,h);
double x = w/2;
double y = h/2;
AffineTransform at = AffineTransform.getRotateInstance(theta, x, y);
x = (w - width)/2;
y = (h - height)/2;
at.translate(x, y);
g2.drawRenderedImage(image, at);
g2.dispose();
return out;
}

private JLabel getLabel()
{
ImageIcon icon = new ImageIcon(image);
label = new JLabel(icon);
label.setHorizontalAlignment(JLabel.CENTER);
return label;
}

private JSlider getSlider()
{
final JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 360, 0);
slider.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
int value = slider.getValue();
BufferedImage bi = getImage(Math.toRadians(value));
label.setIcon(new ImageIcon(bi));
}
});
return slider;
}

public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
String path = "mong.jpg";
ClassLoader cl = Rotation.class.getClassLoader();
BufferedImage bi = ImageIO.read(cl.getResourceAsStream(path));
Rotation r = new Rotation(bi);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JScrollPane(r.getLabel()));
f.getContentPane().add(r.getSlider(), "South");
f.pack();
f.setLocation(200,200);
f.setVisible(true);
}
catch(IOException e)
{
System.out.println(e);
}
}
});
}
}

只需更改您要读取的图像并使用 slider 旋转图像即可。

关于java - 旋转 BufferedImage 显示黑色边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25104349/

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