Rotate BufferedImage显示黑色边界

时间:2014-08-03 11:49:26

标签: java swing rotation bufferedimage jcomponent


我想旋转一个bufferedImage。我使用的代码使图像旋转成为可能。但它将图像切割成正方形。屏幕显示黑色"边框"在左侧和右侧。
如果我使用调试工具,图像宽度大约是整个宽度包括黑色" border"。但黑色"边界"不要旋转,它总是在左侧和右侧。并且图像左右缺少图像部分。如果我再次旋转,则平方图像不会再次被切割。如果我改变了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));
     }
  } 

1 个答案:

答案 0 :(得分:2)

旋转图像时,图像的大小会发生变化。

以下是一些可以使用的代码:

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);
                }
            }
        });
    }
}

只需更改要阅读的图像,然后使用滑块旋转图像。

相关问题