Rotate an image in java

前端 未结 4 1620
有刺的猬
有刺的猬 2020-11-27 21:40

I am looking to rotate an image. I have a JInternalFrame which contains a JLabel. The label contains the image. After the image has been rotated, I

相关标签:
4条回答
  • 2020-11-27 22:26

    Does it help if you change:

    BufferedImage DaImage = new BufferedImage(height, width, type);

    to:

    BufferedImage DaImage = new BufferedImage(**width, height**, type);?

    0 讨论(0)
  • 2020-11-27 22:28

    You could try using a Rotated Icon.

    0 讨论(0)
  • 2020-11-27 22:35

    Based on a previous example, but actually working with recent JDKs and in headless mode:

    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);
    }    
    
    0 讨论(0)
  • 2020-11-27 22:38

    You need to be using trigonometry to determine the correct width/height, using transparency to prevent the black area, and I think the Transform is wrong, which is making it off center.

    Try this:

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

    from http://flyingdogz.wordpress.com/2008/02/11/image-rotate-in-java-2-easier-to-use/

    0 讨论(0)
提交回复
热议问题