简体   繁体   English

旋转BufferedImage显示黑色边界

[英]Rotate BufferedImage shows black bound


I want to rotate a bufferedImage. 我想旋转一个bufferedImage。 The code I use makes it possible that the image rotates. 我使用的代码使图像旋转成为可能。 But it cuts the image to a square. 但这会将图像切成正方形。 The screen shows then a black "border" on the left and the right side. 屏幕然后在左侧和右侧显示一个黑色的“边框”。
If I use debugging tool, the image width is about the whole width included the black "border". 如果使用调试工具,则图像宽度约为包括黑色“边框”在内的整个宽度。 But the black "border" don't rotate, it's always at the left and the right side. 但是黑色的“边框”不旋转,始终在左侧和右侧。 And the image is missing the picture-parts left and right. 并且图像缺少左右两边的图片部分。 The squared-image isn't cut again if I rotate it again. 如果再次旋转正方形图像,则不会再次剪切。 If I change the src.getWidth()-parts the image will be smaller with each rotation. 如果更改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));
     }
  } 

The size of the image will change as you rotate it. 图像的尺寸将随着旋转而改变。

Here is some code to play with: 这是一些可玩的代码:

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

Just change the image you want to read and use the slider to rotate the image. 只需更改要读取的图像,然后使用滑块旋转图像即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM